AS3:根据其旋转移动随机生成的对象

发布于 2024-12-11 17:17:37 字数 1638 浏览 1 评论 0原文

制作一款船舶游戏是因为我非常具有原创性。除此之外,我还有一个问题。我有一个函数可以根据船的旋转来发射子弹。它在创建时使用了以下代码:

        var b = new Bullet  ;
        b.x = x;
        b.y = y;
        b.rotation = rotation;
        parent.addChild(b);
        bullets.push(b);
        score -=  50;
        trace(enemybullets[30].x + "," + enemybullets[30].y);

上面的代码位于我的 Ship 类中,因此我可以轻松地使子弹实现正确的旋转。 并不断更新其在子弹类别中的位置:

        x += Math.cos(rotation / 180 * Math.PI) * speed;
        y += Math.sin(rotation / 180 * Math.PI) * speed;

以便一切顺利。但我有另一个类,EnemyBullet,它随机生成并使用类似的代码来设置其方向和运动。在我的飞船类中:

        var eb = new EnemyBullet ;
        eb.x = (Math.random() * 550) - 550;
        //trace("eb.x is " + eb.x)
        eb.y = (Math.random() * 400) - 400;
        //trace("eb.y is " + eb.y);
        var a1 = eb.y - y;
        var b1 = eb.x - x;
        var radians1 = Math.atan2(a1,b1);
        var degrees1 = radians1 / Math.PI / 180;
        eb.rotation = degrees1;
        if (enemybullets.length < 50)
        {
            parent.addChild(eb);
            enemybullets.push(eb);
        }

在 EnemyBullet 类中:

        x += Math.cos(rotation / 180 * Math.PI) * speed;
        y += Math.sin(rotation / 180 * Math.PI) * speed;

我设置了一个跟踪来跟踪我的一颗子弹的去向,因为它们肯定不会出现在我的屏幕上.. 以下是我的跟踪结果:

x: 121.55, y:-162.05
x: 1197.05, y:-162.05
x: 1842.35, y:-162.05
x: 2368.15, y:-162.05
x: 2547.4, y:-162.05
x: 2702.75, y:-162.05
x: 2882, y:-162.05

我收集到因此旋转总是水平的,但我怎么也看不出为什么?谁能给我答案吗?假设它足够简单,因为我用来设置旋转的代码与我用来将影片剪辑转向鼠标的工作代码相同。

有什么想法吗?

塔!

Making a ship game because I am incredibly original.. With that aside, I have a problem. I have a function to fire bullets from my ship based on its rotation which works.. it uses this code on creation:

        var b = new Bullet  ;
        b.x = x;
        b.y = y;
        b.rotation = rotation;
        parent.addChild(b);
        bullets.push(b);
        score -=  50;
        trace(enemybullets[30].x + "," + enemybullets[30].y);

The above code is in my Ship class so I can easily make the bullets achieve the correct rotation.
And to continually update its position, in the bullet's class:

        x += Math.cos(rotation / 180 * Math.PI) * speed;
        y += Math.sin(rotation / 180 * Math.PI) * speed;

So that all works well. But I have another class, EnemyBullet, which randomly generates and uses similar code to set its direction and movement. In my ship class:

        var eb = new EnemyBullet ;
        eb.x = (Math.random() * 550) - 550;
        //trace("eb.x is " + eb.x)
        eb.y = (Math.random() * 400) - 400;
        //trace("eb.y is " + eb.y);
        var a1 = eb.y - y;
        var b1 = eb.x - x;
        var radians1 = Math.atan2(a1,b1);
        var degrees1 = radians1 / Math.PI / 180;
        eb.rotation = degrees1;
        if (enemybullets.length < 50)
        {
            parent.addChild(eb);
            enemybullets.push(eb);
        }

And in the EnemyBullet class:

        x += Math.cos(rotation / 180 * Math.PI) * speed;
        y += Math.sin(rotation / 180 * Math.PI) * speed;

I have set up a trace to track where one of my bullets going, because they certainly aren't appearing on my screen.. here are the results from my tracing:

x: 121.55, y:-162.05
x: 1197.05, y:-162.05
x: 1842.35, y:-162.05
x: 2368.15, y:-162.05
x: 2547.4, y:-162.05
x: 2702.75, y:-162.05
x: 2882, y:-162.05

I gather that the rotation is therefore always horizontal, but can't for the life of me see why? Can anyone give me an answer? Assuming it's simple enough because the code I used to setup the rotation is the same working code I have used to turn a movieclip towards the mouse..

Any ideas?

Ta!

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

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

发布评论

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

评论(1

紧拥背影 2024-12-18 17:17:37

从弧度到度数的转换包含一个错误:

var degrees1 = radians1 / Math.PI / 180;

应该是:

var degrees1 = radians1 / Math.PI * 180;

或者使其更容易理解:

var degrees1 = 180 * radians1 / Math.PI;

在您的情况下, Degrees1 可能是接近 0 的值;所以运动总是水平的。

The conversion from radians to degrees contains a bug:

var degrees1 = radians1 / Math.PI / 180;

Should be:

var degrees1 = radians1 / Math.PI * 180;

Or to make it more understandable:

var degrees1 = 180 * radians1 / Math.PI;

In your case degrees1 probably is value near 0; so movement is always horizontal.

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