Soy nuevo en Phaser Framework y quería intentar hacer un prototipo de juego de billar 2D desde una perspectiva de arriba hacia abajo. El problema que tengo ahora mismo es detectar si todas las bolas han dejado de moverse antes de reiniciar.
Uso Physics.Matter y aquí está el código fuente cuando lo create hasta ahora:
this.matter.world.setBounds(0, 0, 720, 1280, 32, false, false, false, true); this.add.image(400, 300, 'sky'); var ball = this.matter.add.image(360, 1000, 'ball'); ball.setCircle(); ball.setVelocity(-5, -20); ball.setBounce(0.5); for (var i = 1; i < 10; i++) { var target = this.matter.add.image(Phaser.Math.Between(400,450), Phaser.Math.Between(400,450), 'target'); target.setCircle(); target.setVelocity(0, 0); target.setBounce(0.7); target.setFriction(0, 0.01); target.setSleepEvents(true, true); } this.matter.world.on('sleepstart', function() {console.log('sleepstart');}); this.matter.world.on('sleepend', function() {console.log('sleepend');}); Esto detectaría si cada target se ha dormido, pero necesito detectar si TODOS dejaron de moverse. No puedo contar cuántos han dormido porque a veces, cuando un target ha entrado en estado de suspensión, existe la posibilidad de que otro cuerpo rebote y lo despierte de nuevo.
¿Hay alguna forma de detectarlos globalmente?
EDITAR: como plan alternativo, agrego una función JS básica para llamar cada vez que se llama a la update y contar los cuerpos durmientes, lo que parece que no debería ser una forma adecuada:
var isActive = false; // Some commands here that changes isActive = true function onupdate() { if (isActive) { var bodyCount = this.matter.world.getAllBodies().filter(o => o.isSleeping === true).length; console.log(bodyCount); if (bodyCount >= 11) { isActive = false; } } }Pondría todos los objetos que desea rastrear en un grupo Phaser ( https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.Group.html ) e iteraría sobre los elementos del grupo, para ver si todos tienen la propiedad isSleeping establecida en true ;
Advertencia: no puedo decir cuán eficaz es esta solución, es un caso de uso. Si es demasiado lento, configuraría una variable de contador y contaría hacia atrás/hacia arriba en
sleepstartysleepend. Y cuando el contador es0todos están durmiendo.
Aquí una demostración de trabajo, cómo lo haría:
(explicación están en el código, como comentarios)
// fix to prevent 'Warnings' in stackoverflow console console.warn = _ => _ var config = { type: Phaser.AUTO, width: 400, height: 100, scene: { create }, physics: { default: 'matter', matter: { debug: true, setBounds: { x: 0, y: 0, width: 400, height: 100 }, enableSleeping: true } } }; function create(){ // create the Phaser Group this.targets = this.add.group(); for (var i = 1; i < 10; i++) { var target = this.matter.add.image(200, 0, 10, 10, 'target'); target.setCircle(); target.setBounce(0.7); target.setFriction(0, 0.01); target.setSleepEvents(true, true); // Add Item to the Group this.targets.add(target); } this.matter.world.on('sleepstart', function(event, item){ // Check all targets are sleeping if(!this.targets.getChildren().some( target => !target.body.isSleeping)){ console.log('all are sleeping'); } }, this); // <- pass the scene as context this.matter.world.on('sleepend', function() {console.log('sleepend');}); } var game = new Phaser.Game(config); <script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>