Jump to content

Object moves when hit


Cress
 Share

Recommended Posts

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

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

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

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

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

 Share

  • Recently Browsing   0 members

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