### Author Topic: Enemy movement question/bug.  (Read 3055 times)

#### test84

• Key Contributor
• Posts: 1328
• Karma: +0/-0
• ت
##### Enemy movement question/bug.
« on: Tue, Nov 29, 2011 »
Hi,

With some algorithm, I move my enemies but whenever they collide with the player, they stop, even if player goes out of their way. Is there any way to prevent them doing this so they would continue on their stupidly patrol behavior?

The code is simple and just a stupid patrolling:
Code: [Select]
`if (facing == FlxObject.RIGHT && 0 == velocity.x ) // Catch is if he hits an obstacle, his velocity will be zero. { turnAround(); } if (facing == FlxObject.LEFT && 0 == velocity.x ) { turnAround(); }`-tanks.
blog, twitter, Check out my award winning game, Rot Gut:

#### c023-DeV

• Game Artisan
• Active Member
• Posts: 176
• Karma: +0/-0
##### Re: Enemy movement question/bug.
« Reply #1 on: Tue, Nov 29, 2011 »
does your turn around function set acceleration or velocity? Is there a drag being set anywhere that's not being reset?
If you aint got no dedication, you won't get no education!

#### test84

• Key Contributor
• Posts: 1328
• Karma: +0/-0
• ت
##### Re: Enemy movement question/bug.
« Reply #2 on: Tue, Nov 29, 2011 »
No I'm not setting velocity each time because I'm just changing direction and that's actually what I just want, do I have to reset acceleration and stuff? I'm suspicious because they work OK if enemies don't collide with the player.

I remember there was a property like Solid or Immovable or something that was related to this, can't remember exactly ...
blog, twitter, Check out my award winning game, Rot Gut:

#### c023-DeV

• Game Artisan
• Active Member
• Posts: 176
• Karma: +0/-0
##### Re: Enemy movement question/bug.
« Reply #3 on: Tue, Nov 29, 2011 »
yeah, but did you set a acceleration OR velocity before? (I'd make a shame of myself to try to explain it because I'm not the math guy at all, but I guess that acceleration raps up and velocity is more fixed.)

If you set acceleration, I think you need to stop him with increasing the friction?!

And anyhow, you have to set a new value for the opposite direction in your turnAround() function like:

Code: [Select]
` private function turnAround():void { if (facing == RIGHT) { facing = LEFT; velocity.x = -MOVE_SPEED; } else { facing = RIGHT; velocity.x = MOVE_SPEED; } }`
and you have:

Code: [Select]
`moves = true;immovable = true;solid = true;allowCollisions = ANY; //also: LEFT, RIGHT etc...`
they do as they say.... just try them... but if you WANT the player to collide and get pushed try the immovable ?

OOOH: and try to take out the seccond "if"statement and taking out the "facing == FlxObject.LEFT &&" in the remaining because either way its zero and the turnAround() will do the facing check... so:

Code: [Select]
`if ( velocity.x == 0 ){ turnAround();} `
« Last Edit: Tue, Nov 29, 2011 by c023-DeV »
If you aint got no dedication, you won't get no education!

#### test84

• Key Contributor
• Posts: 1328
• Karma: +0/-0
• ت
##### Re: Enemy movement question/bug.
« Reply #4 on: Tue, Nov 29, 2011 »
umm, thanks for the reply but the code works OK if the player doesnt collide with the enemy, it's just with the collision moment of enemy and player that causes the enemy to stop or even worse, get pushed back or forth, I just want to prevent such "bully" effect of player towards enemies. (and enemies towards player)
blog, twitter, Check out my award winning game, Rot Gut:

#### c023-DeV

• Game Artisan
• Active Member
• Posts: 176
• Karma: +0/-0
##### Re: Enemy movement question/bug.
« Reply #5 on: Tue, Nov 29, 2011 »
then dont use collide on each other?! Use an overlap check for damage and push back of the player? Or try the immoveable flag?
If you aint got no dedication, you won't get no education!

#### camasthecat

