组合两个点并返回不起作用,但单独添加 X/Y 向量是吗?

发布于 2024-10-09 02:25:02 字数 612 浏览 5 评论 0原文

好奇为什么我的方法的行为与手动执行不同。

你好,我正在尝试在代码中计算 X/Y 向量(此处称为角度)。还不知道如何做静态方法,所以我在我的类中执行以下操作:

private var gp:Point = new Point(); //defined at top of file
private function combinept(p1:Point, p2:Point) :Point {
    gp.x = p1.x + p2.x;
    gp.y = p1.y + p2.y;
    return gp;
}

在我的移动方法中,当我调用时:

this.vel.x = this.vel.x + this.ep.x;
this.vel.y = this.vel.y + this.ep.y;

对象弹来弹去,当然有点疯狂:)

但是当我尝试时:

 this.vel = this.combinept(this.vel,this.ep);

相反,该物体在屏幕上不可见......就像它获得了一些疯狂的速度并飞走了。

你能告诉我为什么这些行为会有所不同吗?

Curious why my method is behaving differently than doing it manually.

Hi, I'm trying to calculate an X/Y vector (calling it angle here) in code. Don't know hot to do static methods yet, so I'm doing the following in my class:

private var gp:Point = new Point(); //defined at top of file
private function combinept(p1:Point, p2:Point) :Point {
    gp.x = p1.x + p2.x;
    gp.y = p1.y + p2.y;
    return gp;
}

In my movement method, when I call:

this.vel.x = this.vel.x + this.ep.x;
this.vel.y = this.vel.y + this.ep.y;

The object bounces around, a bit crazily of course :)

But When I try:

 this.vel = this.combinept(this.vel,this.ep);

Instead, the object isn't visible on screen.. like it got some wild velocity and flew off.

Can you tell me why these would behave differently?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的痛♀有谁懂 2024-10-16 02:25:02

我是否正确猜测 this.vel 不是一个 Point,而是一个 MovieClip 或 Sprite?那么不同之处在于,在第一个示例中,您添加了 x 和 y 值,它们是 DisplayObjects 和 Points 的成员,而在第二个示例中,您将返回的 Point 分配给 this.vel,从而断开与您的对象的连接。

试试这个:

private function addPoint(p1:Object, p2:Point) : void {
   p1.x += p2.x;
   p1.y += p2.y;
}

addPoint(this.vel, this.ep);

本例中的 p1 是对 this.vel 的引用,因此您不需要返回任何内容。

Am I correct in guessing that this.vel isn't a Point, but a MovieClip or Sprite? Then the difference would be that in the first example you are adding x and y values, which are members of both DisplayObjects and Points, while in the second example you are assigning the returned Point to this.vel, thus breaking the connection to your object.

Try this:

private function addPoint(p1:Object, p2:Point) : void {
   p1.x += p2.x;
   p1.y += p2.y;
}

addPoint(this.vel, this.ep);

p1 in this case is a reference to this.vel, so you don't need to return anything.

新人笑 2024-10-16 02:25:02

点已添加添加 方法。

var p3 : Point = p1.add(p2);

Point already has an add method.

var p3 : Point = p1.add(p2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文