ActionScript 3:使影片剪辑播放到最后

Posted

技术标签:

【中文标题】ActionScript 3:使影片剪辑播放到最后【英文标题】:ActionScript 3: Make the movieclip play to the end 【发布时间】:2013-05-29 03:20:55 【问题描述】:

(我是一个完全的菜鸟,这是我在 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】:

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

如果您想让笑脸剪辑在播放完毕后消失,您可以将 Event.ENTER_FRAME 事件附加到影片剪辑。 '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);

【讨论】:

嗨!感谢回复!出现一些错误:1 - 您注释掉了 toggleSmiley 函数,但仍然引用了它(我在代码中留下了注释以标记该位置)并且我也收到此错误 TypeError: Error #1009: Cannot access a property或空对象引用的方法。在 mouseRyan2_fla::MainTimeline/smileyEnterFrame()[mouseRyan2_fla.MainTimeline::frame1:9] 哦,是的,我明白了。抱歉,是我的错。我已经修复了代码,这次在 Flash 中对其进行快速测试。它在鼠标移动时起作用(无需单击)。我使用了一个带有轮廓的旋转正方形,它实际上创造了一种有趣的 3d 效果,可能很适合某些东西。 :) 嘿!谢谢,但我只需要在单击时工作......所以我将代码放入 if() 块中,但它不起作用:( 代码如下:pastebin.com/Q3wVyVzX 我已经更新了我的答案,以考虑到笑脸剪辑只能在鼠标按下时创建。您的 if 语句不起作用的原因是因为在该事件处理程序中,唯一的鼠标事件是 MOUSE_MOVE,因为这是事件处理程序注册侦听的唯一鼠标事件。 非常感谢您不仅给了我答案,还解释了为什么、在哪里以及何时使用这些东西。我真的很感激,祝你有美好的一天!干杯!

以上是关于ActionScript 3:使影片剪辑播放到最后的主要内容,如果未能解决你的问题,请参考以下文章

将鼠标悬停在完整播放影片剪辑

Flash Actionscript 3.0 影片剪辑过渡(无按钮)

用于按钮的简单 Actionscript 3

ActionScript:符号影片剪辑不循环

在 EXIT_FRAME 上的 Actionscript 3.0 影片剪辑修改

反向播放影片剪辑并返回到第 1 帧