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


last edited Apr 30 2014 05:00 pm by weirdlike