• Contributor
• Posts: 461
• Karma: +0/-0
• WOW! 400+ Posts!? Jeez! Do I have a life!?!?
##### Re: Enemy movement question/bug.
« Reply #6 on: Tue, Nov 29, 2011 »
umm, thanks for the reply but the code works OK if the player doesn't collide with the enemy, it's just with the collision moment of enemy and player that causes the enemy to stop or even worse, get pushed back or forth, I just want to prevent such "bully" effect of player towards enemies. (and enemies towards player)

Grrr.. I hate this! Especially when the sprite get's pushed through the tilemap...

#### test84

• Key Contributor
• Posts: 1328
• Karma: +0/-0
• ت
##### Re: Enemy movement question/bug.
« Reply #7 on: Tue, Nov 29, 2011 »
Grrr.. I hate this! Especially when the sprite get's pushed through the tilemap...
Yeah, I hate it too.

We had a long discussion with LevelX about this and we came to the point of setting and removing immovable at tricky parts but that didn't help either.
blog, twitter, Check out my award winning game, Rot Gut:

#### camasthecat

• Contributor
• Posts: 461
• Karma: +0/-0
• WOW! 400+ Posts!? Jeez! Do I have a life!?!?
##### Re: Enemy movement question/bug.
« Reply #8 on: Wed, Nov 30, 2011 »
Hmm... What about setting "immovable" when touching a wall and colliding with another thing?

#### test84

• Key Contributor
• Posts: 1328
• Karma: +0/-0
• ت
##### Re: Enemy movement question/bug.
« Reply #9 on: Thu, Dec 1, 2011 »
blog, twitter, Check out my award winning game, Rot Gut:

#### camasthecat

• Contributor
• Posts: 461
• Karma: +0/-0
• WOW! 400+ Posts!? Jeez! Do I have a life!?!?
##### Re: Enemy movement question/bug.
« Reply #10 on: Thu, Dec 1, 2011 »
I think, for some reason, flixel is not checking the bounds when the sprite is being pushed... WHY?

Maybe flixel should have a variable called pushable or something that makes it so.. it can't be pushed. But how would this be achieved?

#### Kytin

• Member
• Posts: 22
• Karma: +0/-0
##### Re: Enemy movement question/bug.
« Reply #11 on: Thu, Dec 1, 2011 »
I have to echo c023-DeVs comment. Are you using collide or overlap checks to detect collisions between the player and enemies? You should be using overlap, if you aren't already.
I have seen the future. You will not.

#### test84

• Key Contributor
• Posts: 1328
• Karma: +0/-0
• ت
##### Re: Enemy movement question/bug.
« Reply #12 on: Fri, Dec 2, 2011 »
I have to echo c023-DeVs comment. Are you using collide or overlap checks to detect collisions between the player and enemies? You should be using overlap, if you aren't already.

Yes but then my player can go through the enmies, which I don't want to. I just want them to behave like they do and just don't push each other around, so they would just collide, finito.
blog, twitter, Check out my award winning game, Rot Gut:

#### Alextfish

• Active Member
• Posts: 174
• Karma: +0/-0
##### Re: Enemy movement question/bug.
« Reply #13 on: Fri, Dec 2, 2011 »
But it isn't "finito". What happens in the next timestep after the collision? Remember that "collide" in Flixel terms means "if they overlap, then separate them and reduce their velocities such that they're not going to run into each other next timestep". In particular, this means "collide" automatically implies in some cases "set velocity to zero". But not in others.

It sounds like you might want to have code in your enemy's update which sets its velocity to (maxspeed*facing_sign).

#### test84

• Key Contributor
• Posts: 1328
• Karma: +0/-0
• ت
##### Re: Enemy movement question/bug.
« Reply #14 on: Fri, Dec 2, 2011 »
Thanks Alextfish,

For now I'm replaced it with overlap because it has other problems such as when my enemies drop on my player, he would go into the levels and bugs like that.
blog, twitter, Check out my award winning game, Rot Gut: