There are some other lines of code on the bullet event and on the gun create event, but this is the majority of the code. I’m not going to go over every line in this picture because I don’t want get the female readers all hot and bothered because of my awesome skills (Sorry ladies, I’m a married man), so I will give a summary. Basically, this code tells the program if the left mouse button is pushed and the “firingdelay” variable is less than 4, the gun will fire bullets. The routine then tells GMS2 to create a “Bullet” instance and project it at whatever angle the gun is at.
Some points of note are the “random_range” feature and writing code for the gun to recoil. The random_range command sets a random number range that the coder sets to the equation. This makes the firing have a bit of sway to it. Very neat little feature. Setting up the recoil required the lengthdir_y, and lengthdir_x commands. To make the gun appear as if was recoiling, I coded the gun to move back five pixels as it was being fired. However, this is a problem as the gun is constantly moving around by the mouse. So, the computer doesn’t know where the gun should move back from. That’s where lengthdir_y, and lengthdir_x come into play. They calculate where the game is pointing and recoil from that.
Tune in for part 5 of the adventures of NickelNDime coding a game. It’s been challenging, awesomely fun, and quite a trip so far. I can’t wait to see where the upcoming parts take me! Stick around to find out!