Views

Level

Crash Course 2: Invaders - Part 10

by Hectate (Updated on 2015-05-02)

Part 10: Enabling Our Ship to Fire Bullets

Step 73: Our next Event will create a Bullet Actor and set it in motion when the player presses a keyboard key. Click the + Add Event button, mouse over Input, and select Keyboard. Remember, we should already be in the Ship's events screen to create this.

Tip: To make sure your bullets fire properly, make sure you remembered to set their Actor Type in the Physics tab to Normal. We covered this in Part 4, Step 26

Step 74: On the block that appears, set the control dropdown to action 1, as shown.

Step 75: Now we need to grab two blocks, one that creates our Bullet Actor and another that sends it on its way.

The first is under Scene -> Actors in the Palette. The second is under Actor -> Motion.

Select the Bullet Actor Type by clicking on the dropdown on the create Actor Type block, and in the blue push [Actor] block, change the [Self] field to [last created actor] and [gently] to [sharply]. Snap them together into the Event block, as shown.

Step 76: Next, we need to set where the Bullet is created and the direction it should travel in. For the x and y values, set them to x of self and y of self, which is the current position of the Ship on screen. This block is in Actor > Position. Alternatively, cClick on each dropdown (for x and y) to choose the x of self block from the Actor category in the Block Picker. Then switch the [x] to [y] in the second block, as shown.

Step 77: From here, we set the direction and force the Bullet Actor will launch at in the push [Last Created Actor] block. Set xDir to 0 (we only want the Ship to shoot bullets toward the Enemy Ships, which is up, not horizontally) and yDir to -1. Remember, that “up” on a computer screen is in the negative y direction. Lastly, set force to 40.

Test the game again, and press Action 1 to shoot your bullets (should be Z by default).

We now need to ensure our Bullet Actors are destroyed when they collide with the Enemy Ships and when they leave the screen.

Disclaimer: Use comments to provide feedback and point out issues with the article (typo, wrong info, etc.). If you're seeking help for your game, please ask a question on the forums. Thanks!

ThatGuyWithTheGibus
If you spam Z and just keep shooting, it lags horribly and you even get graphical glitches.
I wonder how much you need to crash the game...

0 4 years, 5 days ago
Master Productions
For anybody who has the problem of the bullets shooting down(off the screen)try changing the ydir to -1.
1 4 years, 2 months ago
masterjm
Hello, for those who are having problems with the ship flying instead of bullets! I suggest dem one looks at the block "push" it has to be selected "Last Created Actors"! Thanks
0 4 years, 3 months ago
BUlletfly12345
ATTENTION:!!!! SOLUTION: IF you had the problem with the BULLETS NOT FLYING. Appearing but not going anywhere and you had all the commands (x: self and stuff) correct, then check if you have set BULLETS in PHYSICS to NORMAL in GRAVITY. Had this problem myslef for ages and figured it out, just making it easier for people to find. Sorry for caps in advance. :D
0 4 years, 4 months ago
GraceManske
IGNORE LAST COMMENT. I had Create [bullet] at (x: [x ] of [self] (y: [x ] of [self] instead of (y: {y ] of [self]. This should work for anyone who has the same problem.
2 4 years, 5 months ago
GraceManske
When my ship is in the far left of the screen if I shoot my bullets, they start shooting at the top of the screen and they shoot lower and lower as I move right. Eventually they don't shoot at all. PLEASE HELP!!!!!
0 4 years, 5 months ago
mrk2476
For everyone who is having the problem of the bullets staying in one place and your ship moves instead, this might help.

Under ship in events and in keyboard some might find that the push=self. It should be last created actor. I am unsure if gently makes much a difference, but i have mine on sharply and it works.

-1 4 years, 6 months ago
RogerWhan
If you're still seeing small images for your actors try setting your scale is set to 1x when you import the frames for the actor (defaults to 4x)
6 4 years, 7 months ago
Kogoll
when press "Z" bullet are create ,but bullets not moving.