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

91
Views
Cómo leer la posición x del objeto de forma durante la interpolación

Tengo el objeto Circle que extiende la forma y estoy tratando de obtener el valor de x o la posición del objeto en el plano 2d. Durante la animación (interpolación), la posición no se actualiza y permanece estática. Cómo puedo solucionar esto ?

 export class Circle extends Phaser.GameObjects.Shape { tween; constructor(scene:Phaser.Scene, x, y) { super(scene); let enemy = scene.add.circle(x, y, 20); enemy.setStrokeStyle(1, 0x05F9FB); this.tween = scene.tweens.add({ targets: enemy, x: 560, y: 200, ease: 'Power1', duration: 3000, yoyo: true, repeat: -1 }); } }

Tengo el siguiente método en el método de actualización y siempre devuelve el valor x original, no el de actualización durante la interpolación.

 update() { //Always returns the orginal position of x and not the updated or current state of x console.log(enemy.x); }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

La posición enemy x e y no es la posición del circle , que se usa en la interpolación. Una opción es crear una propiedad, para que pueda acceder a la posición, desde la propiedad.

Aquí un pequeño ejemplo, cómo podría funcionar el acceso:

 // Minor formating for stackoverflow document.body.style = "display: flex;flex-direction: column;"; class Circle extends Phaser.GameObjects.Shape { constructor(scene, x, y) { super(scene, x, y); this.circle = scene.add.circle(x, y, 20); this.circle.setStrokeStyle(3, 0x05F9FB); this.tween = scene.tweens.add({ targets: this.circle, x: 400, y: 120, duration: 3000, yoyo: true, repeat: 3 }); } } var config = { type: Phaser.AUTO, width: 536, height: 163, scene: { create, update } }; function update(){ this.info.setText(`x: ${this.enemy.circle.x.toFixed(0)}\ny: ${this.enemy.circle.y.toFixed(0)} `); } function create () { this.enemy = new Circle(this, 100, 100); this.info = this.add.text(20,20, '', 0xffffff); } var game = new Phaser.Game(config);
 <script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"> </script>

Actualizar:

Información: para mantener el código más limpio, usar un getter (y setter) o un método podría ser más limpio

Como esto

 class Circle extends Phaser.GameObjects.Shape { ... getPosition(){ let { x, y } = this.circle; return { x, y }; } }
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!