Hi,
I’ve been looking at the Phaser’s documentation and I’m trying to understand what is the difference between scene.physics.add.staticSprite and sprite.setImmovable(true) with Arcade physics. Can someone enlighten me ?
Thank you
Hi,
I’ve been looking at the Phaser’s documentation and I’m trying to understand what is the difference between scene.physics.add.staticSprite and sprite.setImmovable(true) with Arcade physics. Can someone enlighten me ?
Thank you
Static bodies are completely fixed in one position unless you explicitly refresh them. They have no velocity (or other properties related to movement). They will not move as a result of interactions with other bodies, nor will they follow their Game Object.
Meanwhile, an immovable dynamic body can freely move around, but collisions with other bodies will not alter its velocity. In the event of a collision with another body, the other body will receive the full force instead of the two bodies splitting it between themselves. See this example - the moving platform is immovable, so it doesn’t budge when the player touches it; if you remove the setImmovable
line, you’ll be able to push it around.
Dynamic bodies have a similar property called moves
. When set to false
, they ignore their velocity, which is more similar to a static body, with the exception that the body will still follow its Game Object. However, a body’s type (dynamic/static) cannot be changed without entirely re-creating the body, so moves
is useful for bodies that should only temporarily remain stationary or for bodies that only care about the movement of their underlying Game Object (keep in mind that relying on the movement of a body’s Game Object is fragile).
Thanks for the complete answer. It makes more sense now.
I have an other question, how can I create a static sprite in a custom class inheriting Sprite ? What is done through scene.physics.add.staticSprite(), how could I reproduce the same behavior in a custom Sprite class ?
Follow extending arcade sprite but use
scene.physics.add.existing(this, true); // true: static
Awesome thank you !
Thanks to your help, I’ve been able to create the static clickable sprite I was trying to achieve. I now have an other request for you that is still related to our current subject (should I create a new topic ?).
For some reasons I’m trying to create a static sprite which is clickable and invisible. I was thinking of inheriting the Sprite class and place the body and hit area to enable both physics and click. It works fine until I use setVisible(false) on the sprite. Then the click seems to be disabled. I think I have identified the involved line. See inputCandidate().
After searching for a while, I found an alternative solution by using a rectangle clickable Zone game object and a sprite. I’m wondering if it can be achieved with only one object. What do you think is the cleanest / simplest way if achieving this behavior ?
Thank you
This is by design, but you can use
sprite.input.alwaysEnabled = true;
Exactly what I needed, I didn’t even notice it in the line of code I linked…
Thanks all !