Cress

Object moves when hit

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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.