I create a game with falling objects. Items are generated automatically depends on the data from API.
I have goodBoxes that can be catched (score + 1) and badBoxes (score - 1). What I do is:
-
I create an object every 3s with this function
this.time.addEvent({ delay: 3000, callback: () => { this.itemDrop(); console.log("BAD", this.badBox.children); console.log("GOOD", this.goodBox.children); }, loop: true, });
-
Example of badBox creator
createBadFruits(x, y) { const optionsArray = this.gameData.entries[this.gameRound].answers; const badAnswers = optionsArray.filter((option) => !option.is_correct); const randomIdx = Math.floor(Math.random() * 3); const boxObj = this.physics.add .sprite(x, y, "box") .setOrigin(0.5, 0.5) .setScale(0.2) .setCollideWorldBounds(true); const boxText = this.add .text(x, y, badAnswers[randomIdx].value, darkFont) .setOrigin(0.5, 0.5); this.badBoxes.add(boxObj);
}
What I want to do is to bind text with the sprite obj so they can fall together.
I was checking the container solution:
const boxContainer = this.add.container(x, y, [boxObj, boxText]);
this.physics.world.enable(boxContainer);
boxContainer.body.setCollideWorldBounds(true);
this.badBoxes.add(boxContainer);
But then the text is falling slower then the box itself and my collapse callback function doesn’t work on collision.
detectBadBoxes(player, box) {
box.disableBody(true, true).refreshBody();
this.score = this.score - 1;
this.scoreText.setText(`x ${this.score}`);
}
Any idea what is wrong? Or how to attach text to the sprite so they can fall together an on collision both disappear?