mon Posted July 19, 2014 Share Posted July 19, 2014 Hi there!I'm new to phaser and I've just tried to create a simple game (here: http://fdzteam.com/shooter/)The problem is If i move my ship to top-left corner of game world, all newly-created enemies will seem to be collided with my ship. This is my collisionHandler code:collideHandle: function() { for (var i = 0; i < enemies.length ; i++) { if (enemies[i].live > 0) { //Here if (enemies[i].obj.overlap(player)){ this.playerLive -= 1; enemies[i].live = 0; this.liveText.text = "Lives: " + this.playerLive; this.explose(enemies[i].obj.x, enemies[i].obj.y); if (this.playerLive <= 0) this.endGame(); } for (var j=0; j < lasers.length; j++) { if (lasers[j].live > 0 && enemies[i].obj.overlap(lasers[j].obj)) { this.explose(enemies[i].obj.x, enemies[i].obj.y); enemies[i].live --; if (enemies[i].live == 0) { score += 10; this.scoreText.text = "Score: " + score; } lasers[j].live--; lasers[j].obj.kill(); } } } else enemies[i].obj.kill(); }},Can anyone find out the problem?Thanks so much! Link to comment Share on other sites More sharing options...
mon Posted July 20, 2014 Author Share Posted July 20, 2014 BUMP! Link to comment Share on other sites More sharing options...
lewster32 Posted July 20, 2014 Share Posted July 20, 2014 Presumably when you make a new enemy it starts off at 0, 0 - your collide handler is checking to see if anything is overlapping the ship each frame, so when an enemy is created at 0, 0 (before it's moved) and your ship is at the top left (also 0, 0 or near) this reports that the two are overlapping. Out of interest, is there a reason you're using this method and not the Arcade physics collide/overlap stuff? I'd imagine in nearly all cases Arcade physics would be more optimal. mon 1 Link to comment Share on other sites More sharing options...
mon Posted July 21, 2014 Author Share Posted July 21, 2014 This is my "addEnemy" function addEnemy: function() { var enemy = this.game.add.sprite(game.world.width , Math.floor(Math.random() * (game.world.height - 120)) + 70 , 'mine'); enemy.anchor.setTo(0.5, 0.5); enemy.animations.add('fly'); enemy.animations.play('fly', 20, true); enemies.push({obj: enemy, live: mineBaseLive}); },I'm wondering that if i use this.game.add.sprite(x, y , ..), my Enemy will start off at 0,0 or at x,y instead?About Arcade physics, I've just known it yesterday. I'll try to use It nowANW, Thanks so much for you supporting Link to comment Share on other sites More sharing options...
lewster32 Posted July 21, 2014 Share Posted July 21, 2014 There's a chance the sprite starts at 0, 0 internally before being moved - because of execution order your array may be queried before the sprite has actually been re-positioned to its final place. You could maybe try this if you want to continue using your method:addEnemy: function() { var enemy = this.game.add.sprite(game.world.width , Math.floor(Math.random() * (game.world.height - 120)) + 70 , 'mine'); enemy.anchor.setTo(0.5, 0.5); enemy.animations.add('fly'); enemy.animations.play('fly', 20, true); // use a short timer (20ms so it happens after at least one frame - 1 frame being roughly 16.7ms) to ensure the object is positioned before being added to the array this.game.time.events.add(20, function() { enemies.push({obj: enemy, live: mineBaseLive}); }, this); },It's a bit ugly and hacky but if it works, it roughly reveals the problem, and can do something more elegant. I'd strongly suggest you consider using Arcade physics and Groups for your game however; these will make your life a lot easier, and allow your game to scale better. Link to comment Share on other sites More sharing options...
mon Posted July 25, 2014 Author Share Posted July 25, 2014 Thanks so much I'm changing to Arcade physics nowAgain, thanks for your support Link to comment Share on other sites More sharing options...
Recommended Posts