Venki
Venki

Reputation: 7

hit and explode an object using a bullet in Actionscript 3

I am making a small game in which I have an enemy flying around at the top of the screen and I have a player shooting bullets from the bottom. I have an explosion MovieClip, which I need to play when the enemy gets hit by a bullet. After the hit enemy should be hidden and the explosion MovieClip should appear at its place. Can someone help me with this?

Upvotes: 0

Views: 826

Answers (1)

Vesper
Vesper

Reputation: 18747

Say Explosion is a library asset of the explosion, enemy is the enemy in question, bullet is the bullet in question. Do like this:

 if (enemy.hitTestObject(bullet)) {
     var explosion:Explosion=new Explosion();
     explosion.x=enemy.x;
     explosion.y=enemy.y;
     addChild(explosion);
     removeChild(enemy);
     // removeChild(bullet); // optional
     // do any cleanup, and don't forget to track explosion
     // so it will be removed once finished.
 }

Upvotes: 2

Related Questions