Changing the direction of a shot after some time

B

Babuchi

Guest
Hi there; I've run into a complication in game programming, and would like some insight into how to go about fixing it.

Here is some background: The game initially had a simple 8-direction shooting mechanic, which simply opted to shoot the projectile based on what sprite the character displayed, since there is a separate sprite for each direction. However, this wasn't precise, so I went back and did a better revision.

The new method of shooting checks for whether the character's direction is over 0. Each time it is, it sets a new variable I defined, referred to as "shotdirection" to be equal; hence even if the character isn't moving, if its most recent direction moved was north, its shot will move north.

All of that is pretty nifty while the character is moving, and shortly after. It does, however, become a bit less convenient if the character is idle and doesn't shoot at all in what feels like the correct direction. For example, the character might be in the sprite for facing straight right, but instead of shooting straight right, shoot southeastward--this is because its shotdirection is stuck in southwest from previously moving there. So I want to have another mechanic, that if the player has been still long enough, it will not use my new shotdirection variable, but instead shoot in one of the eight straight directions again, in accordance with its sprite.

I decided to attempt it with another variable, which I define as "shotstraightened" and set to 1 in the Create Event. At the moment I haven't implemented any of the actual shooting mechanics connected to that, and instead just put in a temporary printing of its value in the Draw event, to test whether it is working. So moving right along, in the Step Event, I set the "shotraightened" variable to 0 every time "direction" is over 0; this once again is so the character shoots in a more precise direction while moving, and this part works; that is, when I start the game the "shotstraightened" variable is set to 1, and the moment it starts moving it's set to 0 instead, and Alarm 2 is set to 20. The alarm is set constantly back to 20 in that part, to ensure that it never goes off until after the character has stopped moving long enough for it to tick down.

Here is where I encounter a problem; I can't get "shotstraightened" to change back to 1 after the character has stopped moving for a while, unless that character was stopped by colliding with an obstacle. I had to put in another draw action to figure out why, and it turns out the direction variable never reverts to 0 simply from you taking your fingers off the keys and letting the character slow to a stop. Why that's the case, I don't really know, but it needs to be addressed somehow. I either have to force direction to be 0 again after the character has slowed to a stop, or use something else to determine when it's time to straighten the shot.

I apologize for typing something so lengthy. I am continuing to look into this myself, but I really would appreciate help.
 
A

Aura

Guest
Code:
if (x == xprevious && y == yprevious) {
   if (alarm[0] < 0 && !shotstraightened) {
      alarm[0] = 2 * room_speed;
   }
}
In the Alarm 0 event, set shotstraightened to true.
 
B

Babuchi

Guest
I'm not so sure xprevious and yprevious will really work for this, since my point is to have a zero direction, and I'm not sure when they'd be 0. I have since discovered that the hspeed and yspeed variables do revert to 0 with friction stops, so I could feasibly use them instead. I'll keep cracking at this, and I won't rule out taking your advice.
 
Not quite sure what you mean specifically when you describe your code. But it seems over complicated unless I misunderstand. If all you want to do is shoot in 8 directions, then you can do something like this:

Code:
var_direction_last = direction;
You only update this while the player is moving. If they stop moving it uses whatever was the last position.

The bullets move in the direction of var_direction_last.
 
B

Babuchi

Guest
Not quite sure what you mean specifically when you describe your code. But it seems over complicated unless I misunderstand. If all you want to do is shoot in 8 directions, then you can do something like this:

Code:
var_direction_last = direction;
You only update this while the player is moving. If they stop moving it uses whatever was the last position.

The bullets move in the direction of var_direction_last.
A big reason that "it seems overcomplicated" is I've only got sprites for eight basic directions, but the character can move in more, because of a new system of acceleration and deceleration. Thus, which direction is most intuitive for a shot to go varies based upon what the character is doing. While the character is moving, what the sprite looks like is of secondary concern; the primary concern being that the shot should move in the direction the character moves. When the character isn't moving, though, the sprite becomes more important, as it's the only thing to judge whether a shot can be made or not; hence it's important that when a character's sprite points to the 12:00 position, it doesn't shoot towards 11:30. That's why in essence I'm going for two aiming systems; one for motion and one for standing still. On that note, here's code I developed since my last post, which works (although it needs a bit more tweaking):
Code:
/// This code detects when she's moving, so she can fire in that direction instead of straight.  The alarm is set in this step event because so long as the character is moving, it keeps setting to 10 and never goes off, while if she stops, it goes off pretty soon.
if (!(hspeed=0) or !(vspeed=0)) {
shotstraightened=0;
alarm[2]=5;
}
In other words, it works to keep "shotstraightened" from returning to 1 until the character stops moving, after which it does. So now to add the actions to shoot straight.
 
Top