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});
}