Jump to content

Player not jumping in platforms


henrikkee
 Share

Recommended Posts

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

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

 Share

  • Recently Browsing   0 members

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