Here’s my code. (It’s a class for a MovieClip which is attached to the stage)
class People extends MovieClip {
var speed:Number = 2;
var old:Number = 0;
var newY:Number = 0;
var RobotSpeed:Number = 0;
var xVally:Number;
function onLoad(){
rando();
this._x = xVally;
this._y = 350;
speed = Math.round(Math.random(2)+1);
}
function onEnterFrame(){
if(xVally == -10){
this._x += speed;
this._xscale = -100;
}else if(xVally == 710){
this._x -= speed;
}
newY = _root.RobotInstance._y;
checkRemove();
this.swapDepths(_root.getNextHighestDepth());
checkCollision();
checkY();
}
function rando(){
if(Math.round(Math.random(1,0)) == 1){
xVally = -10;
}
if(Math.round(Math.random(1,0)) == 0){
xVally = 710;
}
}
function checkRemove(){
if(this._x > Stage.width+10 && xVally == -10){
this.removeMovieClip();
}
if(this._x < -10 && xVally == 710){
this.removeMovieClip();
}
}
function checkCollision(){
var xVal:Number = this._x;
if(this.hitTest(_root.RobotInstance) && RobotSpeed < 0){
this.removeMovieClip();
_root.attachMovie("Bloody", "BloodMC"+_root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:xVal, _y:355});
}
}
function checkY(){
RobotSpeed = (old-newY);
old = newY;
}
}
It works pretty good, however some of the People, instead of spawning at X 710 or -10, just spawn at X 0 and stay there. What is causing this and more importantly, how can I fix it?