Search code examples
actionscript-3movieclip

AS3 - Trying to declare the value of a Movie Clip


Okay, I have the following code in my Bullet.as file:

public var impact:MovieClip;

public function Bullet():void
{
    addEventListener(Event.ADDED_TO_STAGE, whenAdded);
}

function whenAdded(e:Event)
{
    if(this is zArrow){
       power = -1;
       speed = 15;
       impact = arrowImpact;
       trace(impact);
    }
    if(this is Dice){
       power = -Math.round(Math.random()*5 + 1);
       speed = 10;
       impact = diceImpact
    }
}

See, I am trying to set the value of "public var impact:MovieClip" as the movie clip "arrowImpact" or "diceImpact". What I want is whenever a bullet collides with an enemy, it leaves an impact image behind and I'm trying to change what impact is shown depending on what bullet is colliding.

I am able to change all of the other variables like power and speed using this setup, but I can't declare which impact movie clip the "impact" movie clip variable is.


Solution

  • From the way I understand your question now, you want to pull these specific movie clips from the Library. If I am not mistaken. To do this, you need to pair each of the movie clips in the library to an AS class that extends MovieClip.

    Make sure you check "Export for Actionscript" and create the class you want for each. Then, in your code for the Bullet, you can create a new instance of them. So have it say:

    impact = new ArrowImpact)();
    

    or DiceImpact depending on your classes.

    Hope this is along the lines of what you wanted.

    In order to use these, I would recommend creating a getImpact method along the lines of:

    public function getImpactMC():MovieClip
    {
        return impact;
    }
    

    Then all you need to do in your main Document is addChild the proper impact from this method. However, be aware that you need to adjust the x and y values of the impactMC before adding it as a child on the stage to make sure that it displays in the proper position.

    Glad this helps!