hoskope Posted January 2, 2016 Share Posted January 2, 2016 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 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 More sharing options...
heisenthurg Posted January 2, 2016 Share Posted January 2, 2016 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 More sharing options...
hoskope Posted January 2, 2016 Author Share Posted January 2, 2016 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 functionCheers,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 More sharing options...
dr.au Posted January 3, 2016 Share Posted January 3, 2016 I believe you have to call them separately:game.physics.arcade.collide(player, platforms);game.physics.arcade.collide(player, smallWoodenBox); hoskope 1 Link to comment Share on other sites More sharing options...
hoskope Posted January 3, 2016 Author Share Posted January 3, 2016 Hey, Thanks a lot, it worked. Never came to mind I believe you have to call them separately:game.physics.arcade.collide(player, platforms);game.physics.arcade.collide(player, smallWoodenBox); Link to comment Share on other sites More sharing options...
Recommended Posts