I’m using this class to create random movement on a sprite however when I add another sprite its random movement is the same as the first sprite and they end up sitting on top of each other. Could anyone point me in the right direction as to where I’m going wrong with this.
package {
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;
import flash.geom.Point;
public class Nodes extends Sprite {
private var _particles:Array;
private var _numParticles:uint = 2;
private var _targetX:Number = Math.random() * stage.stageWidth;
private var _targetY:Number = Math.random() * stage.stageHeight;
private var _ease:uint = Math.random() * 5 + 20;
public function Nodes() {
init();
}
private function init():void {
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
_particles = new Array();
for (var i:uint = 0; i < _numParticles; i++) {
var particle:Ball = new Ball(5, 0x62C1AF);
particle.x = Math.random() * stage.stageWidth;
particle.y = Math.random() * stage.stageHeight;
particle.x = _targetX + Math.random() * stage.stageWidth;
particle.y = _targetY + Math.random() * stage.stageHeight;
addChild(particle);
_particles.push(particle);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
for (var i:uint = 0; i < _numParticles; i++) {
var particle:Ball = _particles*;
var _moveX = _targetX - particle.x;
var _moveY = _targetY - particle.y;
particle.x = particle.x + _moveX / _ease;
particle.y = particle.y + _moveY / _ease;
if (Math.sqrt(Math.pow(_moveX, 4) + Math.pow(_moveY, 4)) < 15) {
this._targetX = Math.random() * stage.stageWidth;
this._targetY = Math.random() * stage.stageHeight;
}
}
}
}
}
Cheers