Cress Posted June 14, 2015 Share Posted June 14, 2015 Hi everybody, I'm making a sidescroller game and when the player hits a spike it dies and the game starts over. This is fine but the spike starts moving to the right if it's hit from the left and it starts moving downward if it's hit from above. I don't know why this happens! The spike moves if hit even without the playerDie function. This is the line for collision in the update function:this.game.physics.arcade.collide(this.player, this.spikes, this.playerDie, null, this);spikes is a group of all spikes. This is the playerDie function: playerDie: function(player, blockedLayer) { this.player.alive = false; this.player.body.velocity.x = 0; this.player.loadTexture('playerDead'); this.game.time.events.add(1500, this.gameOver, this); }And this is the gameOver function: gameOver: function() { this.game.state.start('Game'); }Any hints? Link to comment Share on other sites More sharing options...
marein Posted June 14, 2015 Share Posted June 14, 2015 Hello Cress, you need to set the spike objects immovable. I do this in the constructor. An example (note: I inject a state, not the entire game): GameObject.Brick = function (state, x, y) { this.state = state; var game = state.game; Phaser.Sprite.call(this, game, x, y, 'texture'); this.frameName = 'some_image_name.png'; state.physics.enable(this, Phaser.Physics.ARCADE); // this line is for you this.body.immovable = true;}; I hope this helps. Link to comment Share on other sites More sharing options...
Tilde Posted June 14, 2015 Share Posted June 14, 2015 I actually remember this from the official Phaser tutorial! It's a product of the Arcade physics system that's on by default. You wanna do this: spikes.body.immovable = true; Link to comment Share on other sites More sharing options...
substandardgaussian Posted June 14, 2015 Share Posted June 14, 2015 By default, collision events in Arcade will cause motion. For your spike sprites, you should set "body.immovable" to true. They won't receive impacts from collisions but will still cause collisions to occur and callbacks to be called. Link to comment Share on other sites More sharing options...
Tilde Posted June 14, 2015 Share Posted June 14, 2015 Of course, my previous post didn't go through before the question got answered by someone else since the mods are asleep... Link to comment Share on other sites More sharing options...
Cress Posted June 15, 2015 Author Share Posted June 15, 2015 Thanks guys but I get the error this.spikes.body is undefined even if enableBody is set to true Link to comment Share on other sites More sharing options...
marein Posted June 15, 2015 Share Posted June 15, 2015 Hey, you need to set this on each spike object. My previous code is an object oriented way to do that. Maybe you don't use the oop way to achieve that. Can you paste your code that create the spikes? I'm looking forward to extend it. Or maybe this example fit your needs https://phaser.io/examples/v2/arcade-physics/sprite-vs-group. Take a look between line 27 and 37. Link to comment Share on other sites More sharing options...
Cress Posted June 15, 2015 Author Share Posted June 15, 2015 This is the code : createSpikes: function() { this.spikes = this.game.add.group(); this.spikes.enableBody = true; var result = this.findObjectsByType('spike', this.map, 'objectsLayer'); result.forEach(function(element){ this.createFromTiledObject(element, this.spikes); }, this); },I haven't understand your code very well anyway, can you explain it? Link to comment Share on other sites More sharing options...
marein Posted June 15, 2015 Share Posted June 15, 2015 If I understand your code right, you want to check if the player overlap with a spike. You don't want to move the player and the spike physically. If so, replace this:this.game.physics.arcade.collide(this.player, this.spikes, this.playerDie, null, this);With this:this.game.physics.arcade.overlap(this.player, this.spikes, this.playerDie, null, this);This should do the trick. But if you want a bouncing player, please show me the createFromTiledObject function. EDIT:You asked for an explanation for my code. In my games I create a class for each object. There is an example on the phaser site https://phaser.io/examples/v2/sprites/extending-sprite-demo-1. Link to comment Share on other sites More sharing options...
Cress Posted June 15, 2015 Author Share Posted June 15, 2015 Yes, the overlapping solved everything! Link to comment Share on other sites More sharing options...
Recommended Posts