views:

1597

answers:

2

I have buttons on the stage (run1_btn - run5-btn) that when clicked adds a movie clip to the stage.(hand) The movie clip contains a few frames of animation. When a button is clicked the movieclip gets added but the animation is already finished. I thought that when the mc got added to the stage then the animation would start, but this does not seem to be the case. Does anyone know a way around this.

Here is my code:

var handSlap:hand;
handSlap = new hand();

//event listeners
newPig.run1_btn.addEventListener(MouseEvent.CLICK, clickArea);
newPig.run2_btn.addEventListener(MouseEvent.CLICK, clickArea);
newPig.run3_btn.addEventListener(MouseEvent.CLICK, clickArea);
newPig.run4_btn.addEventListener(MouseEvent.CLICK, clickArea);
newPig.run5_btn.addEventListener(MouseEvent.CLICK, clickArea);




//functions
function clickArea(evtObj:MouseEvent):void
{
    trace(evtObj.target.name);
    addChild(handSlap);
    handSlap.x =200;
    handSlap.y=200;

}
A: 

possibly more elegant (depends on your point of view), it ensures that in any context the hand will restart its timeline animation when it's added to the stage:

hand.addEventListener(Event.ADDED_TO_STAGE, onHandAddedToStage, false, 0, true);
function onHandAddedToStage(event:Event):void
{
    var mc:Movieclip = MovieClip(event.currentTarget);
    mc.gotoAndPlay(1);
}

If you're not familiar with the event model, the "false, 0, true" bit just ensures that if you ever need to unload the hand it won't get snagged by the event listener and stay in memory, probably you don't need it but it does no harm.

alecmce
Thanks, this worked but I had to add the event listener to handSlap rather than hand. The next problem I have it that it only works once. The second and subsequent times a button is clicked nothing happens.Do I need to remove the the added movie clip before re adding another one?
jamie holliday
Yes, sorry - to handSlap not hand!Do you do anything like roving the event listener, or does the code look just like the code above?When the animation is finished what do you do? visible=false? If so do parent.removeChild(this) instead to remove handSlap from the stage.
alecmce
roving = removing, sorry on phone atm
alecmce
Ok thanks, I think I maybe missing something tho. I've added the full code as an answer because it won't let me add it all in the comments here. What I am trying to do is, when a button is clicked add the handSlap mc and let it play (a few seconds) when the button is clicked again do the same thing.(add the mc and play the animation) It is not playing through more than once. I guess I need to remove the first instance of the movie clip added before trying to add another. Thanks for you help (code is below)
jamie holliday
Still on phone, but try in your onClick before addChild: if(handSlap.parent) handSlap.parent.removeChild(handSlap); can't be more help til I get back from holidays. Good luck
alecmce
Excellent, that did the trick. Thanks for the help.
jamie holliday
A: 
  var newPig:pig;
newPig = new pig();
addChild(newPig);
newPig.y=360;
newPig.x=350;

var handSlap:hand;
handSlap = new hand();

//event listeners
newPig.run1_btn.addEventListener(MouseEvent.CLICK, clickArea);
newPig.run2_btn.addEventListener(MouseEvent.CLICK, clickArea);
newPig.run3_btn.addEventListener(MouseEvent.CLICK, clickArea);
newPig.run4_btn.addEventListener(MouseEvent.CLICK, clickArea);
newPig.run5_btn.addEventListener(MouseEvent.CLICK, clickArea);

handSlap.addEventListener(Event.ADDED_TO_STAGE, onHandAddedToStage, false, 0, true);


//functions
function clickArea(evtObj:MouseEvent):void
{
    trace(evtObj.target.name);

    addChild(handSlap);
    handSlap.x =200;
    handSlap.y=200;


}

function onHandAddedToStage(event:Event):void
{
    var mc:MovieClip = MovieClip(event.currentTarget);
    mc.gotoAndPlay(1);
}
jamie holliday