Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Why baddie won't stop?


Hynx
 Share

Recommended Posts

My friend and I were trying to do the baddie move in the tutorial, following the player, and it all went so well. But then we tried to make the baddie stop, and stop its animation that time, however the baddie keeps going into other ifs and wont stop at all.

 

The code is like this:

 

    if (Math.round(player.body.screenX) == Math.round(baddie.body.screenX))
    {
        baddie.body.velocity.x = 0;
        baddie.animations.stop(false);        
    }
    if (Math.round(player.body.screenX) < Math.round(baddie.body.screenX))
    {
        //  Move to the left
        baddie.body.velocity.x = -90;
        baddie.animations.play('left');
    }
    else if (Math.round(player.body.screenX) > Math.round(baddie.body.screenX))
    {
        //  Move to the right
        baddie.body.velocity.x = 90;
        baddie.animations.play('right');
    }
 
We found a workaround for this, by moving the baddie one pixel at the time like below, but we wondered if there is any way to do it with velocity, or if we did something wrong:
 
    if (Math.round(player.body.screenX) < Math.round(baddie.body.screenX))
    {
        //  Move to the left
        baddie.x += -1;
        baddie.animations.play('left');
    }
    else if (Math.round(player.body.screenX) > Math.round(baddie.body.screenX))
    {
        //  Move to the right
        baddie.x += 1;
        
        baddie.animations.play('right');
    }
    else if (Math.round(player.body.screenX) == Math.round(baddie.body.screenX))
    {
        baddie.animations.stop();
        baddie.frame = 1
    }
 
Can someone help, please?
 
Thanks.

 

Link to comment
Share on other sites

I guess that's probably because Math.round(player.body.screenX) is never exactly equal to Math.round(baddie.body.screenX)

 

Instead of checking for equals you could check if the baddie is within a small range of the player and stop.

The problem is that we used a debug text to check if we were getting into that IF and this was the case. It keeps coming in and out of the IF, like the baddie won't be stopping in time so it won't be falling into the second IF.

 

=( So velocity.x = 0 should stop the sprite IMMEDIATLY?

Link to comment
Share on other sites

I had the same issue myself. My enemies were walking to the left constantly. I'm still working on my AI code for more advance movement but I found using if(game.physics.distancebetween(player, baddies < 100)) { // baddies does stuff } works great for a lot of stuff. Thought it may be worth a shot for changing directions etc. Can even set the distance between each baddie. If baddie walks with range of another baddies make the baddie do something new, change direction etc etc. Hope this is helpful to you :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...