Jump to content

Collision with player and object


hoskope
 Share

Recommended Posts

Hey,

 

I just started learning Phaser shortly and it seems i'm not able to get collision working with the player and object even there's a lot of tutorials about the subject.

 

All i want that player can jump top of the wooden box :)

game.png

 

What is the theory behind achieving something like this? Below you can see my messy game.js

 

FallingDown.Game = function (game) {};FallingDown.Game.prototype = {  preload: function () {    },  create: function () {    game.physics.startSystem(Phaser.Physics.ARCADE)    platforms = game.add.group()     platforms.enableBody = true        ground = platforms.create(0, game.world.height - 12, 'floor')    ground.scale.setTo(25 ,25)    ground.body.immovable = true     // Sprites and images    floor = game.add.tileSprite(0, 588, 800, 35, 'floor')    smallWoodenBox = game.add.sprite(232, game.world.height - 45, 'smallWoodenBox')        player = game.add.sprite(32, game.world.height - 150, 'dude-walking')    game.physics.arcade.enable(player)    player.body.gravity.y = 800    player.body.collideWorldBounds = true    player.animations.add('right', [0, 1, 2, 3], 10, true)    player.animations.add('left', [4, 5, 6, 7], 10, true)    game.physics.arcade.enable(smallWoodenBox)    smallWoodenBox.body.setSize(34, 34)    smallWoodenBox.body.immovable = true    cursors = game.input.keyboard.createCursorKeys()  },  update: function () {    game.physics.arcade.collide(player, platforms)    player.body.velocity.x = 0    if (cursors.left.isDown) {      player.body.velocity.x = -150      player.animations.play('left')    } else if (cursors.right.isDown) {      player.body.velocity.x = 150      player.animations.play('right')    } else {      player.animations.stop()    }    if (cursors.up.isDown && player.body.touching.down) {      player.body.velocity.y = -350      }    },    render: function () {      // For debugging      game.debug.body(smallWoodenBox)      // game.debug.body(sprite2)    }};

 

Link to comment
Share on other sites

So what is currently happening? Is the player colliding with the box at all, or just moving past it?

 

If you take a look in your update function, you will see 'game.physics.arcade.collide(player, platforms)' - this is specifying that you want the player sprite to collide with the platforms group.

 

However, you haven't specified that you would like the smallWoodenBox sprite to collide with the player, so try adding the following into your update function:

game.physics.arcade.collide(player, smallWoodenBox)
Link to comment
Share on other sites

Hey,

 

Currently player moving through the wooden box, so nothing happens. Your tip makes it collide with the box, that's great!.

But i need 'platforms' there as well, because of my floor.

 

If i have these three there:

game.physics.arcade.collide(player, platforms, smallWoodenBox)

I got error:

Uncaught TypeError: c.call is not a function

Cheers,

P

 

 

So what is currently happening? Is the player colliding with the box at all, or just moving past it?

 

If you take a look in your update function, you will see 'game.physics.arcade.collide(player, platforms)' - this is specifying that you want the player sprite to collide with the platforms group.

 

However, you haven't specified that you would like the smallWoodenBox sprite to collide with the player, so try adding the following into your update function:

game.physics.arcade.collide(player, smallWoodenBox)
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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