Faktori Posted November 11, 2015 Share Posted November 11, 2015 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 More sharing options...
Skeptron Posted November 11, 2015 Share Posted November 11, 2015 I could make it work : http://phaser.io/sandbox/edit/aZekiQWZ (press left to flip image) Does it help? Faktori 1 Link to comment Share on other sites More sharing options...
Faktori Posted November 11, 2015 Author Share Posted November 11, 2015 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 More sharing options...
Skeptron Posted November 11, 2015 Share Posted November 11, 2015 You shouldn't use scale to do health bars, you should use crop. It's safer (ie your issue) and if you use sprites it's gonna look much better. Link to comment Share on other sites More sharing options...
Recommended Posts