PDA

View Full Version : Simple Multi Directional Shooting


stilianos
01-15-2011, 07:43 AM
I'm having a little trouble coding in multi-directional shooting in my game:

Basically I have:

if (left)
{
player.x -= player.agility;
boltDir = 270;
}
if (down)
{
player.y += player.agility;
boltDir = 180;
}
if (right)
{
player.x += player.agility;
boltDir = 90;
}
if (up)
{
player.y -= player.agility;
boltDir = 0;
}

if (up && right)
{
boltDir = 45;
}
if (up && left)
{
boltDir = 315;
}
if (down && right)
{
boltDir = 135;
}
if (down && left)
{
boltDir = 225;
}
boltDir is the direction of the fired object, the above code works great, the only problem is 50% of the time if you are trying to do a diagonal shot for example up-left, it will get stuck and go left or up not diagonally.

Anyone know how to improve this?

Thanks

rrh
01-16-2011, 04:48 PM
How are the down, left, up, right values set?

stilianos
01-18-2011, 05:44 AM
event listener of keyDown and keyUp for the arrow keys.

rrh
01-18-2011, 01:15 PM
Okay, that sounds like it *should* work.

Throw some traces in there to see if the values of them are right, and if the "if" statements are executing when you'd expect them to.

trace('values=',up,right);
if (up && right)
{
trace('up + right');
boltDir = 45;
}

If these are all working as you'd expect, look next at what it does with boltDir. If the values aren't right, double-check the keyDown and keyUp listeners.