I’ve never been able to get this to work as expected. I have a static group for the floor, and one or more physics objects as player, enemies, whatever.
When one physics body lands on another, it pushes the other one through the floor. I’ve tried setting pushable to false and even cancelling out the incoming velocity, but nothing works. What is the best way to prevent this?
https://jsfiddle.net/paynterc/gsw48zqk/8/
var config = {
type: Phaser.AUTO,
width: 800,
height: 800,
parent: 'test',
scene: {
preload: preload,
create: create,
update: update
},
physics: {
default: 'arcade',
arcade: {
gravity: { y: 2600 },
debug: false
}
},
};
var game = new Phaser.Game(config);
var obj1, obj2,platforms;
function preload ()
{
this.load.image('dude', 'https://i.postimg.cc/tCMhjzhF/Snowman.png');
this.load.image('block', 'https://i.postimg.cc/TPcRgfJG/Block.png');
this.load.image('snowball', 'https://i.postimg.cc/rm1zCjBR/snowball.png');
}
function create ()
{
platforms = this.physics.add.staticGroup();
platforms.create(400, 400, 'block');
obj1 = this.physics.add.sprite(400, 20, 'dude');
obj1.body.pushable=false;
obj1.setBounce(0.2);
obj2 = this.physics.add.sprite(400, 350, 'snowball');
obj2.body.pushable=false;
obj2.setBounce(0.2);
this.physics.add.collider(obj1, platforms);
this.physics.add.collider(obj2, platforms);
this.physics.add.collider(obj1, obj2);
}
function update ()
{
if(obj2.y>500){
obj2.body.velocity.y=0;
obj2.y=20;
}
if(obj1.y>500){
obj1.body.velocity.y=0;
obj1.y=20;
}
}