Skip to content

Instantly share code, notes, and snippets.

@samsmo
Created February 10, 2012 22:27
Show Gist options
  • Save samsmo/1793581 to your computer and use it in GitHub Desktop.
Save samsmo/1793581 to your computer and use it in GitHub Desktop.
Tap-A-Story Document Class
package Code {
import flash.display.*;
import flash.display.Sprite;
import flash.events.*;
import flash.ui.Multitouch;
import flash.ui.MultitouchInputMode;
import flash.sensors.Accelerometer;
import flash.utils.*;
public class Document extends MovieClip {
private var myAcc:Accelerometer;
private var sceneArray:Array = new Array();
private var crtScene:int=0;
private var aScene:SceneBase;
private var layerArray:Array = new Array();
private var night:Boolean = false;
private var acceleration:Number = 0;
private var originalX:Array= new Array();
private var currentMC;
private var soundLayer:MovieClip;
private var soundBox:MovieClip;
private var lastScene;
private var sceneLayer:MovieClip;
private var firstScene:Menu_Screen;
private var buttonLayer:GUIHOLDER;
private static var _instance:Document;
public static function get instance():Document { return _instance; }
public function Document() {
// constructor code
//create a reference to the Document class so we can access it in other places
_instance = this;
// create a layer for the scenes
sceneLayer = new MovieClip;
//add that layer to the Display List
addChild(sceneLayer);
//create the first Screen
firstScene = new Menu_Screen();
sceneLayer.addChild(firstScene);
//Make the story button clickable
firstScene.fox_BTN.addEventListener(MouseEvent.CLICK, initStory);
}
private function initStory(e:MouseEvent){
//clean up after ourselves
firstScene.removeEventListener(MouseEvent.CLICK,initStory);
//return the next Scene
currentMC = firstScene.returnClass();
//Garbage man! clean up the menu screen!
sceneLayer.removeChild(firstScene);
firstScene = null;
//add the next scene to the
sceneLayer.addChild(currentMC);
buttonLayer = new GUIHOLDER();
addChild(buttonLayer);
//currentMC = this.getChildAt(0);
//push the layers of the current Scene into an Array
//this is so we can parallax later!
for(var i:int =0;i<currentMC.numChildren; i++){
layerArray.push(currentMC.getChildAt(i));
}
//this array is also for parallaxing, here we are getting the original x positions
for each(var aLayer:MovieClip in layerArray){
originalX.push(aLayer.x);
}
//setup the orientation so we can determine hold you're holding the ipad
var startOrientation:String = stage.orientation;
if (startOrientation == StageOrientation.DEFAULT || startOrientation == StageOrientation.UPSIDE_DOWN)
{
stage.setOrientation(StageOrientation.ROTATED_RIGHT);
}
else
{
stage.setOrientation(startOrientation);
}
//set up the accelerometer for parallaxing
myAcc = new Accelerometer();
//set the timeout for the accelerometer
myAcc.setRequestedUpdateInterval(60);
myAcc.addEventListener(AccelerometerEvent.UPDATE,tiltUpdate);
//make sure the stage doesnt scale and align it properly
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
//this event is for the switching of night/day
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, onChanging);
//make stuff touchable!
Multitouch.inputMode = MultitouchInputMode.GESTURE;
//enter frame to do some parallaxing!
stage.addEventListener(Event.ENTER_FRAME, onEnter);
//GUI Events!
buttonLayer.nextBTN.addEventListener(MouseEvent.CLICK, onClick);
buttonLayer.soundBTN.addEventListener(MouseEvent.CLICK,soundStart);
buttonLayer.homeBTN.addEventListener(MouseEvent.CLICK, goHome);
}
private function goHome(e:MouseEvent):void{
gotoAndStop('Menu');
}
private function soundStart(e:MouseEvent):void{
soundLayer.soundBox.play();
}
private function onEnter(e:Event):void{
//set up parallaxing!
if(acceleration > .05 || acceleration < -.05){
var count:int =0;
var movement:int = 25;
for each(var layer:MovieClip in layerArray){
if(layerArray[0].x >= originalX[0]+25)
{
layer.x = originalX[count]+movement;
}else if(acceleration > 0){
if(layer.x < originalX[count]+movement
&& (acceleration*(movement/2)+layer.x) < originalX[count]+movement)
{
layer.x +=(acceleration*(movement/2));
}
}if(layerArray[0].x <= originalX[0]-25){
layer.x = originalX[count]-movement;
}else if (acceleration < 0){
if(layer.x > originalX[count]-movement
&& (acceleration*(movement/2)+layer.x) > originalX[count]-movement)
{
layer.x+=(acceleration*(movement/2));
}
}
movement = movement + 25;
count++;
}
}
}
public function hideNext():void{
buttonLayer.nextBTN.visible = false;
}
public function showNext():void{
buttonLayer.nextBTN.visible = true;
}
public function goToNextPage():void{
//clear out the scene arrays ( layer and its x pos )
layerArray.splice(0, layerArray.length);
originalX.splice(0, layerArray.length);
//go to the next scene
lastScene = currentMC;
currentMC = null;
currentMC = lastScene.returnClass();
//Garbage man! clean up the menu screen!
sceneLayer.removeChild(lastScene);
lastScene = null;
//add the next scene to the
sceneLayer.addChild(currentMC);
//populate the arrays
for(var i:int =0;i<currentMC.numChildren; i++)
{
layerArray.push(currentMC.getChildAt(i));
var changeTime = currentMC.getChildAt(i);
if(changeTime.getChildByName("soundBox")){
soundLayer = changeTime;
soundLayer.soundBox.stop();
}
try
{
switch(night)
{
case true:
changeTime.gotoAndStop('night');
break;
case false:
changeTime.gotoAndStop('day');
break;
}
}catch(error:Error){
break;
}
}
for each(var aLayer:MovieClip in layerArray){
originalX.push(aLayer.x);
}
}
private function onClick(e:MouseEvent):void{
goToNextPage();
}
public function tiltUpdate(e:AccelerometerEvent):void {
//store the ipad acceleration for later use
acceleration = e.accelerationX;
}
private function onChanging(e:StageOrientationEvent):void {
if (e.afterOrientation == StageOrientation.DEFAULT
|| e.afterOrientation == StageOrientation.UPSIDE_DOWN)
{
e.preventDefault();
}
switch (e.afterOrientation) {
case StageOrientation.ROTATED_LEFT:
//stage.setOrientation(StageOrientation.ROTATED_LEFT);
if(night){
night = false;
for each (var mc:MovieClip in layerArray) {
mc.gotoAndStop("day");
}
}else{
night = true;
for each (var mc2:MovieClip in layerArray) {
mc2.gotoAndStop("night");
}
}
break;
case StageOrientation.ROTATED_RIGHT:
if(night){
night = false;
for each (var mc3:MovieClip in layerArray) {
mc3.gotoAndStop("day");
}
}else{
night = true;
for each (var mc4:MovieClip in layerArray) {
mc4.gotoAndStop("night");
}
}
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment