AS3:根据其旋转移动随机生成的对象
制作一款船舶游戏是因为我非常具有原创性。除此之外,我还有一个问题。我有一个函数可以根据船的旋转来发射子弹。它在创建时使用了以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从弧度到度数的转换包含一个错误:
应该是:
或者使其更容易理解:
在您的情况下, Degrees1 可能是接近 0 的值;所以运动总是水平的。
The conversion from radians to degrees contains a bug:
Should be:
Or to make it more understandable:
In your case degrees1 probably is value near 0; so movement is always horizontal.