ActionScript 3:使movieclip播放到最后

时间:2013-06-03 01:16:09

标签: flash actionscript movieclip

(我是一个完整的菜鸟,这是我在Flash / AS3中的第一个脚本之一,如果这是“常识”,请原谅我) 我有一个大约10帧的“笑脸”电影剪辑 现在,当一个人点击并拖动时,我得到了光标旁边的笑脸,这是我的代码:

stage.addEventListener(MouseEvent.MOUSE_MOVE, mousePosition);

var smiley:MovieClip = addChild(new Smiley) as MovieClip; 
stage.addEventListener(MouseEvent.MOUSE_DOWN,toggleSmiley);
stage.addEventListener(MouseEvent.MOUSE_UP,toggleSmiley); 

function mousePosition(event:MouseEvent) {
smiley.x = mouseX; smiley.y = mouseY;

}

function toggleSmiley(e:MouseEvent):void
{
    smiley.visible = (e.type == MouseEvent.MOUSE_DOWN); 
    }

问题是:
1 - 如果一个人快速点击并快速释放它不会播放整个笑脸动画片段,因为它们会释放它消失,我如何让它播放整个笑脸影片剪辑?

2 - 如果他们点击并拖动,我希望它留下一串与上面第1点完全相同的表情符号。

有什么想法吗?

提前致谢!

1 个答案:

答案 0 :(得分:1)

当释放鼠标时,笑脸影片剪辑消失,因为toggleSmiley()被调用:( e.type == MouseEvent.MOUSE_DOWN)评估false,这使得影片剪辑的可见属性为false,这使其不可见(保持在请注意,隐形剪辑实际上仍然在舞台上,因为你没有删除它们......)

如果你想让笑脸片段保持不变,那么当它完成播放时就会消失,你可以将一个Event.ENTER_FRAME事件附加到动画片段。每当movieclip的帧勾选时,抛出并处理'Event.ENTER_FRAME'事件。所以处理程序你可以检查当前帧是否在最后一帧,然后让它自行删除。

类似的东西:

// and event handler that runs every frame tick of a movieclip
// when it detects the current frame is the same as the last frame (indicating it is done playing) remove it from the stage
function smileyEnterFrame(inputEvent:Event):void {
    var clip:MovieClip = (MovieClip) (inputEvent.target); // inputEvent.target should refer to the smiley clip as it is what threw the event (just need to cast this to a movieclip)
    if(clip.currentFrame == clip.totalFrames){
        // remove this event handler for the clip since the clip is set to be removed (no longer need the event listener)
        clip.removeEventListener(Event.ENTER_FRAME, smileyEnterFrame);
        // remove the clip from the stage
        clip.parent.removeChild(clip);
    }
}

然后返回原始代码:

stage.addEventListener(MouseEvent.MOUSE_MOVE, mousePosition);

// moved this line in to the mouse movement, as that is where the clip would actually be created
// (when the mouse moves to a new position)
//var smiley:MovieClip = addChild(new Smiley) as MovieClip; 

//stage.addEventListener(MouseEvent.MOUSE_DOWN,toggleSmiley); // no longer need this
//stage.addEventListener(MouseEvent.MOUSE_UP,toggleSmiley); // no longer need this

function mousePosition(event:MouseEvent) {
    var smiley:MovieClip = new Smiley();
    smiley.x = mouseX;
    smiley.y = mouseY;
    this.addChild(smiley);

    smiley.addEventListener(Event.ENTER_FRAME, smileyEnterFrame, false, 0, true);
}

// no longer needed as the smiley clips will remove themselves once they are done playing
/*function toggleSmiley(e:MouseEvent):void {
    smiley.visible = (e.type == MouseEvent.MOUSE_DOWN); 
}*/

在我的评论

中编辑

将所有生成的代码现在一起添加到一个框中可能更容易。这很麻烦,但是我已将所有原始代码保留在那里注释掉,这样你就可以看到我在做什么。

我做的改变是删除了mousePosition()事件监听器(因为它是创建笑脸剪辑的那个)所以它不会马上添加。它仅在MOUSE_DOWN事件发生时添加,并在MOUSE_UP事件发生时被删除。

import flash.events.MouseEvent;

// and event handler that runs every frame tick of a movieclip
// when it detects the current frame is the same as the last frame (indicating it is done playing) remove it from the stage
function smileyEnterFrame(inputEvent:Event):void {
    var clip:MovieClip = (MovieClip) (inputEvent.target); // inputEvent.target should refer to the smiley clip as it is what threw the event (just need to cast this to a movieclip)
    if(clip.currentFrame == clip.totalFrames){
        // remove this event handler for the clip since the clip is set to be removed (no longer need the event listener)
        clip.removeEventListener(Event.ENTER_FRAME, smileyEnterFrame);
        // remove the clip from the stage
        clip.parent.removeChild(clip);
    }
}

// moved to toggleSmiley() as this event listener that spawns the smiley clips 
// when the mouse moves, should only be running when the mouse button is down
//stage.addEventListener(MouseEvent.MOUSE_MOVE, mousePosition);

// moved this line in to the mouse movement, as that is where the clip would actually be created
// (when the mouse moves to a new position)
//var smiley:MovieClip = addChild(new Smiley) as MovieClip; 

//stage.addEventListener(MouseEvent.MOUSE_DOWN,toggleSmiley); // no longer need this
//stage.addEventListener(MouseEvent.MOUSE_UP,toggleSmiley); // no longer need this

function mousePosition(inputEvent:MouseEvent) {
    var smiley:MovieClip = new Smiley();
    smiley.x = inputEvent.stageX;
    smiley.y = inputEvent.stageY;
    smiley.addEventListener(Event.ENTER_FRAME, smileyEnterFrame, false, 0, true);
    this.addChild(smiley);
}

// no longer needed as the smiley clips will remove themselves once they are done playing
/*function toggleSmiley(e:MouseEvent):void {
    smiley.visible = (e.type == MouseEvent.MOUSE_DOWN); 
}*/

// this adds or removes the mousePosition() event listener based on the given mouse event
function toggleSmiley(inputEvent:MouseEvent):void {
    // if down, then add this event listener (which would create the smiley clips when the mouse moves)
    if(inputEvent.type == MouseEvent.MOUSE_DOWN){
        this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mousePosition, false, 0, true);
    // else on any other mouse event (MOUSE_UP), remove this event listener to stop the smiley clips from being created
    } else {
        this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mousePosition, false);
    }
}

this.stage.addEventListener(MouseEvent.MOUSE_UP,toggleSmiley);
this.stage.addEventListener(MouseEvent.MOUSE_DOWN,toggleSmiley);