Hi,
How can i give random movements for a movieclip in actionscript 3.0
Printable View
Hi,
How can i give random movements for a movieclip in actionscript 3.0
find a random point
move your movieClip to it
when he arrives, do it again forever
That depends on whether or not you want A) movement in a random angle, or B) just want to bounce it around a random point on stage repeatedly...
Untested.Code:// for A)
//...
var deg2rad:Number = Math.PI/180;
var speed:Number = 3; // set to speed you want
someClip.addEventListener(Event.ENTER_FRAME, moveIt);
someClip.rotation = Math.random() * 360;
var angle:Number = someClip.rotation * deg2rad;
function moveIt(e:Event) {
e.target.x -= speed*Math.cos(angle);
e.target.y -= speed*Math.sin(angle);
}
// for B)
// ...
someClip.addEventListener(Event.ENTER_FRAME, bounceIt);
function bounceIt(e:Event) {
e.target.x = Math.random()*stage.stageWidth;
e.target.y = Math.random()*stage.stageHeight;
}