Be a Supporter!

Stop Enemy Jitter (as2)

  • 158 Views
  • 4 Replies
New Topic Respond to this Topic
Legodude2000
Legodude2000
  • Member since: Nov. 28, 2008
  • Offline.
Forum Stats
Member
Level 16
Game Developer
Stop Enemy Jitter (as2) 2014-04-13 15:31:20 Reply

The code below is for the enemy that has to follow the "player" mc and for the enemy to face the direction in which the "player" mc is in. The code works perfectly fine, but when its on the same x or y as the "player" mc, the enemy begins to jitter. Can someone tell me how to stop the jittering? Thanks.

Code:

onClipEvent(load){
scale=_xscale;
}
onClipEvent(enterFrame){
if(this._x<_root.player._x){
_xscale=+scale;
}
if(this._x>_root.player._x){
_xscale=-scale;
}
}
onClipEvent(enterFrame){
if(_root.player._x<this._x){
this._x-=3
}
else if(_root.player._x>this._x){
this._x+=3
}
}
onClipEvent(enterFrame){
if(_root.player._y<this._y){
this._y-=3
}
else if(_root.player._y>this._y){
this._y+=3
}
}


- LegoDudeStudios

BBS Signature
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to Stop Enemy Jitter (as2) 2014-04-13 15:38:32 Reply

Just check if the enemy is within 3 pixels before you do the move. If he is, don't bother moving it. You could also just add a margin of 3 pixels (the amount you're moving it by) to your if statements.

Legodude2000
Legodude2000
  • Member since: Nov. 28, 2008
  • Offline.
Forum Stats
Member
Level 16
Game Developer
Response to Stop Enemy Jitter (as2) 2014-04-13 15:56:17 Reply

At 4/13/14 03:38 PM, Sam wrote: Just check if the enemy is within 3 pixels before you do the move. If he is, don't bother moving it. You could also just add a margin of 3 pixels (the amount you're moving it by) to your if statements.

Nothing seems to work. What's another way of re-writing the code?


- LegoDudeStudios

BBS Signature
Legodude2000
Legodude2000
  • Member since: Nov. 28, 2008
  • Offline.
Forum Stats
Member
Level 16
Game Developer
Response to Stop Enemy Jitter (as2) 2014-04-13 16:32:10 Reply

At 4/13/14 03:56 PM, Legodude2000 wrote:
At 4/13/14 03:38 PM, Sam wrote: Just check if the enemy is within 3 pixels before you do the move. If he is, don't bother moving it. You could also just add a margin of 3 pixels (the amount you're moving it by) to your if statements.
Nothing seems to work. What's another way of re-writing the code?

Never mind. Thanks, fixed it.

onClipEvent(load){
scale=_xscale;
}
onClipEvent(enterFrame){
if(this._x<_root.player._x){
_xscale=+scale;
}
if(this._x>_root.player._x){
_xscale=-scale;
}
}
onClipEvent (enterFrame) {
if (_root.player._x>_x) {
_x += 2;
}
if (_root.player._x<_x) {
_x -= 2;
}
if (_root.player._y>_y) {
_y += 2;
}
if (_root.player._y<_y) {
_y -= 2;
}
}


- LegoDudeStudios

BBS Signature
Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to Stop Enemy Jitter (as2) 2014-04-13 17:14:29 Reply

You haven't solved the root problem. You also don't need separate enterFrames, just have the one. Additionally, use code tags to preserve formatting.

speed = 3;

onClipEvent(enterFrame)
{
   if(Math.abs(player.x - x) > speed)
   {
        // move x 
    }
}

I haven't touched AS2 in a long time, so this code won't just work by copy and paste. By subtracting the x's of the two objects and absoluting the value (gets the amount from 0, makes negative into positive in practice) we can tell how far apart they are. If it's greater than how much the object can move per frame, then move it. Otherwise, they're close enough. You can do an else and snap it to the x of the player if you want to here, or whatever you want to do when their x's are close enough to be considered equal by your game.

As I try to tell everybody using AS2, move to AS3!