尝试移动小于 1 的 point.x / .y

发布于 2024-10-07 14:34:49 字数 1024 浏览 5 评论 0原文

我试图从将角度计算为向量的方法返回一个点。

然后我添加这个点 x & y 到我的玩家对象(来自示例代码)的 sx 和 y 数字 - 这些是数字,而不是点。在其他地方,我看到数字定义为浮点数,所以我没想到这会成为问题。

当我将 distvar 和 distvar2 乘以 * 2 时,我的对象会移动,我可以最好地判断,因为这些值(以前为 .59~ & ) -.8~,均大于1(绝对值)。

有什么提示吗?我应该在我的玩家对象上使用 Point 变量吗?我还必须做些什么才能从 point.x/y 转换为数字吗?刚接触 flash,感谢您的帮助。

谢谢!

public function GetAngle(initialp:Point,secondp:Point):Point{
    distvar = ((initialp.x - secondp.x) * (initialp.x - secondp.x));
    distvar2 = ((initialp.y - secondp.y) * (initialp.y - secondp.y));

 //Emergency guard clause against dividing by 0
    if (distvar + distvar2 == 0){
        distvar = 1;
        distvar2 = 1;
    }
    veldistance = Math.sqrt((distvar+distvar2));

distvar = ((Math.abs(initialp.x-secondp.x))/veldistance);
distvar2 = ((Math.abs(initialp.y-secondp.y))/veldistance);

if (secondp.x < initialp.x){
    distvar = -distvar;
}
if (secondp.y < initialp.y){
    distvar2 = -distvar2;
}

apoint.x = distvar;
apoint.y = distvar2;

return apoint;

I'm trying to return a point from a method that calculates angle as a vector.

I'm then adding this points x & y to my player object (from example code)'s x and y Numbers - these are Numbers, not a point. In other places I see numbers defined as floats, so I didn't expect this to be a problem.

When I multiply distvar and distvar2 by * 2, then my object move, best I can tell because the values, formerly .59~ & -.8~, are greater than 1 (absolute value).

Any hints? Should I just use a Point variable on my player object instead? Is there something else I must do to convert from a point.x/y to a Number? New to flash, appreciate your help.

Thanks!

public function GetAngle(initialp:Point,secondp:Point):Point{
    distvar = ((initialp.x - secondp.x) * (initialp.x - secondp.x));
    distvar2 = ((initialp.y - secondp.y) * (initialp.y - secondp.y));

 //Emergency guard clause against dividing by 0
    if (distvar + distvar2 == 0){
        distvar = 1;
        distvar2 = 1;
    }
    veldistance = Math.sqrt((distvar+distvar2));

distvar = ((Math.abs(initialp.x-secondp.x))/veldistance);
distvar2 = ((Math.abs(initialp.y-secondp.y))/veldistance);

if (secondp.x < initialp.x){
    distvar = -distvar;
}
if (secondp.y < initialp.y){
    distvar2 = -distvar2;
}

apoint.x = distvar;
apoint.y = distvar2;

return apoint;

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

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

发布评论

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

评论(1

执笔绘流年 2024-10-14 14:34:49

point.x/y 的值是数字。将 point.x/y 添加到 object.x/y 的方式应该没问题。我怀疑代码还有其他问题,尽管没有更多代码我不确定是什么。首先,我看不到apoint是在哪里创建的。

The values of a point.x/y are numbers. The way you are adding the point.x/y to your object.x/y should be fine. I suspect there is something else wrong with the code, though without more of your code I'm not sure what. For one, I can't see where apoint is created.

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