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!
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.