[help] Random _x position glitch

Hi all,

I’m currently trying to get a movieclip of mine to randomly pick a target x location, go to that location, and once it is there I need it to pick another location, rinse, repeat. My current code works for the first location (called targetx) but once it gets there it vibrates back and forth. I must be missing something…


onClipEvent(load){
var speed = 5;
this._y = 120;
this._x = 250;
var targetx = 250;
}
onClipEvent(enterFrame){ 
if (this._x > targetx){
this._x -= speed;
}else if (this._x < targetx){
this._x -= -speed;
}else if (this._x == targetx){
targetx = Math.random(1)*500;
}
}

Thanks in advance!

Nevermind, I fixed it. Here’s the updated code:


onClipEvent(enterFrame){
    if (this._x >= targetx + 5 ){
        this._x -= speed;
    }else if (this._x <= targetx - 5){
        this._x -= -speed;
    }else if ((Math.abs(this._x - targetx) <= 10)){
        targetx = Math.random(1)*500;
    }
}