Animating a nested movie clip

Hi,

what i’m looking to do is be able to animate a moving platform in my platformer game in as3.

I will post a working .swf version later (no access to a host atm) but heres my code:

i added some comments where the problem is.

sorry if i havent given enough information. will be more useful once i get the swf working.

what should happen is that movingPlatform moves to the right 256 units then back to the left. whilst hero.onMoving is true, then the hero.mc.x will follow the moving platform.


package {
	import flash.text.*;
	import flash.display.*;
	import flash.events.*;
	import flash.ui.Keyboard;
	import flash.utils.getTimer;
	import flash.media.Sound;

	public class Main extends MovieClip {
		//variables go here

		private var jumpSpeed:int=0;
		private var variableText:TextField = new TextField();


		private var currentLevel:int=4;




		private var lives:int=5;
		private var guiText:TextField = new TextField();
		private var thisLevel:int=1;
		private var gameMode:String="menu";
		private var hero:Object;
		private var jumpHeight:int;
		private var timeDiff:Number;
		private var firstJump:Boolean=true;
		static const gravity=20;
		private var lastTime:Number=0;
		private var mapShift:Boolean=false;
		private var screenNo:int=0;
		private var lastKey:int;

		private var beenCreated:Boolean=false;

		private var onPlatform:Boolean=false;

		private var vX:int=0;
		private var vY:int=0;

		//movieclips
		private var characterMc:MovieClip=new character  ;

		private var heroAbilityAnimation_mc:MovieClip = new shiftAnimation();
		private var menuBtn:MovieClip= new menuBtnSymbol();
		private var endLevel_mc:MovieClip = new endLevel();
		private var startGameBtn:startGameBtn_symbol=new startGameBtn_symbol  ;
		private var levelContainer:MovieClip = new MovieClip();


		/* 0 is nothing, 1 is boundaries, 2 is platform, 3 is moving platform 4 is ground, 5 = BLACK PLATFORM, 6 = WHITE PLATFORM there are 6 colums and 12 rows - 9 end of level*/
		private var levelPlan:Array = new Array(new Array
		//level 1
		(
		1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,5,0,0,0,5,0,0,0,0,0,0,0,0,9,0,1,
		1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,0,0,0,0,0,0,0,0,0,0,0,6,0,0,1,
		1,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,2,0,2,2,0,0,0,0,5,0,1,
		1,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,4,4,0,0,4,4,0,0,4,1)
		
		,
				//level 4

				new Array(
		1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,2,2,0,0,0,0,0,0,5,0,6,0,9,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,6,6,0,0,6,0,0,0,0,1,
		1,0,3,0,0,0,0,0,0,0,0,0,0,0,0,5,0,1,
		1,0,0,0,0,0,6,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,0,0,0,0,0,0,6,0,0,1,
		1,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,2,0,2,2,0,0,0,0,5,0,1,
		1,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
		1,0,0,0,0,0,0,0,4,4,0,0,4,4,0,0,4,1)
		);

		//objects that can be stood on
		private var platformObjects:Array;
		//main
		public function Main():void {
			trace("hi");
			menu();
			listeners();
		}
		//setup menu
		public function menu():void {
			trace("its the menu");
			startGameBtn.buttonMode=true;
			startGameBtn.x = (stage.stageWidth) / 2;
			startGameBtn.y = (stage.stageHeight) / 2;
			addChild(startGameBtn);
		}
		//start game after leav menu
		public function gogame(event:MouseEvent):void {
			removeChild(startGameBtn);
			gameMode="play";
			startgame();
			stage.focus=stage;
			variableText.x=200;
			variableText.y=10;
			variableText.width=500;
			variableText.text = String(("inAir = " + hero.inAir + "   jump = " + hero.jump + "   on platformm? = " + onPlatform));
			addChild(variableText);





			//gui
			menuBtn.x=stage.stageWidth-menuBtn.width;
			menuBtn.y=5;
			menuBtn.buttonMode=true;
			addChild(menuBtn);


			guiText.x=200;
			guiText.y=100;
			guiText.width=stage.stageWidth-50;
			guiText.text=String("Lives: "+lives);
			addChild(guiText);


		}

		//creates the hero
		public function createHero():void {
			hero=new Object  ;
			hero.mc=characterMc;
			hero.direction=0;
			hero.vx=0.0;
			hero.vy=0.0;
			hero.inAir=false;
			hero.falling=true;
			hero.jump=false;
			hero.jumpSpeed=-90;
			hero.walkSpeed=60;
			hero.mc.x=200;
			hero.mc.y=104;
			hero.animState="standBlack";
			hero.ability="black";
			hero.onMoving = false;
			hero.mc.gotoAndStop(1);
			addChild(hero.mc);
		}
		public function listeners():void {
			startGameBtn.addEventListener(MouseEvent.CLICK,gogame);
			menuBtn.addEventListener(MouseEvent.CLICK,returnMenu);

			this.addEventListener(Event.ENTER_FRAME,mainLoop);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,moveKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP,moveKeyUp);
		}
		public function returnMenu(e:MouseEvent):void {
			trace("return menu mans");
			removeChild(levelContainer);
			removeChild(menuBtn);
			removeChild(hero.mc);
			removeChild(variableText);
			menu();
		}
		public function startgame():void {
			createLevel();
			createHero();
			levelContainer.x=0;
			levelContainer.y=0;
			addChild(levelContainer);


		}
		public function updateText():void {
			variableText.text = String(("inAir = " + hero.inAir + "   jump = " + hero.jump + "   on platformm? = " + onPlatform));
			guiText.text=String("Lives: "+lives);
		}
		public function nextLevel():void {
			while ((levelContainer.numChildren > 0)) {
				levelContainer.removeChildAt(0);
			}
			currentLevel+=1;
			createLevel();
			resetScreen();

		}

		public function resetScreen():void {
			screenNo=0;
			levelContainer.x=0;
			hero.mc.x=100;
			hero.mc.y=200;
		}
		public function createLevel():void {
			var levelColumns:int=Math.ceil(levelPlan[int(currentLevel)-1].length/12);
			var row:int=0;





			//Creates the level using the levelPlan array.

			for (var i:int = 0; i<levelPlan[int(currentLevel) - 1].length; i++) {
				if (levelPlan[int(currentLevel)-1]*==1) {

					if (i/levelColumns==int(i/levelColumns)) {
						row++;
					}
					
					//generate end level
				} else if (levelPlan[int(currentLevel) - 1]*==9) {
					endLevel_mc.x =  (i-(row-1)*levelColumns)*newBlackPlatform.width -256;
					endLevel_mc.y = (row-1)*newBlackPlatform.height;
					levelContainer.addChild(endLevel_mc);
					trace(endLevel_mc.x);
					trace(endLevel_mc.y);
					
					//generate white platform
				} else if (levelPlan[int(currentLevel) - 1]*==6) {


					var newWhitePlatform:MovieClip = new platformWhite();
					newWhitePlatform.x = (i-(row-1)*levelColumns)*newWhitePlatform.width -256;
					newWhitePlatform.y = (row-1)*newWhitePlatform.height;

					levelContainer.addChild(newWhitePlatform);
					
					//generate black platform
				} else if (levelPlan[int(currentLevel) - 1]*==5) {


					var newBlackPlatform:MovieClip = new platformBlack();
					newBlackPlatform.x = (i-(row-1)*levelColumns)*newBlackPlatform.width -256;
					newBlackPlatform.y = (row-1)*newBlackPlatform.height;

					levelContainer.addChild(newBlackPlatform);
					
					//generate normal platform
				} else if (levelPlan[int(currentLevel) - 1]*==2) {


					var newPlatform:MovieClip = new platform();
					newPlatform.x = (i-(row-1)*levelColumns)*newPlatform.width - 256;
					newPlatform.y = (row-1)*newPlatform.height;
					
					// CALL THE FUNCTION HERE NOT WORKING
					//animateMovingPlatform(newPlatform);

					levelContainer.addChild(newPlatform);
				} else if (levelPlan[int(currentLevel) - 1]*==3) {


					var newMovingPlatform:MovieClip = new platform_moving();
					newMovingPlatform.x = (i-(row-1)*levelColumns)*newMovingPlatform.width - 256;
					newMovingPlatform.y = (row-1)*newMovingPlatform.height;

					levelContainer.addChild(newMovingPlatform);
				}
				
				
				else if (levelPlan[int(currentLevel) - 1]*==4) {
					var newGround:MovieClip=new ground();
					newGround.x=(i-(row-1)*levelColumns)*newGround.width-256;
					newGround.y=(row-1)*newGround.height;
					levelContainer.addChild(newGround);

				}


			}
			var totalWalls:int = (((stage.stageHeight/64)) * 2);
			trace(totalWalls);

			//add right walls
			for (var u:int=0; u<(totalWalls); u++) {
				var newWall:MovieClip = new wall();
				if (u<(totalWalls / 2)) {
					newWall.x=0;
					newWall.y=u*newWall.height;
				} else {
					newWall.x=levelContainer.width-newWall.width;
					newWall.y=((u - 12))*newWall.height;
				}

				levelContainer.addChild(newWall);
			}
			trace(levelContainer.width);
		}
		
		/* NOT WORKING
		public function animateMovingPlatform(e:MovieClip){
			var initialpos:int = e.x;
			while(initialpos < initialpos + 256){
				e.x += 8;
				initialpos += 8;
			}
		}
		*/
		public function mainLoop(e:Event):void {
			if (lastTime==0) {
				lastTime=getTimer();
			}
			timeDiff=getTimer()-lastTime;
			lastTime+=timeDiff;

			if (gameMode=="play") {
				moveCharacter(timeDiff);
				checkCollision();
				updateText();
				animationManager();



			}
		}
		public function createAnimations():void {
			if (lastKey==Keyboard.SHIFT) {
				heroAbilityAnimation_mc.x=hero.mc.x;
				heroAbilityAnimation_mc.y=hero.mc.y;
				addChild(heroAbilityAnimation_mc);
				heroAbilityAnimation_mc.beenCreated=true;
			}
		}
		public function animationManager():void {

			if ((heroAbilityAnimation_mc.currentFrameLabel=="end") && (heroAbilityAnimation_mc.beenCreated)) {
				heroAbilityAnimation_mc.beenCreated=false;
				removeChild(heroAbilityAnimation_mc);
			}
		}
		public function switchAbility() {
			if (hero.ability=="black") {
				hero.ability="white";
				hero.mc.gotoAndStop("standWhite");
			} else if ((hero.ability == "white")) {
				hero.ability="black";
				hero.mc.gotoAndStop("standBlack");
			}
			hero.animState="abilityChange";
		}
		public function checkCollision():void {
			var noChildren:int=levelContainer.numChildren;
			for (var i:int = 0; i<noChildren; i++) {
				var blockTest:DisplayObject=levelContainer.getChildAt(i);
				//if falling and hit test
				if ((hero.mc.hitTestObject(blockTest))) {
					
					trace(String(blockTest));
					//HIT COLOURED
					if ((hero.vy > 0) && (String(blockTest) != "[object wall]") && (hero.ability=="black") && (String(blockTest) == "[object platform_moving]")) {
						//hero.mc.x = blockTest.x + 126;
						onPlatform=true;
						hero.inAir=false;
						hero.jump=false;
						hero.mc.y=blockTest.y-hero.mc.height;
						hero.vy=0;
					}
					else if ((hero.vy > 0) && (String(blockTest) != "[object wall]") && (hero.ability=="black") && (String(blockTest) == "[object platformBlack]")) {
						onPlatform=true;
						hero.inAir=false;
						hero.jump=false;
						hero.mc.y=blockTest.y-hero.mc.height;
					}

					else if ((hero.vy > 0) && (String(blockTest) != "[object wall]") && (hero.ability=="white") && (String(blockTest) == "[object platformWhite]") && (String(blockTest) !="[object endLevel]")) {
						onPlatform=true;
						hero.inAir=false;
						hero.jump=false;
						hero.mc.y=blockTest.y-hero.mc.height;
					}



					//HIT GENERIC PLATFORM TOP
					else if ((hero.vy > 0) && (String(blockTest)!= "[object wall]") && ((String(blockTest)!="[object platformWhite]" && (String(blockTest) !="[object platformBlack]") && (String(blockTest) !="[object endLevel]")))) {
						onPlatform=true;
						hero.inAir=false;
						hero.jump=false;
						hero.mc.y=blockTest.y-hero.mc.height;
						hero.vy=0;
						//HIT GENERIC PLATFORM BOTTOM
					} else if ((hero.vy < 0) && (String(blockTest)!= "[object wall]")) {
						if ((hero.ability == "black") && (String(blockTest) == "[object platformBlack]")) {
							trace("black peeps may go thru");

						} else if ((hero.ability == "white") && (String(blockTest) == "[object platformWhite]")) {
							trace("white peeps may go thru");

						} else {
							onPlatform=false;
							hero.vy=50;
							hero.inAir=true;
							hero.jump=false;
						}
						//HIT WALL
					} else if ((String(blockTest)== "[object wall]")) {
						if (hero.direction==-1) {
							hero.mc.x=64;
							trace("hit left wall");
						}
						//HIT WALL
						if (hero.direction==1) {
							hero.mc.x = (stage.stageWidth - 128);
							trace(hero.mc.x);

							//hero.mc.x=stage.stageWidth -(levelContainer.width - stage.stageWidth) - hero.mc.width - 256;
							trace("hit right wall");
						}

					} else if ((String(blockTest) == "[object endLevel]")) {
						trace("end of game you deed it mang");
						nextLevel();
					}

				} else {
					if (hero.vy>0) {
						onPlatform=false;

					}

					if (! hero.jump) {
						hero.vy=90;
					}


				}




			}
			//stop left collision
			if (hero.mc.x >= (((stage.stageWidth - hero.mc.width))) && (hero.direction == 1)) {
				screenNo++;
				levelContainer.x-=stage.stageWidth;
				hero.mc.x=0;
			} else if ((hero.mc.x < 50) && (hero.direction == -1)) {
				screenNo--;
				trace("shift map left");
				hero.vx=0;
				hero.mc.x=stage.stageWidth;
				levelContainer.x+=stage.stageWidth;

			}








			if (hero.mc.y >= (stage.stageHeight)) {
				death();
			}

		}
		public function death():void {
			trace("you die nows");
			if (lives>0) {
				screenNo=0;
				levelContainer.x=0;
				lives-=1;
				hero.mc.x=100;
				hero.mc.y=200;
			} else {
				trace("GAEMZ OVA");
			}



		}

		public function moveCharacter(timeDiff:Number) {
			if (timeDiff<1) {
				return;
			}
			var timeDiffSec=timeDiff/100;
			if (hero.direction==1) {
				//moving right
				hero.vx=hero.walkSpeed;
			} else if (hero.direction == -1) {
				//moving left
				hero.vx=- hero.walkSpeed;
			} else if (hero.direction == 0) {
				//stationary
				hero.vx=0;
			}

			//faling 

			//jumping algorithms
			if (hero.jump) {
				hero.inAir=true;




				if (hero.vy<0) {
					hero.vy+=timeDiffSec*gravity;
				} else if (hero.vy == 0) {
					trace("apex");
					hero.vy+=timeDiffSec*gravity;

				} else if (hero.vy > 0) {
					hero.vy+=timeDiffSec*gravity;

				} else {
					hero.inAir=false;
				}

			}

			//move
			hero.mc.x+=hero.vx*timeDiffSec;
			hero.mc.y+=hero.vy*timeDiffSec;

		}
		public function moveKeyDown(event:KeyboardEvent) {
			lastKey=event.keyCode;
			if (event.keyCode==Keyboard.RIGHT) {
				hero.direction=1;
			} else if (event.keyCode==Keyboard.LEFT) {
				hero.direction=-1;
			} else if (event.keyCode==Keyboard.SPACE) {
				if (! hero.inAir) {
					hero.vy=hero.jumpSpeed;
					hero.jump=true;
					firstJump=false;
				}
			}
			if (event.keyCode==Keyboard.SHIFT) {
				switchAbility();
				createAnimations();

			}



		}
		public function moveKeyUp(event:KeyboardEvent) {
			if (event.keyCode==Keyboard.RIGHT) {
				hero.direction=0;
			} else if (event.keyCode==Keyboard.LEFT) {
				hero.direction=0;
			}

		}

	}

}