In another tutorial it showed how two sprites could be put together to make a sort of turret. The base was could move and the gun attached would stick to it while rotating to aim at the mouse. This isn't very useful without an actual projectile to fire. So, this tutorial will show how to generate bullets.
The first thing to do is add a Bullet sprite onto the layout. I have this red ball which I resized to 10 x 10 pixels to fit my Gun sprite. It would look strange to have a bullet bigger than the barrel of your gun. I also put it outside the visible window of the game so that the bullet doesn't show up when the game first loads.
Next we need to add behaviors to this Bullet.
The bullet behavior was added, plus the destroy outside layout. This will make sure when the Bullet sprite leaves the layout it disappears, and the computer doesn't keep calculating where it should be.
One more piece of setup that needs to be done is to create another image point for the Gun. Go into edit the sprite and add an image point.
I called it "Shooting" and placed it at the end of the barrel where the bullets will appear.
Now let's add some events.
So what's going on here is when the left mouse button clicks the system will create a Bullet object at the "Shooting" image point we put on the Gun earlier. (Note that because the image point isn't a variable it needs quotation marks around it. Otherwise, you could also use the number for that image point instead, without quotations. In our case it would be 1.)
Now if you test it Bullet sprite will generate at the tip of the Gun every time you click, but something seems off. It doesn't aim where the Gun is aiming. This is because the Bullet has it's own angle and it isn't the same as the Gun angle.
This action needs to be added to change that.
This sets the Bullet angle to the angle of the Gun right after the object is created.
Now if you test it everything should work as you think it should.
Various Ways to Use: Beginner
Tanks and turrets
We added bullet behavior to the Bullet sprite. Built into Construct is are parameters of the bullet behavior which can be changed on the left-hand menu if you click on the Bullet sprite.
You can change the speed and gravity that affects the Bullet, and by doing so you can make the shot arch like a basketball.
Various Ways to Use: Advanced
If the mouse event was split up into left button down and left button release it can set up a charge shot. While the button is down the Bullet generated could have a speed of zero and grow in size. And when the button is released it will change its speed.
With some games you might want unlimited rapid fire shooting, but to add some difficulty a shot delay could be useful. By adding a toggling variable and a wait action it is possible to only shoot once every half a second rather than every click.
Unlimited ammo is great for some shooters. For others restricted ammo builds up more tension. Add a variable that changes for ever shot fired and when ammo is picked up.