Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
¿Cómo puedo cambiar la velocidad de un hijo de un grupo de física en Phaser.js?

Estoy haciendo un juego en Phaser que se ve así: ingrese la descripción de la imagen aquí

el jugador tiene que atrapar los huevos, por lo que los huevos (que están hechos de gameState.eggs = this.physics.add.group(); ) tienen cierta velocity mientras están en la rampa, pero luego, una vez que están fuera de la rampa, yo desea setVelocity() en uno con 0 para la coordenada x, en lugar de simplemente disparar a través de la pantalla.

Aquí está mi función de generación de huevos:

 function eggGen() { let num = Math.random(); let xCoord, yCoord, eggDirection, eggAnimation, velocityX if (num < .5) { xCoord = 100; eggDirection = 'eggLeft'; eggAnimation = 'rollingLeft' velocityX = this.velocityX; if (num < .25) { yCoord = 232; } else { yCoord = 382; } } else { xCoord = 700; eggDirection = 'eggRight'; eggAnimation = 'rollingRight'; velocityX = -(this.velocityX) if (num < .75) { yCoord = 232; } else { yCoord = 382; } } let egg = gameState.eggs.create(xCoord, yCoord, eggDirection).setVelocity(velocityX, this.velocityY).setScale(.6); if (egg.x > 220 && egg.x < 580) { egg.setVelocity(0, this.velocityY); } egg.anims.play(eggAnimation); }

el último condicional es lo que esperaba que hiciera la magia, pero no hace nada. Para aclarar, la función eggGen se llama dentro this.time.addEvent();

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Sin conocer su código (y suponiendo que se use la física de arcade) , yo:

Simplemente verifique en la función de update , de la escena, si un huevo está "en la rampa" y tiene una velocidad x de 0

 function update(){ // ... gameState.eggs.getChildren().forEach(egg => { if(egg.velocity.x > 0 && (egg.x > 220 || egg.x < 580)) { // ... stop velocity.x or set the whole velocity new egg.velocity.x = 0; } }); // ... }

Aquí una mini demostración:
Solo cubre lo básico

 document.body.style = 'margin:0;'; var config = { type: Phaser.AUTO, width: 300, height: 183, physics: { default: 'arcade', arcade: { debug: true, } }, scene: { create, update }, banner: false }; let objectGroup; function create () { objectGroup = this.physics.add.group(); this.time.addEvent({ delay: 500, callback: createObject, callbackScope: this, loop: true }); } function createObject(){ let spawnLeft = Phaser.Math.Between(0, 1); let obj = this.add.rectangle(spawnLeft ? 0 : config.width, 0, 10, 10, 0xff0000); this.physics.add.existing(obj); objectGroup.add(obj); obj.body.setVelocity((spawnLeft ? 1 : -1) * 75, 30); } function update(){ if(!objectGroup) return; objectGroup.getChildren().forEach(obj =>{ if(obj.body.velocity.x > 0 && (obj.x > 100 && obj.x < 150) || (obj.x > config.width - 150 && obj.x < config.width - 100) ){ // Just to keep the same speed, even after changing direction let speed = obj.body.velocity.length(); obj.body.velocity.x = 0; obj.body.velocity.y = speed; } }); } new Phaser.Game(config);
 <script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda