Jump to content

Flipping sprite enemy sprite horizontally


Faktori
 Share

Recommended Posts

Hey guys, new on phaser, currently learning js at school, I'm getting started on my first game.

 

In a setup where there are multiple enemies following the player in a top-down view, I want the enemy to flip horizontally to face in direction of the player. I have a problem with this chunk of code : 

flip: function() {   // for each enemy   for (i=0; i<enemies.z; i++) {      // if the enemy is on the right of the character and if it's facing right (scale.x>0)      // flip enemy      if (enemies.getAt(i).body.position.x > player.body.position.x + 5 && enemies.getAt(i).scale.x > 0) {          enemies.getAt(i).scale.x *= -1;      }      // if the enemy is on the left of the character and if it's facing left (scale.x<0)      // flip enemy      if (enemies.getAt(i).body.position.x < player.body.position.x - 5 && enemies.getAt(i).scale.x < 0) {          enemies.getAt(i).scale.x *= -1;      }   }},

If the enemy is coming from the right, it will disappear when the flip should occur.

I've tested enemies.getAt(i).scale.x *= -1; in the console and it does work. the problem must be with the position check, but can't figure it out. Btw, I can't use scale.x = -1; since each enemy has it's own scale and it's used in other ways.

 

I've tested the same code with enemies.getAt(i).width *= -1;

same problem, works in console, not in action.

 

If you guys have any idea of why this doesn't work, I would be happy to know :)

 

Link to comment
Share on other sites

OK I FOUND THE ISSUE !!!!

This is so stupid actually, since I use scale as a health counter. You reduce when you get hit and if you go under a certain scale, you die. So here, the scale value be comes negative and kills enemy.

 

I feel dumb now.

 

@Skeptron : well, I ended up finding the solution myself. Thanks for your time !!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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