Weird math.random() problem

Hi all.

I’m working on a little project in which some movieclips are to move random between certain X and Y values. Basically Ive just tried to make this into AS3 code.

This is what it looks like:

this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

var acceleration:Number = 10;
var ranx:Number = (Math.round(Math.random()*358))+59;
var rany:Number = (Math.round(Math.random()*72))+64;
    
function newpos():void
{
    trace("newpos");
    ranx = (Math.round(Math.random()*358))+59;
    rany = (Math.round(Math.random()*72))+64;
}

function enterFrameHandler(event:Event)
{
    this.x += ((ranx-this.x)/acceleration);
    this.y += ((rany-this.y)/acceleration);
    if (Math.round(this.x) == ranx || Math.round(this.y) == rany) 
    {
        newpos();
    }        
}

My problem, then, is this: I need to change the acceleration var to get the movieclip to move slower. However, when i set it to 12 og higher the movieclip will only generate 3 or 4 new coordinates, and then stop completely.

Does anyone know why this is?

Ive attached my .fla so you all can see for yourselves. The code is in the first frame the movieclips timeline.

I hope you can all help as im pretty new to actionscripting.

Thanks,
Esben

try just use some tween engine and randomize target coords

Thanks for your reply Felixz, but i am not sure about what you mean by ‘tween engine’, could you provide a link to an example of one such? Remember that i have almost no experience with AS, so i mostly cut n paste stuff i find on the Internet.

Also, i kind of like the code ive used, does anyone know why i am not able to change the acceleration var above 11 without the whole thing going wrong?

Esben

Problem solved courtesy of mr. Bombdogs at actionscript.org:

u can use Tween class which is built in.
Or TweenLite, Tweener, Fuse etc.
U can search for them or u can go directly here