加农炮火力相位器3

Cannon Fire Phaser3

提问人:yunusemre agdin 提问时间:10/31/2023 更新时间:10/31/2023 访问量:27

问:

我想为我在 2D phaser3 中开发的游戏制作炮火。我还没有实现这一举动。 当我以这种方式编码时,它会在某个点后直接急剧下降。我怎样才能在这里获得炮火射击。

import {
  ActorProps,
  ICollectable,
  IDieable,
  IMoveable,
} from "@games/common/interfaces";
import { Actor } from "@games/common/objects";

export default class Cannon
  extends Actor
  implements IMoveable, IDieable, ICollectable
{
  declare body: Phaser.Physics.Arcade.Body;

  private initialX: number;
  private initialY: number;
  private velocityX: number;
  private velocityY: number;
  private isMoving: boolean;
  private isMovingUp: boolean;

  constructor(props: ActorProps) {
    super(props);
    this.scene.physics.add.existing(this);
    this.body.setAllowGravity(false);

    this.initialX = this.x;
    this.initialY = this.y;
    this.velocityX = 100;
    this.velocityY = -100;
    this.isMoving = true;
    this.isMovingUp = true;
  }

  update(time: number, delta: number): void {
    if (this.isMoving) {
      this.x -= this.velocityX * (delta / 1000);
      this.y += this.velocityY * (delta / 2000) * 0.5;

      if (this.x >= this.initialX + 200 || this.y <= this.initialY - 200) {
        this.velocityY = -this.velocityY;
      }

      if (this.velocityY < 0 && this.y >= this.initialY) {
        this.isMoving = false;
        this.y = this.initialY;
      }
    }
  }

  die(): void {}

  move(): void {}

  collect(...props: any[]): void {}
}

目前,物体从起始位置向上和向左加速,但我无法获得我想要的结果,因为它根据固定值移动。

我在 Unity 中制作了这段代码,它可以正常工作。我使用了抛物线公式。

public float a=-1, b=0, c=0;
    
    public float x=-5;
    [Range(0,10)]public float speed;
    // Update is called once per frame
    void Update()
    {
        Vector3 newPos = new Vector3(x,axx+bx+c,0);
        transform.position = newPos;
        x += Time.deltaTime speed;
    }
javascript typescript 游戏-物理 phaser-framework

评论

0赞 winner_joiner 11/5/2023
我想知道我的答案是否解决了您的问题,如果是这样,请考虑接受投票箭头下方的绿色复选标记。

答:

1赞 winner_joiner 10/31/2023 #1

主要问题是,您没有使用 phyiscs 引擎。
不应该自己设置和属性,你应该让引擎为你做这件事。使用函数(链接到文档),或使用(链接到文档)设置加速。
xysetVelocitysetAcceleration

但是要使用这些函数,你必须重写一些代码。

查看这些官方示例/演示基本平台游戏迷你宇宙飞船acrade 物理示例集合),了解如何在 phaser 中使用街机引擎的详细信息。