ActionScript 3.0:移动对象,移动之间有延迟

发布于 2024-11-08 00:10:32 字数 4558 浏览 4 评论 0原文

我正在开发一款游戏,我希望将影片剪辑从一个随机位置移动到另一个位置,并在每个移动之间暂停。我在一些教程的帮助下编写了一些代码,但我无法让它按照我想要的方式工作。我对 ActionScript 非常陌生,所以我可能做错了一切。现在,我的游戏所做的就是暂停一定时间,然后对象以极快的速度从一个位置跳到另一个位置,并且不会停止。

//movement of searchlight

var i:int;
for (i = 0; i < 5; i++)
{
var startTime = getTimer();

stage.addEventListener(Event.ENTER_FRAME, timeDelay);

function timeDelay(event:Event):void
{
    var timePassed = getTimer();
    if (timePassed - startTime >= 5000)
    {
        moveTo();
    }
}

myObject.x = Math.round(Math.random() * 550);
myObject.y = Math.round(Math.random() * 400);
myObject.rotation = Math.round(Math.random() * 360);

// a counter that counts the current
// frame of the animation
var currentFrameCount:int;
// the total frames in the animation
var totalFrameCount:int = 20;

// the location when the animation starts
var initialX:Number;
var initialY:Number;
// the distance between the initial
// location and the destination
var distanceX:Number;
var distanceY:Number;

// when animation starts
function moveTo():void
{

    var myLocationX = Math.round(Math.random() * 550);
    var myLocationY = Math.round(Math.random() * 400);

    // the destination location 
    var destinationX:Number = myLocationX;
    var destinationY:Number = myLocationY;

    // reset frame count to 0
    currentFrameCount = 0;
    // set initial locations to the
    // current location of myObject
    initialX = myObject.x;
    initialY = myObject.y;
    // find the distance values by finding
    // the difference between initial and destination
    distanceX = destinationX - initialX;
    distanceY = destinationY - initialY;
            // set up rotation
    var initialAngle = myObject.rotation;
    var myAngleStart = Math.atan2(myLocationY-initialY,myLocationX-initialX)/(Math.PI/180);
    var myAngle = myAngleStart + 284;
    myObject.rotation = myAngle;

    // set up the enterFrame loop to 
    // animate the animation
    myObject.addEventListener(Event.ENTER_FRAME, animateMoveTo);

    // handling the animation over time;
    function animateMoveTo(evt:Event):void
    {
        // each frame, increment the frame count
        // to move the animation forward
        currentFrameCount++;
        // if the frame count has not yet reached the
        // final frame of the animation, myObject
        // needs to be moved to the next location
        if (currentFrameCount < totalFrameCount)
        {
            // find the progress by comparing current frame
            // with the total frames of the animation
            var progress:Number = currentFrameCount / totalFrameCount;
            // set myObject's position to include the new
            // distance from the original location as
            // defined by the distance to the destination
            // times the progress of the animation
            myObject.x = initialX + distanceX * progress;
            myObject.y = initialY + distanceY * progress;
            myObject.rotation = initialAngle + myAngle * progress;
        }
        else
        {
            // when the animation is complete, set the
            // position of myObject to the destination
            myObject.x = destinationX;
            myObject.y = destinationY;

            // remove the enterFrame event handler so the 
            // animation ceases to run
            myObject.removeEventListener(Event.ENTER_FRAME, animateMoveTo);
        }
    }
}
}

更新:这就是我最终所做的:

import com.greensock.TweenLite;

//set random starting location and rotation
myObject.x = Math.round(Math.random() * 550);
myObject.y = Math.round(Math.random() * 400);
myObject.rotation = Math.round(Math.random() * 360);

//create variables
var initialX:Number;
var initialY:Number;
var destinationX:Number;
var destinationY:Number;
var myAngleStart;
var myAngle;

//timer
var timer:Timer = new Timer( 12000 )
timer.addEventListener( TimerEvent.TIMER,rotate );
timer.start();

