简体   繁体   中英

How can I move a sprite with a player while using arcade physics with gravity?

I want to have a player that runs around with a gun. Seems like that shouldn't be too hard, but it keeps getting more and more complicated. I thought of using a container, but containers don't allow gravity, so I can't use that. Then I tried using a physics group, which kind of worked. There is a big issue with the physics group though. I can only have gravity on or off for ALL objects in the group which isn't what I want. Obviously I want the player to have gravity and jump around, but the gun shouldn't fall through the world. The player should have gravity, the gun shouldn't. In fact I'm pretty sure the gun doesn't need physics at all. Is there a way to disable gravity for just one object in a physics group? Or is there a better approach to my issue altogether where you don't use a group?

I would:

  1. create a custom based on Phaser Sprite / GameObject / Container (depending on the properties needed) .

  2. In that custom Object, I would add the needed extra GameObject(s), to the scene, in the constructor .

  3. Update the position(s) of those extra object(s) in the custom object itself, so they would not need a own physics body.

So it turned out that I'm actually just dumb an containers DO have physics bodies. It didn't work when I tried it, but that was because of something else I did wrong, and not the physics body. It works now if I use a container to move everything together.

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