# Animate a movieclip to random locations on stage

**URL:** <https://forum.kirupa.com/t/animate-a-movieclip-to-random-locations-on-stage/293999>\
**Category:** Uncategorized\
**Created:** [August 6, 2009, 6:45pm UTC](https://forum.kirupa.com/t/animate-a-movieclip-to-random-locations-on-stage/293999 "2009-08-06T18:45:35Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThreeDollarBill](https://avatars.discourse-cdn.com/v4/letter/t/7ea924/32.png) [@ThreeDollarBill](https://forum.kirupa.com/u/ThreeDollarBill)\
**Post date:** [August 6, 2009, 6:45pm UTC](https://forum.kirupa.com/t/animate-a-movieclip-to-random-locations-on-stage/293999/1 "2009-08-06T18:45:35Z")

</div>

so basically, here’s what i’m trying to do…  
supposing there is a movieclip in stage… let’s say its a circle with an instance name of “theCircle”…

the circle has to move / go to / animate to a random location on stage every 5 seconds… basically, the circle should move to the random location, stop and wait for 5 seconds, then move again to another random location… again and again…

the code below is untested and incomplete… but it is what i am trying to do… please give me your suggestions on how i should go about this…

```auto
var randomXPosition:int;
var randomYPosition:int;
var positionTimer:Timer;
var theCircle:MovieClip

initGame();

function initGame():void
{
    randomXPosition = Math.round(Math.random() * stage.stageWidth);
    randomYPosition = Math.round(Math.random() * stage.stageHeight);
    
    stage.addEventListener(Event.ENTER_FRAME, moveCircle);
}

function moveCircle(event:Event):void
{
    /* need to put a code here to move the circle with instance name "theCircle"
    to the randomXPosition & randomYPosition at a constant speed */
    
    /* once the circle's X position is equal to randomXposition,
    and once the circle's Y position is equal to randomYposition,
    start the timer... */
    
    startTheTimer();
}

function startTheTimer():void
{
    positionTimer = new Timer(5000, 1);
    positionTimer.addEventListener(TimerEvent.TIMER, setPositionValues);
    positionTimer.start();
}

function setPositionValues(event:TimerEvent):void
{
    positionTimer.removeEventListener(TimerEvent.TIMER, setPositionValues);
    randomXPosition = Math.round(Math.random() * stage.stageWidth);
    randomYPosition = Math.round(Math.random() * stage.stageHeight);
}

```

on the moveCircle function, i can simply set the X and Y of the circle equal to randomXPosition and randomYPosition… but that would just change the location of the circle, not move it… i am trying to make the circle animate towards that location at a constant speed…