//function rotate
function rotate( e:TimerEvent ):void{
//set variables for current location
initialX = myObject.x
initialY = myObject.y
//set new destination
destinationX = Math.round(Math.random() * 550);
destinationY = Math.round(Math.random() * 400);
//set rotation
myAngleStart = Math.atan2(destinationY-initialY,destinationX-initialX)/(Math.PI/180);
myAngle = myAngleStart + 284;
//rotate towards new destination
TweenLite.to(myObject, 5, {rotation:myAngle, onComplete:moveTo});
}

//function moveTo
function moveTo():void {
TweenLite.to(myObject, 7, {x:destinationX, y:destinationY});
}

I am working on a game in which I want a movie clip to move from one random location to another, with a pause in between each movement. I have written some code with the help of some tutorials, but I can't get it to work the way I want it to. I am extremely new to ActionScript, so I'm probably doing everything wrong. Right now all my game does is pause for a certain amount of time, and then the object just jumps from one location to another extremely quickly and doesn't stop.

//movement of searchlight

var i:int;
for (i = 0; i < 5; i++)
{
var startTime = getTimer();

stage.addEventListener(Event.ENTER_FRAME, timeDelay);

function timeDelay(event:Event):void
{
    var timePassed = getTimer();
    if (timePassed - startTime >= 5000)
    {
        moveTo();
    }
}

myObject.x = Math.round(Math.random() * 550);
myObject.y = Math.round(Math.random() * 400);
myObject.rotation = Math.round(Math.random() * 360);

// a counter that counts the current
// frame of the animation
var currentFrameCount:int;
// the total frames in the animation
var totalFrameCount:int = 20;

// the location when the animation starts
var initialX:Number;
var initialY:Number;
// the distance between the initial
// location and the destination
var distanceX:Number;
var distanceY:Number;

// when animation starts
function moveTo():void
{

    var myLocationX = Math.round(Math.random() * 550);
    var myLocationY = Math.round(Math.random() * 400);

    // the destination location 
    var destinationX:Number = myLocationX;
    var destinationY:Number = myLocationY;

    // reset frame count to 0
    currentFrameCount = 0;
    // set initial locations to the
    // current location of myObject
    initialX = myObject.x;
    initialY = myObject.y;
    // find the distance values by finding
    // the difference between initial and destination
    distanceX = destinationX - initialX;
    distanceY = destinationY - initialY;
            // set up rotation
    var initialAngle = myObject.rotation;
    var myAngleStart = Math.atan2(myLocationY-initialY,myLocationX-initialX)/(Math.PI/180);
    var myAngle = myAngleStart + 284;
    myObject.rotation = myAngle;

    // set up the enterFrame loop to 
    // animate the animation
    myObject.addEventListener(Event.ENTER_FRAME, animateMoveTo);

    // handling the animation over time;
    function animateMoveTo(evt:Event):void
    {
        // each frame, increment the frame count
        // to move the animation forward
        currentFrameCount++;
        // if the frame count has not yet reached the
        // final frame of the animation, myObject
        // needs to be moved to the next location
        if (currentFrameCount < totalFrameCount)
        {
            // find the progress by comparing current frame
            // with the total frames of the animation
            var progress:Number = currentFrameCount / totalFrameCount;
            // set myObject's position to include the new
            // distance from the original location as
            // defined by the distance to the destination
            // times the progress of the animation
            myObject.x = initialX + distanceX * progress;
            myObject.y = initialY + distanceY * progress;
            myObject.rotation = initialAngle + myAngle * progress;
        }
        else
        {
            // when the animation is complete, set the
            // position of myObject to the destination
            myObject.x = destinationX;
            myObject.y = destinationY;

            // remove the enterFrame event handler so the 
            // animation ceases to run
            myObject.removeEventListener(Event.ENTER_FRAME, animateMoveTo);
        }
    }
}
}

UPDATE: Here's what I ended up doing:

import com.greensock.TweenLite;

