ForumsProgramming ForumOne Function To Rule Them All

0 1998
1,305 posts

A pretty nifty trick I learned that saved me literally hours of coding, is by using the .currentTarget in combination with .name

For simplicity I will use 3 different enemies that point to only one function.

var redEnemy:rEnemy = new rEnemy();
addChild(redEnemy); = "red";

var blueEnemy:bEnemy = new bEnemy();
addChild(blueEnemy); = "blue";

var greenEnemy:gEnemy = new gEnemy();
addChild(greenEnemy); = "green";

redEnemy.addEventListener("dispatchEvent", enemyFire);
blueEnemy.addEventListener("dispatchEvent", enemyFire);
greenEnemy.addEventListener("dispatchEvent", enemyFire);

function enemyFire(event):void
var currentEnemy:Object = event.currentTarget;


if ( == "red&quot
trace("red gunfire"
else if ( == "blue&quot
trace("blue gunfire"
else if ( == "green&quot
trace("green gunfire"

now you might say what is the point of .currentTarget but you can use the object that you declared as the current target (in this case I used currentEnemy) and use its x and y coordinates for whatever you need.

  • 0 Replies
Showing 1-0 of 0