0

(我是一个完全的菜鸟,这是我在 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 点完全一样的表情符号。

有任何想法吗?

提前致谢!

4

1 回答 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);
于 2013-06-03T03:10:37.897 回答