Jump to content

Why couldn't sprite jump when it touches the ground?


stone
 Share

Recommended Posts

 After the gravity is added to sprite, it can jump before it falls to the ground, but it doesn't react to the click after falling to the ground. 


var game = new Phaser.Game(500, 600, Phaser.AUTO, 'game_div');
var main_state = {

preload: function() {
game.load.image('hello', 'assets/hello.png');
},

create: function() {
this.hello = this.game.add.sprite(20, 20, 'hello');
this.hello.body.collideWorldBounds = true;
this.hello.body.gravity.y = 1000;
this.hello.width = this.hello.height = 50;
this.input.onDown.add(this.jump, this);
},

update: function() {

},
jump: function(){
this.hello.body.velocity.y = -300;
}
};

game.state.add('main', main_state);
game.state.start('main');
Link to comment
Share on other sites

Besides that this code does not work, i added what it was missing and tried it. It jumps correctly.

I'm guessing that you didn't give us the exact code or you are using a different edition of phaser.

What edition do you use?

var game = new Phaser.Game(500, 600, Phaser.AUTO, 'game_div');    var main_state = {         preload: function() {            game.load.image('hello', 'assets/hello.png');        },         create: function() {           this.hello = this.game.add.sprite(20, 20, 'hello');          //added this               game.physics.arcade.enable(this.hello);          this.hello.body.collideWorldBounds = true;          this.hello.body.gravity.y = 1000;          this.hello.width = this.hello.height = 50;          this.input.onDown.add(this.jump, this);        },         update: function() {                    },        jump: function(){          this.hello.body.velocity.y = -300;        }    };     game.state.add('main', main_state);      game.state.start('main');  
Link to comment
Share on other sites

My version of phaser is 1.1.5, which has been changed into 2.0.5 and added with game.physics.arcade.enable(this.hello). And then everything is OK. 

Thank you.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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