简体   繁体   English

动态将动画片段添加到舞台as3

[英]Dynamically adding movieclip to stage as3

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. 我在舞台上有一些按钮(run1_btn-run5-btn),单击这些按钮会将影片剪辑添加到舞台上。(手)影片剪辑包含几帧动画。 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. 我以为,当mc添加到舞台上时,动画便会开始,但事实并非如此。 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;

}

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. 如果您不熟悉事件模型,则“ false,0,true”位只是确保如果您需要卸载指针,则不会被事件监听器卡住并留在内存中,可能您不会不需要它,但没有害处。

  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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM