Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flash 动作脚本3:让电影唇一直播放到最后_Flash_Actionscript_Movieclip - Fatal编程技术网

Flash 动作脚本3:让电影唇一直播放到最后

Flash 动作脚本3:让电影唇一直播放到最后,flash,actionscript,movieclip,Flash,Actionscript,Movieclip,(我是一个彻头彻尾的noob,这是我在Flash/AS3中的第一个脚本,如果这是“常识”,请原谅) 我有一个大约10帧的“笑脸”电影剪辑。 现在,当一个人点击并拖动光标时,我会看到光标旁边的笑脸,这是我的代码: stage.addEventListener(MouseEvent.MOUSE_MOVE, mousePosition); var smiley:MovieClip = addChild(new Smiley) as MovieClip; stage.addEventListener

(我是一个彻头彻尾的noob,这是我在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点一样

有什么想法吗


提前谢谢

释放鼠标时调用toggleSmiley()时,笑脸电影剪辑将消失:(e.type==MouseEvent.mouse_DOWN)求值为false,这会使电影剪辑的visible属性为false,从而使其不可见(请记住,不可见剪辑实际上仍在舞台上,因为您没有删除它们…)

如果要使笑脸剪辑保持不变,然后在播放结束后消失,可以将Event.ENTER_FRAME Event附加到movieclip。“Event.ENTER_FRAME”事件在每次movieclip的帧滴答声时都会被抛出和处理。因此,处理程序可以检查当前帧是否位于最后一帧上,然后将其自身移除

比如:

// 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()事件监听器(因为它是创建笑脸片段的监听器),所以不会立即添加它。它仅在鼠标按下事件发生时添加,在鼠标向上事件发生时删除

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函数,但仍然引用了它(我在代码中留下了注释来标记位置),并且我也得到了以下错误类型error:error#1009:无法访问空对象引用的属性或方法。在mouseRyan2_fla::MainTimeline/smileyEnterFrame()[mouseRyan2_fla.MainTimeline::frame1:9]哦,是的,我看到了。对不起,我弄错了。我已经修复了代码,这次在flash中进行了快速测试。它在鼠标移动时工作(无需单击)。我使用了一个带有轮廓的旋转正方形,它实际上创造了一种有趣的3d效果,可能对某些东西来说很整洁。:)嘿谢谢,但我只需要在点击时使用它。。。因此,我将代码放入if()块中,但它不起作用:(以下是代码:我更新了我的答案,以考虑到只有在鼠标按下时才能创建笑脸片段。if语句不起作用的原因是,在该事件处理程序中,唯一的鼠标事件是mouse_MOVE,因为这是事件处理程序注册侦听的唯一鼠标事件。非常感谢感谢您不仅给了我答案,而且还解释了为什么、在哪里以及何时使用这些东西。我真的非常感谢您,并祝您度过美好的一天!干杯!