Vaunt
November 14, 2020, 6:04pm
1
Hello, i need help with the collider, i have a player and a group of enemies, the collider work like this :
this.physics.add.collider(this.player, this.enemies)
But when i collide with the enemies, the enemies are moved
I have try this on enemy :
this.enemies = this.physics.add.group()
const enemy = self.physics.add.sprite(enemyInfo.x, enemyInfo.y, playerInfo.tank_choice).setCollideWorldBounds(true).setBounce(0)
enemy.body.immovable = true
enemy.setImmovable(true)
this.enemies.add(enemy)
but nothing of this work
need help
Hi @Vaunt ,
When you add a new element to the physics group, a new physics body is created, so you have change the physics properties after add it. One way would be:
this.enemies.create(enemyInfo.x, enemyInfo.y, playerInfo.tank_choice)
.setCollideWorldBounds(true)
.setBounce(0)
.setImmovable(true);
Greetings.
Vaunt
November 14, 2020, 7:22pm
3
Hi @jjcapellan sorry but dont work.
There is no longer even a collision
Ok. Here the code is working, maybe you need to change something:
Good luck!!
Vaunt
November 15, 2020, 12:39am
6
When i put setImmovable(true)
to the children the collider not working, There is no longer even a collision, but i can log an overlap…(in the collider callback) and in the body of the enemy i can see immovable: true
And with your method, how can i see the childrens ? I dont see they in the enemies object (children).
this is my code :
addPlayer Function :
this.ship = self.physics.add.sprite(playerInfo.x, playerInfo.y, playerInfo.tank_choice).setImmovable(true).setCollideWorldBounds(true)
…
this.physics.add.collider(this.ship, this.lockLayer) // this work
this.physics.add.collider(this.ship, this.enemies, (player, enemy) => {
console.log(player.body.immovable) // return true
console.log(enemy.body.immovable) // return true
})
addEnemies Function
const enemy = this.enemies.create(enemyInfo.x, enemyInfo.y, enemyInfo.tank_choice).setImmovable(true)
enemy.enemyId = enemyInfo.enemyId
however, i launch the addEnemies function and addPlayer function if i have a player connected with socket.io
const vm = this
this.socket.on('newEnemy', function (enemyInfo) {
vm.addEnemies(self, enemyInfo)
})
To get the array of group elements, they are two alternatives:
let enemyArray = this.enemies.children.entries;
// or
let enemyArray = this.enemies.getChildren();
So, after that, you can add new properties:
enemyArray[index].id = 'myId';
You also can create the group from a config object:
this.enemies = this.physics.add.group({
immovable: true;
});
// and then
this.enemies.create(enemyInfo.x, enemyInfo.y, playerInfo.tank_choice)
// ....
Vaunt
November 15, 2020, 10:47am
8
Thx bro this work,
and i have removed setImmovable(true) from my player, because with this, my player passed through enemies.