251,344

Views

Beginner

Level

117

Comments

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.

Let’s continue to Part 11.

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!

117 Comments

ali hatem
Hi weegie look at the image before the last one and make sure you put (x) and (y)
0 12 years, 10 months ago
weegie
No matter what, I can't fire from the right side of the screen. And as i go more to the left the bullets fire higher and higher. Help! >:-(
2 12 years, 11 months ago
blueoxen
No mater what I put into the X setting for the bullets they always shoot from the bottom left corner.
0 12 years, 11 months ago
KrazieK
real frustrating tut. Ive done evrything it say till the end, no sound on collisions or missile fire

0 12 years, 12 months ago
chethan9
This is soo complicated ,,,we basically chose stencyl to make with ease any way here is quick solution if u r sucking at this ...

select u r ship > behavior
on left u see add button select> controls > 2 way and define left and right and chose same animation thats it done u get u r ship moving

0 13 years, 2 days ago
JAPrice
I've gone through the steps here numerous times but every time I test it and hit Z to fire, the Flash player crashes. The bullet actor type is correct and everything. I can't figure out what I'm doing wrong.
0 13 years, 1 month ago
YiGeMe
Trying the capital "Z" may be useful.
0 13 years, 1 month ago
kbrig
I'm sure I'm being stupid, but my ship flies upward after successfully firing bullets at the bottom right of the screen. Suggestions? I've gone through the code twice and can't spot it, so I suspect there is an error in my screen settings, but I can't spot it.
1 13 years, 1 month ago
ds1239
ATTENTION: The reason that most of you are having the problem of the bullet not shooting is probably because you are putting this in the events of the bullet! Put it in the events of the Ship, then it should work.
1 13 years, 1 month ago
bperk5332
I can't get my bullets to fire at all
-1 13 years, 2 months ago

" id="comment_form">



Please keep your comments civil and do not spam. Thanks for your cooperation!
Sign In to Comment