Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
¿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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!