我想为我在 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 {}
}
当前物体正在从起始位置向上并向左加速,但我无法得到我想要的结果,因为它按照固定值移动。
我统一编写了这段代码,它工作正常。我用的是抛物线公式。
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;
}
主要问题是,您没有使用 phyiscs 引擎。
你不应该自己设置 x
和
y
属性,你应该让引擎为你做这件事。使用功能 setVelocity
(链接到文档),或使用
setAcceleration
设置加速度(链接到文档)。 但是要使用这些函数,您必须重写一些代码。
查看这些官方示例/演示
(