简体   繁体   中英

Phaser applying physics to child class of Phaser.Sprite (ES6)

I'm trying to apply Gravity.Y Arcade Physics to a Character Class(Phaser.Sprite). But gravity.y did not affect the Sprite and remained in its position.

Is there any specific order to apply the physics? NOTE: I have also tried to enable/apply physics before and after game.add.existing(char)

1) Failed

An example of the extended Phaser.Sprite Character class,

which has applied ARCADE physics, but it's not functioning:

class Char extend Phaser.Sprite {
     constructor(game,x,y){
        super(game,x,y,'dude')
        game.add.existing(this)
        game.physics.arcade.enable(this)
        this.body.gravity.y = 300
     }
}

// calling it in create()
create(game){
   game.physics.startSystem(Phaser.Physics.ARCADE)

   var x = new Char(game,0,0)

}

2) Working

And then tried the normal Sprite creation and the gravity.y works...

create(game){
   game.physics.startSystem(Phaser.Physics.ARCADE)

   var x = game.add.sprite(0,0,'dude')
   game.physics.arcade.enable(x)
   x.body.gravity.y=300
}

My Bad

The code is actually working, it was due to another setting "fixedToCamera = true"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM