简体   繁体   English

Phaser将物理学应用于Phaser.Sprite(ES6)的子类

[英]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). 我正在尝试将Gravity.Y Arcade Physics应用于角色类(Phaser.Sprite)。 But gravity.y did not affect the Sprite and remained in its position. 但是重力没有影响Sprite并保持在原位。

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) 注意:我也尝试在game.add.existing(char)之前和之后启用/应用物理。

1) Failed 1)失败

An example of the extended Phaser.Sprite Character class, 扩展的Phaser.Sprite字符类的示例,

which has applied ARCADE physics, but it's not functioning: 已应用ARCADE物理,但无法正常运行:

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 2)工作

And then tried the normal Sprite creation and the gravity.y works... 然后尝试正常的Sprite创作和引力工作。

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" 该代码实际上在工作,这是由于另一个设置“ fixedToCamera = true”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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