ForumsProgramming ForumOne Function To Rule Them All

0 5458
weirdlike
offline
weirdlike
1,299 posts
Prince

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);
redEnemy.name = "red";

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

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

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

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

trace(currentEnemy.name)

if (currentEnemy.name == "red&quot
{
trace("red gunfire"
}
else if (currentEnemy.name == "blue&quot
{
trace("blue gunfire"
}
else if (currentEnemy.name == "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