Greetings.
Please excuse me if I got some mistakes withing my language. But I hope someone can help me out with this. I got a spaceship with top-view and I want to shoot in the direction the ship is pointing. So I got this function:
private function _shoot(te:TimerEvent):void {
if (_degrees < 0) _degrees += 360;
if (_degrees >= 0 && _degrees <= 90) {
_gun.x += _degrees;
_gun.y -= 90 - _degrees;
}
else if (_degrees > 90 && _degrees <= 180) {
_degrees -= 90;
_gun.x += 90 - _degrees;
_gun.y += _degrees;
}
else if (_degrees > 180 && _degrees <= 270) {
_degrees -= 180;
_gun.x -= _degrees;
_gun.y += 90 - _degrees;
}
else if (_degrees > 270 && _degrees <= 360) {
_degrees -= 270;
_gun.x -= 90 - _degrees;
_gun.y -= _degrees;
}
}
What does it do? It get´s the current degree of my ship…a value between -360° and 360°. Then it checks in which direction the ship points and shoots into the same direction. It works pretty well so far but the code seems to be bulky. I knew there is an easier way but I m not that good with math. ^^
If you got an idea please let me know.
Thanks!
TZP