在as3中2秒后制作一个movieclip跟随鼠标

时间:2016-07-10 17:23:04

标签: actionscript-3 flash-cs5

我试图获得一个电影剪辑" orb_mc"在延迟2秒之后沿着鼠标指针的相同纵坐标移动,也就是说在球体移动到与指针相同的位置之前应该有2秒的延迟。下面是我的代码 - 一切正常,除了延迟。

import flash.events.Event;

Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveThatMouse);

function moveThatMouse(evt:MouseEvent):void {
    wand.x = stage.mouseX;
    wand.y = stage.mouseY;
    evt.updateAfterEvent();
}

var myTimer:Timer = new Timer(2000);
        var speed:Number=10;

        orb_mc.addEventListener(Event.ENTER_FRAME,follow);
        function follow(e:Event):void{
            myTimer.start();
           orb_mc.x -= (orb_mc.x - mouseX) / speed;
            orb_mc.x = orb_mc.x +2;
           orb_mc.y -= (orb_mc.y - mouseY) / speed;
            orb_mc.y = orb_mc.y +3;

        }

1 个答案:

答案 0 :(得分:1)

在这种情况下,我不打算使用计时器。根据您对我的评论的回复,我了解您只是希望orb_mc能够顺利地移动到鼠标停止移动的位置。当然你可以简单地说一下“如果鼠标没有移动,将orb发送到鼠标”的效果,但问题是你会得到这些不稳定的动作,因为鼠标移动不会有这些小时刻注册(特别是当鼠标减慢以开始移动另一个方向时)。这就是为什么要添加某种延迟的原因。所以我接近这个的方法是基本上每个帧都有一个计数器,并且每当鼠标移动时重置为0。

import flash.display.MovieClip;
import flash.events.Event; 
Mouse.hide(); 

var mouseCounter: int = 0;
var mouseDelay: int = 20; // how many frames the mouse must stay still before the follow code is run.    
// var myTimer:Timer = new Timer(2000); won't need this with my method.
var speed:Number=10; 

stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);

stage.addEventListener(Event.ENTER_FRAME,follow);

// set counter back to zero whenever the mouse is moved. 
function mouseMove(e:MouseEvent):void
{
    wand.x = stage.mouseX;
    wand.y = stage.mouseY;
    e.updateAfterEvent();
    mouseCounter = 0;
}

function follow(e:Event):void{
    // increment the counter each frame
    mouseCounter++; 

    // now run the follow block if the mouse has been still for enough frames. 
    if (mouseCounter >= mouseDelay)
    {
        //myTimer.start();  won't need this for my method. 
        orb_mc.x -= (orb_mc.x - mouseX) / speed;
        orb_mc.x += 2;
        orb_mc.y -= (orb_mc.y - mouseY) / speed;
        orb_mc.y += 3;
     }
}