Your first game! Part 2: Shooting StuffPosted on: March 10, 2015 at 1:01 am,
Making a bullet!
Next up is to create our bullet. We need a new sprite and a new object. Create a new sprite called “spr_bullet“. Now you can design this to your taste but I chose to make a simple 4×4 white square. Just make sure that the origin is properly centered on your bullet!
Then create a new object called obj_bullet. Assign it to have spr_bullet as a sprite. Feel familiar? This is exactly what we did in part one to create the player spaceship!
For now we only need to create one piece of logic for the bullet itself, we’re going to make it so that we don’t end up with an infinite number of bullets on screen by destroying them when they leave the play area. So first off go ahead and add the “Outside Room” event. After that, in the “Main 1” tab in the actions area, find the “Destroy Instance” action:
and add it to the event. Make sure “applies to self” is ticked on the dialogue window that pops up and click ok.
This works as you might expect. When the object is outside the room, gamemaker destroys it! If you want to know how to do this in code, you can accomplish exactly the same thing using “Execute Code” and the line “instance_destroy();” This function will destroy the instance (specific object in a room) which calls it. This function does not have any arguments that need passing along to it, so you can simply open and close the brackets at the end of the command without putting anything in them.
However as destroying the instance is literally all we want to do in this case, we can save our fingers a few presses and just use this handy action as it does exactly the same thing!