251,343

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

Squigly
My spaceship only shoots once. Any idea what I forgot to do?
0 12 years, 1 month ago
matthew1
Alexandre, x=0, y=0 is on the top left corner of the screen. If you want to walk or jump on the y axis you need to use negative values
0 12 years, 2 months ago
AlexandreBastos
Hello, I am newbie in Stencyl. Thyltin two questions and request help to clarify, being:
1) An actor is instantiated (placed in the scene) considering its center or its upper left corner?
2) Where is the coodenada 0.0 (x = 0, y = 0) in the scene? Is centered on the screen or in the upper left corner of the screen?

0 12 years, 3 months ago
Esomewa
Lol, whoops. I accidentally made it so when I press fire, my ship flies up instead of the bullets. :o
0 12 years, 3 months ago
jojo60387
I wrote the code correctly but when I press fire, The bullets don't appear
What should I do?

0 12 years, 4 months ago
snake13
My bullets now fire but stays in same place!
3 12 years, 4 months ago
snake13
My bullets don't fire!
0 12 years, 4 months ago
avoronko
so under my keyboard when I choose the choose control, neither action 1 or action 2 pops up. How do I fix it?

0 12 years, 5 months ago
Agent X
if ur bullets stay still

make sure "bullets" is not affected by gravity

go to physics

actor type "normal"

4 12 years, 7 months ago
Heymi
ok igot it!! this event will be done in SHIP's Events not in BULLETS...now its working.. :)
1 12 years, 7 months ago

" id="comment_form">



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