Steel
September 8, 2020, 4:41am
1
I have a sprite called player and a physics group called obs, both are rectangular sprites so nothing complex. But the collision is only detected if player touches top part of obs. If I try to do it sideways it’s passes straight through obs.
obs = this.physics.add.group({
defaultKey : 'obs',
maxSize : 100,
velocityY : -300});
A timer calls the below function at 2sec intervals to create obstacles.
function createObs(){
obsChild = obs.get(Phaser.Math.Between(0,500),900);
obsChild.setScale(4).refreshBody;
}
I have a collision setup between player sprite and obs group. Please help.
Edit :
Timer code.
var timer = this.time.addEvent({
delay: 2000,
callback: createObs.bind(this),
loop: true });
Collision.
this.physics.add.collider(player,obs);
BlunT76
September 8, 2020, 7:29am
2
Hi,
Show us your timer and collision code please.
obsChild.setScale(4).refreshBody()
// i suppose refreshBody is a function no?
Steel
September 8, 2020, 8:27am
3
I have added the timer and collision code on my post.
Yes you are right, refreshBody is a function when I corrected the code to :
obsChild.setScale(4).refreshBody();
It gave me error.
Uncaught TypeError: this.body.updateFromGameObject is not a function
So I guess refreshBody wasn’t doing anything in the code before.
BlunT76
September 8, 2020, 9:09am
4
There is probably something wrong in your code elsewhere.
I tried your code in codepen, and it works.
// console.clear();
document.getElementById("version").textContent = "Phaser v" + Phaser.VERSION;
var config = {
type: Phaser.AUTO,
width: 400,
height: 256,
loader: {
baseURL: "https://labs.phaser.io",
crossOrigin: "anonymous"
},
physics: {
default: 'arcade',
arcade: {
tileBias: 20,
gravity: {
y: 0
},
debug: true,
debugShowBody: true,
debugShowStaticBody: true,
},
},
scene: {
preload: preload,
create: create,
update: update
},
};
var game = new Phaser.Game(config);
var cursors;
var player;
var obs;
function preload() {
this.load.image('player', 'assets/sprites/brain.png');
this.load.image('obs', 'assets/demoscene/ball-tlb.png');
}
function create() {
cursors = this.input.keyboard.createCursorKeys();
player = this.add.sprite(200, 128, "player")
.setVisible(true)
.setOrigin(0.5, 0.5).setDisplaySize(20, 20);
this.physics.world.enable(player);
player.body.setSize(120, 120);
player.body.setCollideWorldBounds(true);
obs = this.physics.add.group({
defaultKey : 'obs',
maxSize : 100,
velocityY : -300});
var timer = this.time.addEvent({
delay: 2000,
callback: createObs.bind(this),
loop: true
});
this.physics.add.collider(player, obs, ()=>console.log('collide'), null, this);
}
function createObs(){
obsChild = obs.get(Phaser.Math.Between(0,400),256);
obsChild.setScale(4);
}
function update() {
player.body.setVelocity(0);
if (cursors.left.isDown)
{
player.body.setVelocityX(-300);
}
else if (cursors.right.isDown)
{
player.body.setVelocityX(300);
}
if (cursors.up.isDown)
{
player.body.setVelocityY(-300);
}
else if (cursors.down.isDown)
{
player.body.setVelocityY(300);
}
}