I'm having a little trouble coding in multi-directional shooting in my game:
Basically I have:
Code:
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.
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.