This site requires JavaScript, please enable it in your browser!
Greenfoot back
CosmicCaleb
CosmicCaleb wrote ...

2018/10/22

Shooting Direction

CosmicCaleb CosmicCaleb

2018/10/22

#
I have a person who is shooting a gun. When you are running left it shoots left.
1
2
3
4
5
if (Greenfoot.isKeyDown("left"))
{
     direction = -50;
     directions = 0;
}
When you are running right it shoots right.
1
2
3
4
5
if (Greenfoot.isKeyDown("right"))
{
     direction = +50;
     directions = 0;
}
When I have him standing still, he shoots up.
1
2
3
4
5
6
7
8
if (!right && !left && !up && !down)
        {
            if (Greenfoot.isKeyDown("space"))
            {
 
                 direction = 0;
                 directions = -50;
            }
I want to make it so that when you are stopped it shoots in the direction you were last facing. So if I was running left, and stopped, and shot, it would shoot left. If I was running right, stopped, and shot, it would shoot right. ("direction" is the horizontal movement, + is right, - is left.) ("directions" is the vertical movement, + is down, - is up.)
danpost danpost

2018/10/22

#
To be sure, Is this to be a top-down view scenario where you can shoot in all 4 directions or a side view one where you only want left and right shooting?
CosmicCaleb CosmicCaleb

2018/10/23

#
You can shoot in all 4 directions, but I just had it like this so it would be easier to read and I wouldn't have to write quite as much.
danpost danpost

2018/10/23

#
CosmicCaleb wrote...
You can shoot in all 4 directions, but I just had it like this so it would be easier to read and I wouldn't have to write quite as much.
This really did not answer my question. Nor, did it provide all of what I was trying to understand. I was hoping to get an answer to this, as well:
Does your player's rotation change dependent to the direction it moves and should shoot?
If not, then you need a single field for the "facing" direction (not two). Otherwise, the rotation of the player can be used, instead. If using a field, its values could be equivalent to the rotation (in degrees) or just the turned amount (the same degrees divided by 90). It is best to determine the new "facing" direction before any movement or shooting. That means getting all down keys before doing anything. This does a couple of things; there is no bias toward any particular direction(s) and the code is simplified. I usually do this to determine facing directiion:
1
2
3
4
5
int dx = 0, dy = 0; // pointers for horizontal and vertical
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("right")) dx++;
if (Greenfoot.isKeyDown("up")) dy--;
if (Greenfoot.isKeyDown("down")) dy++;
Now, I would want a single direction to be acquired. That is, no diagonal movement (meaning one of the two, dx or dy, must have a value of zero); and some movement (one of the two must be non-zero). So, I continue with:
1
2
3
4
if (dx*dy == 0 && dx+dy != 0)
{
    // set facing direction and move here
}
Line 3 would be replaced by either (rotating actor)
1
2
3
int direction = dx != 0 ? 1-dx : 2-dy;
setRotation(direction*90);
move(1); // adjust speed as needed
or by (non-rotating actor; using direction field)
1
2
direction = dx != 0 ? 1-dx : 2-dy;
setLocation(getX()+dx, getY()+dy);
Then, for shooting, use either 'getRotation()' or 'direction*90', respectively, for bullet direction.
You need to login to post a reply.