//set random starting location and rotation
myObject.x = Math.round(Math.random() * 550);
myObject.y = Math.round(Math.random() * 400);
myObject.rotation = Math.round(Math.random() * 360);

//create variables
var initialX:Number;
var initialY:Number;
var destinationX:Number;
var destinationY:Number;
var myAngleStart;
var myAngle;

//timer
var timer:Timer = new Timer( 12000 )
timer.addEventListener( TimerEvent.TIMER,rotate );
timer.start();

//function rotate
function rotate( e:TimerEvent ):void{
//set variables for current location
initialX = myObject.x
initialY = myObject.y
//set new destination
destinationX = Math.round(Math.random() * 550);
destinationY = Math.round(Math.random() * 400);
//set rotation
myAngleStart = Math.atan2(destinationY-initialY,destinationX-initialX)/(Math.PI/180);
myAngle = myAngleStart + 284;
//rotate towards new destination
TweenLite.to(myObject, 5, {rotation:myAngle, onComplete:moveTo});
}

//function moveTo
function moveTo():void {
TweenLite.to(myObject, 7, {x:destinationX, y:destinationY});
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱没勇气 2024-11-15 00:10:32

您应该研究许多方便的补间引擎之一,例如 TweenLiteTweener。他们让这种事情变得非常容易。

You should look into one of the many convenient Tweening engines, such as TweenLite or Tweener. They make this sort of thing very easy.

涙—继续流 2024-11-15 00:10:32

好的,我对此做了一些工作,我认为这很接近你想要的。轮换工作不正常,但我现在没有时间去搞乱它。该方法高度依赖于帧速率。如果用户帧速率低于您设置的电影帧速率,则使用此方法进行补间将会出现问题。我将帧速率设置为 30fps。我认为这可以让您走上改进代码的正确轨道:

    //Declare Globals
var currentFrameCount:int=0;
var totalFrameCount:int = 30;
myObject.x = Math.round(Math.random() * 550);
myObject.y = Math.round(Math.random() * 400);
var destinationX:Number = myObject.x;
var destinationY:Number = myObject.y;
var initialX:Number;
var initialY:Number;
var distanceX:Number;
var distanceY:Number;
var xProg:Number;
var yProg:Number;
var myAngleChange;
var myAngleEnd;
var initialAngle;

var countFrame:int = 0;
var delay:int = 0;

addEventListener(Event.ENTER_FRAME,callDelay);
function callDelay(e:Event){
        countFrame++;
        delayEvent(delay);
}
//Code to move the object to a random location and give it a random target to move to.
function spawnObject(){
        myObject.rotation = Math.round(Math.random() * 360);//random rotation
        initialAngle = myObject.rotation;
        destinationX = Math.round(Math.random() * 550);//random destination
        destinationY = Math.round(Math.random() * 400);
        currentFrameCount = 0;
        initialX = myObject.x;
        initialY = myObject.y;
        distanceX = destinationX - initialX;//distance between destination and initial
        distanceY = destinationY - initialY;
        initialAngle = myObject.rotation;//buggy rotation code
        myAngleEnd = Math.atan2(destinationY-initialY,destinationX-initialX)/(Math.PI/180);
        myAngleChange = (initialAngle - myAngleEnd)/totalFrameCount;
        xProg = distanceX/totalFrameCount;//amount to increase by each frame
        yProg = distanceY/totalFrameCount;
}


function delayEvent(period){
        if ( (countFrame) >= period){
                removeEventListener(Event.ENTER_FRAME,callDelay);
                timedEvent();
                countFrame=0;
                return;
        }

}


function timedEvent ():void
{
                currentFrameCount = totalFrameCount;
                myObject.x = destinationX;
                myObject.y = destinationY;
                spawnObject();//move the object to a new location and give new destination
                myObject.addEventListener(Event.ENTER_FRAME,moveTo);//add an event listener to move the object around
}

function moveTo(e:Event):void
{
                myObject.rotation += myAngleChange; //increase rotation by the rotation step value (buggy)
        currentFrameCount++;
        if (currentFrameCount < totalFrameCount)
        { 
            myObject.x += xProg;//incrase x by the x step value
            myObject.y += yProg;//increase y by the y step value
        }
        else
        {
            myObject.removeEventListener(Event.ENTER_FRAME, moveTo);// remvoe this event listener
                        addEventListener(Event.ENTER_FRAME,callDelay);
        }
}

OK, I worked on this a bit and I think this is close to what you want. The rotation doesn't work properly, but I don't have time to mess with that right now. This method is highly Dependant on frame rate. There will be problems if you tween using this method if the users framerate drops below the frame rate you have the movie set at. I set my frame rate for 30fps. I think this can set you on the right track to improving your code though:

    //Declare Globals
var currentFrameCount:int=0;
var totalFrameCount:int = 30;
myObject.x = Math.round(Math.random() * 550);
myObject.y = Math.round(Math.random() * 400);
var destinationX:Number = myObject.x;
var destinationY:Number = myObject.y;
var initialX:Number;
var initialY:Number;
var distanceX:Number;
var distanceY:Number;
var xProg:Number;
var yProg:Number;
var myAngleChange;
var myAngleEnd;
var initialAngle;

var countFrame:int = 0;
var delay:int = 0;

addEventListener(Event.ENTER_FRAME,callDelay);
function callDelay(e:Event){
        countFrame++;
        delayEvent(delay);
}
//Code to move the object to a random location and give it a random target to move to.
function spawnObject(){
        myObject.rotation = Math.round(Math.random() * 360);//random rotation
        initialAngle = myObject.rotation;
        destinationX = Math.round(Math.random() * 550);//random destination
        destinationY = Math.round(Math.random() * 400);
        currentFrameCount = 0;
        initialX = myObject.x;
        initialY = myObject.y;
        distanceX = destinationX - initialX;//distance between destination and initial
        distanceY = destinationY - initialY;
        initialAngle = myObject.rotation;//buggy rotation code
        myAngleEnd = Math.atan2(destinationY-initialY,destinationX-initialX)/(Math.PI/180);
        myAngleChange = (initialAngle - myAngleEnd)/totalFrameCount;
        xProg = distanceX/totalFrameCount;//amount to increase by each frame
        yProg = distanceY/totalFrameCount;
}


function delayEvent(period){
        if ( (countFrame) >= period){
                removeEventListener(Event.ENTER_FRAME,callDelay);
                timedEvent();
                countFrame=0;
                return;
        }

}


function timedEvent ():void
{
                currentFrameCount = totalFrameCount;
                myObject.x = destinationX;
                myObject.y = destinationY;
                spawnObject();//move the object to a new location and give new destination
                myObject.addEventListener(Event.ENTER_FRAME,moveTo);//add an event listener to move the object around
}

function moveTo(e:Event):void
{
                myObject.rotation += myAngleChange; //increase rotation by the rotation step value (buggy)
        currentFrameCount++;
        if (currentFrameCount < totalFrameCount)
        { 
            myObject.x += xProg;//incrase x by the x step value
            myObject.y += yProg;//increase y by the y step value
        }
        else
        {
            myObject.removeEventListener(Event.ENTER_FRAME, moveTo);// remvoe this event listener
                        addEventListener(Event.ENTER_FRAME,callDelay);
        }
}
临风闻羌笛 2024-11-15 00:10:32

如果您只是想以设定的间隔移动对象,只需使用此

var timer:Timer = new Timer( 1000 )
timer.addEventListener( TimerEvent.TIMER,moveTo );
timer.start();

function moveTo( e:TimerEvent ):void{
    myObject.x = Math.round(Math.random() * 550);
    myObject.y = Math.round(Math.random() * 400);
}

If you are just looking to move an object at set intervals just use this

var timer:Timer = new Timer( 1000 )
timer.addEventListener( TimerEvent.TIMER,moveTo );
timer.start();

function moveTo( e:TimerEvent ):void{
    myObject.x = Math.round(Math.random() * 550);
    myObject.y = Math.round(Math.random() * 400);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文