组合两个点并返回不起作用,但单独添加 X/Y 向量是吗?
好奇为什么我的方法的行为与手动执行不同。
你好,我正在尝试在代码中计算 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我是否正确猜测
this.vel
不是一个 Point,而是一个 MovieClip 或 Sprite?那么不同之处在于,在第一个示例中,您添加了 x 和 y 值,它们是 DisplayObjects 和 Points 的成员,而在第二个示例中,您将返回的 Point 分配给this.vel
,从而断开与您的对象的连接。试试这个:
本例中的 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 tothis.vel
, thus breaking the connection to your object.Try this:
p1 in this case is a reference to
this.vel
, so you don't need to return anything.点已添加添加 方法。
Point already has an add method.