In my work in progress demo.
The enemy and the player are both created with this.physics.add.sprite
When the enemy collide with the player, the walking animation keeps playing and both sprites stop at the position they are. But if I control the player to move forward it will push the enemy sprite to that direction.
Is there anyway to stop this pushing behavior in arcade physics?
if sprite name does not start with this.
nameOfSprite.body.setImmovable(true);
if the sprite name starts with this
this.nameOfSprite.body.setImmovable(true);
Hello @ohio .
Thanks for the response.
I looking into the setImmovable
function as you mentioned and call it when the enemy sprite is collided with the player sprite.
The result is, the enemy sprite won’t be push by the player but the player can be push by the enemy if it is moving towards the player.
If I set both the enemy and the player sprite with setImmovable(true)
, they both will overlap with each other instead.
So my problem is more like how to stop both sprite pushing to one another without overlap?
And for further more. How to stop the sprite that is being pushed to go into the wall?
you can do .setPushable() setPushable api
if you want the player or the enemy to be pushed by something else you can set pushable to true only when the player and enemy are colliding. If you dont understand setPushable just ask chatgpt to give you an example of useing setPushable in phaser. (I am curious what kind of game you are making? Could you please put it in showcase when you are done?)
Glad to hear from you.
The game I’m making is in concept mostly. Something like a rougelike game. The dungeon is formed with 3X3 grid. Each grid is a room and you start in random one each time the game start.
So far, I’ve achieved to visited all the rooms and can go back and forth. And is working on the path finding but the sprite pushing behavior keeps bugging me.
I’ve prepared a codeSandbox to help better understand the problem.
The code is quite a mess. To makes things clear, the enemy have a private function called #onCollide as the callBack to fired when collide. That’s where I use setImmovable
.
Please use arrow keys to move around the dungeon. F key to open the door and D key to attack (only the animation and the damage calculation is done at this point. No further events.)
@ohio
As far as I understand from this example.
The setPushable()
can only be apply to the object that I don’t want to be pushed. Since I set both the player and the enemy with setPushable(false)
the push will happen for there is velocity to be split.
I ended up setting a property called collide on the enemy, change it to true if collided with player and check the property before the velocity value changes. Which kinda work as I thought. But I want to know if I misunderstood setPushable()
or not.
why can it only be applied to the object that you do not want to be pushed? That seems wierd, I guess you could try using .body.setImmovable(true) on the player and setPushable(false) on the enemy.
I think setPushable(false)
only works when the other one is not moving against each other. When the two object collide and both have setPushable(false)
. The one with smaller velocity will get pushed.
If I use setImmovable(true)
on the player and setPushable(false)
on the enemy. Now the player can push the enemy around.
The behavior can be tested if we tweak this example a bit.
At this point. I guess using the custom property to restrict the enemy is the way to do when both sprite are moving. Still, I wish there’s a build-in function to do this.
In the end.
I checked up, right, down, left
properties in sprite.body.touching
to determine if this sprite is collided or not when the player is moving.
If the property of the direction is true. Then I won’t fire the sprite.body.setVelocity
to move the sprite. With that, the sprite is going to stay at where it is.