1 Replies - 2129 Views - Last Post: 01 May 2010 - 05:40 AM

#1 Guest_Albert*


Reputation:

[AS3] Death Animation help

Posted 29 April 2010 - 10:04 PM

I am making a very simply zombie game in flash using AS3. I have an enemy movieclip with a death animation which starts @ frame 2. Within my main class, i have code that removes the enemy using removeChild, but i want the animation to play before it removes the player. So i tried using something similar to this :

if ( enemy health = 0)
{
enemy.gotoAndPlay(2);
removeChild(enemy);
}

But whenever it runs, the enemy gets removed without playing its animation. Can somebody please help me out in finding a way to get the animation to play THEN remove the movieclip? Here is my Main class code:

package 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class main extends MovieClip
	{
		private var score:Number;
		private var numEnemies:Number;
		private var numAmmo:Number;
		private var enemySpawnTimer:Timer = new Timer(1000, 1);
		private var ammoTimer:Timer = new Timer(7000,1);
		private var _bullets:Array;
		private var _enemies:Array;
		private var _ammo:Array;
		private var _damage:Number;
		
		public function main()
		{
		    score = 0;
			numEnemies = 0;
			numAmmo = 0;
			_bullets = new Array();
			_bullets = [];
			_enemies = new Array();
			_enemies = [];
			_ammo = new Array();
			_ammo = [];
			
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		private function onAddedToStage(event:Event):void
		{
			player.stop();
			enemySpawnTimer.addEventListener(TimerEvent.TIMER , onTimeStart);
			ammoTimer.addEventListener(TimerEvent.TIMER , onAmmoStart);
			stage.addEventListener("bulletCreated", onBulletCreated);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
			addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		private function onRemovedFromStage(event:Event):void
		{
			enemySpawnTimer.removeEventListener(TimerEvent.TIMER , onTimeStart);
			ammoTimer.removeEventListener(TimerEvent.TIMER , onAmmoStart);
			stage.removeEventListener("bulletCreated", onBulletCreated);
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			
		}
		private function onBulletCreated(event:Event):void
		{
			_bullets.push(MovieClip(event.target));
		}
		private function onTimeStart(event:TimerEvent):void
		{
			var angle = Math.random()*2*Math.PI;
			var newX = Math.cos(angle)*500 + stage.stageWidth;
			var newY = Math.cos(angle)*500 + stage.stageHeight;
			var newEnemy:Enemy = new Enemy();
			
			_enemies.push(newEnemy);
			newEnemy.x = newX;
			newEnemy.y = newY;
			addChild(newEnemy);
			numEnemies++;
		}
		private function onAmmoStart(event:TimerEvent):void
		{
			var newAmmo:Ammo = new Ammo();
            addChild(newAmmo);
			newAmmo.x = (Math.random()*500);
			newAmmo.y = (Math.random()*200);
			_ammo.push(newAmmo);
			numAmmo++;
			
			if (player.hitTestObject(newAmmo))
			{
				player.ammo = 20;
				numAmmo--;
				removeChild(newAmmo);
			}
			
		}
		public function onEnterFrame(event:Event):void
		{
			//Start enemy timer
			if (numEnemies < 10){
				enemySpawnTimer.start();
			}
			//Start ammo timer
			if (numAmmo < 1){
				ammoTimer.start();
			}
			
			//blockPlayer();
			
		for (var j:int = 0; j < _bullets.length ; j++)
				{
					for (var i:int = 0; i < _enemies.length ; i++)
					{
						
						if (_bullets[j].hitTestObject(_enemies[i]))
						{
							score+=20;
							scoretext.text = String(score);
							_enemies[i].sub.health.scaleX -= .2;
							stage.removeChild(_bullets[j]);
							_bullets.splice(j,1);
							j--;
								
								if(_enemies[i].sub.health.scaleX <= 0)
									{

                     //THIS IS WHERE IM TRYING TO GET THE ANIMATION TO WORK
									_enemies[i].gotoAndPlay(2);
									removeChild(_enemies[i]);
									_enemies.splice(i,1);
									i--;
									numEnemies--;
									}
						}
					}
				}
				
				
		for (var g:int = 0; g < _enemies.length ; g++)
		{
			
			if (_enemies[g].hitTestObject(player))
				{
					health.scaleX -= .005;
				}
		}
		
		
		if (health.scaleX <= 0)
			{
			var mainscreen = new startMain();
			mainscreen.x = stage.stageWidth/2;
			mainscreen.y = stage.stageHeight/2;
			parent.addChild(mainscreen);
			parent.removeChild(this);
			removeChild(player);
			}
				
			
		for (var k:int = 0; k < _ammo.length ; k++)
			{
				if(player.hitTestObject(_ammo[k]))
				{
					if(player.ammo < 20 )
					{
					player.ammo = 20;
					removeChild(_ammo[k]);
					_ammo.splice(k,1);
					k--;
				    numAmmo--;
					ammotext.text = "20";
					}
				}
			}
		
		}
	}
	}




Please all help is greatly appreciated!! Even if there is another approach to doing it, please help me ...

Thank you

Is This A Good Question/Topic? 0

Replies To: [AS3] Death Animation help

#2 Jason_Fields  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 14
  • Joined: 06-December 09

Re: [AS3] Death Animation help

Posted 01 May 2010 - 05:40 AM

One way you could do this is to put something like this into onEnterFrame
if(enemy.health <= 0)
{
     enemy.gotoAndPlay(2);
}
if(enemy.currentFrame > /*last frame of death animation*/)
{
     removeChild(enemy);
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1