HELP object moves left, when hits a wall, how do I make him turn around and move that
Hey guys,
I am not a compleat noob at actionscripting but I have a problem here..very tricky and flash always ends up terminating the script during gamplay as it just does not work the way I think it should.
Anyway. I will give you an example of what I am doing:
I am making a game like Security 2. 2D overhead view where you are an agent going from room to room collecting objects and compleating objectives.
So far I have managed to create 1 unit..and duplicate that to make many more enemys which makes things very easy.
Now, I have the enemy spawn randomly around the map..max units are 7 and they will auto move to the left. Code looks like this:
this._x = random(600)+100;
this._y = random(480);
this.speed = random(3)+1;
now..I want it so when they meet a wall, they will rotate 180 degrees and move the other way...and t hen onec they hit another wall..they will rotate again and move that way!
this code is long here..works perfectly atm...it spawns the units..tells them to move at a random speed and spawn randomly...if you can..please show me how to make it so..hey will turn and move the other way!!
onClipEvent (load) {
function reset() {
if (this._name == "enemy") {
this._visible = false;
} else {
this._visible = true;
}
this._x = random(600)+100;
this._y = random(480);
this.speed = random(3)+1;
}
this.reset();
}
onClipEvent (enterFrame) {
this._x -= this.speed;
if (this._x<-50) {
this.reset();
}
}
onClipEvent (load) {
power = 3;
radius = 6;
}
onClipEvent (enterFrame) {
while (_root.wall.hitTest(_x-radius, _y, true)) {
_x++;
}
while (_root.wall.hitTest(_x+radius, _y, true)) {
_x--;
}
if ((_root.enemy.hitTest(_x, _y+radius, true)) or (_root.enemy.hitTest(_x, _y-radius, true)) or (_root.enemy.hitTest(_x+radius, _y, true)) or (_root.enemy.hitTest(_x-radius, _y, true))) {
_x = 1000;
_y = 1000;
}
}
Thank you for listening and I hope you can give me a hand. :p