henrikkee Posted July 19, 2015 Share Posted July 19, 2015 I'm having a problem in my game that i cant make my player jump when he is on an platform.Can anyone help me, here is the code (i dont know what part is wrong so i'll put the full code) Ps: I'm using phaser 2.3.0, and i dont speak english very well, so sorry if i write something wrong. game.physics.startSystem(Phaser.Physics.ARCADE); game.stage.backgroundColor = '#000000'; game.world.setBounds(0, 0, 4000, 400); campo = game.add.tileSprite(0, 0, 4000, 400, 'fundo'); cursors = game.input.keyboard.createCursorKeys(); jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); game.physics.arcade.gravity.y = 100; player = game.add.sprite(100,300 , 'boneco'); game.physics.arcade.enable(player) player.body.collideWorldBounds =true; player.animations.add('frente' , [0,1,2], 4); player.animations.add('tras' , [3,4,5], 4); player.animations.add('puloe' , [6]); player.animations.add('pulod' , [7]); player.animations.add('parado' , [8]); game.camera.follow(player); platforms = this.add.physicsGroup();platforms.create(50, 350, 'terra');platforms.create(200, 180, 'terra');platforms.create(400, 296, 'terra');platforms.create(600, 412, 'terra');platforms.setAll('body.allowGravity', false);platforms.setAll('body.immovable', true);platforms.setAll('body.velocity.x', 0)platforms.setAll('body.velocity.y', 0)platforms.setAll('body.collideWorldBounds', true); }, update: function () { player.body.bounce.y = 0.2; player.body.gravity.y = 200; player.body.velocity.x = 0; if (cursors.left.isDown) {player.body.velocity.x = -150; if (facing != 'left') { player.animations.play('tras'); facing = 'left'; }} else if (cursors.right.isDown) {player.body.velocity.x = 150; if (facing != 'right') { player.animations.play('frente'); facing = 'right'; } } else {if (facing != 'idle') {player.animations.play('parado'); facing = 'idle';}} if (jumpButton.isDown && player.body.onFloor() && game.time.now > jumpTimer) { player.body.velocity.y = -250; jumpTimer = game.time.now + 750;} if (jumpButton.isDown && player.body.onFloor() && game.time.now > jumpTimer) { player.body.velocity.y = -250; jumpTimer = game.time.now + 750;} game.physics.arcade.collide(platforms, player) Link to comment Share on other sites More sharing options...
CodeToWin Posted July 20, 2015 Share Posted July 20, 2015 my suspicion is that the player.body.onFloor() function is returning false. You can do a console.log to check its value. also, an administrative comment: you have the if statement for jumping twice Link to comment Share on other sites More sharing options...
CodeToWin Posted July 20, 2015 Share Posted July 20, 2015 also, you should consider why you put the collision function at the end of the update function. Try putting it at the beginning. Link to comment Share on other sites More sharing options...
frenetikm Posted July 20, 2015 Share Posted July 20, 2015 onFloor() does not work with sprites, but with tilemaps (or the world bounds). From the documentation : onFloor() → {boolean} Returns true if the bottom of this Body is in contact with either the world bounds or a tile. CodeToWin 1 Link to comment Share on other sites More sharing options...
henrikkee Posted July 20, 2015 Author Share Posted July 20, 2015 Thanks to everyone. OnFloor really does not work with sprites. I fixed it but now by player jumps into air. There's something i can do? (That isn't changing the sprite to a tilemap) play = function (game) { }; var facing = 'left';var jumpTimer = 0; play.prototype = { create: function () { game.physics.startSystem(Phaser.Physics.ARCADE); game.stage.backgroundColor = '#000000';game.world.setBounds(0, 0, 4000, 400);campo = game.add.tileSprite(0, 0, 4000, 400, 'fundo');cursors = game.input.keyboard.createCursorKeys(); jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);game.physics.arcade.gravity.y = 100; player = game.add.sprite(100,300 , 'boneco');game.physics.arcade.enable(player)player.body.collideWorldBounds =true;player.animations.add('frente' , [0,1,2], 4);player.animations.add('tras' , [3,4,5], 4);player.animations.add('puloe' , [6]);player.animations.add('pulod' , [7]);player.animations.add('parado' , [8]); game.camera.follow(player); platforms = this.add.physicsGroup(); platforms.create(50, 350, 'terra');platforms.create(200, 180, 'terra');platforms.create(400, 296, 'terra');platforms.create(600, 412, 'terra'); platforms.setAll('body.allowGravity', false);platforms.setAll('body.immovable', true);platforms.setAll('body.velocity.x', 0)platforms.setAll('body.velocity.y', 0)platforms.setAll('body.collideWorldBounds', true); },update: function () { game.physics.arcade.collide(platforms, player)player.body.bounce.y = 0.2;player.body.gravity.y = 200; player.body.velocity.x = 0; if (cursors.left.isDown) {player.body.velocity.x = -150; if (facing != 'left') { player.animations.play('tras'); facing = 'left'; }}else if (cursors.right.isDown){player.body.velocity.x = 150; if (facing != 'right') { player.animations.play('frente'); facing = 'right'; } } else{if (facing != 'idle') {player.animations.play('parado'); facing = 'idle';}} if (jumpButton.isDown && game.time.now > jumpTimer) { player.body.velocity.y = -250; jumpTimer = game.time.now + 750; console.log('funciona');} },}; I Try to change the jumpTimer (game.time.now + 750;) to a higher number, but the player delay to jump in the nearest platforms. Link to comment Share on other sites More sharing options...
AzraelTycka Posted July 21, 2015 Share Posted July 21, 2015 Hello, well your platforms are sprites, so why not use sprite.body.touching.down, it;s the sprite variant of onFloor() this way you check whether your body is standing on anything, if it is true then player can jump, if not then it can't jump? Link to comment Share on other sites More sharing options...
henrikkee Posted July 21, 2015 Author Share Posted July 21, 2015 Thanks it Worked Link to comment Share on other sites More sharing options...
Recommended Posts