251,337

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

SpiritFive
for those of you who receive ERROR 1009 make sure that you're using the right actor creater
http://postimg.org/image/8orlym4sd/

0 11 years, 8 months ago
SidKid2000
When It gives you this error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
it is because you have the block
push LAST CREATED OBJECT Sharply towards etc...
set to
push LAST COLLIDED OBJECT Sharply towards etc...
change it to LAST CREATED OBJECT and it should fix the problem

-1 11 years, 8 months ago
luiswerolol
haha quite fun
0 11 years, 10 months ago
Starplayer360
Here are the errors its giving me:
at MethodInfo-3603()[Source/scripts/Ac torEvents_0.hx:97]
at com.stencyl::Engine$/invokeListener s3()[C:\Program Files (x86)\Stencyl\plaf\haxe\lib/stencyl /1,00/com/stencyl/Engine.hx:3880] at com.stencyl::Engine/update()[C:\Pro gram Files (x86)\Stencyl\plaf\haxe\lib/stencyl /1,00/com/stencyl/Engine.hx:2281] at com.stencyl::Engine/postUpdate()[C: \Program Files (x86)\Stencyl\plaf\haxe\lib/stencyl /1,00/com/stencyl/Engine.hx:2585] at com.stencyl::Engine/onUpdate()[C:\P rogram Files (x86)\Stencyl\plaf\haxe\lib/stencyl /1,00/com/stencyl/Engine.hx:2578]

0 11 years, 11 months ago
Starplayer360
When I try to fire bullets, it crashes. I cant get to work, when I try it gives me an error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
and then it says it cant access a bunch of files. Can someone help?

0 11 years, 11 months ago
Ddawg
Hey guys, I set my bullets to fire but when I press the z key my bullets will fire correctly toward the right of the screen but as I go to the left side I fire them and they stay stuck in the air and then start to fall down.. anyone know how to fix this?
0 11 years, 11 months ago
ChryslineAshtar
Mine didn't fire at first, here's how mine was fixed: I created the event inside the Bullet actor instead of the Ship actor. From Bullet, I copy-pasted (right-click on the event name) it to Ship's events. And deleted the event from bullet.
1 12 years, 1 week ago
squirtleturtle01
I cannot fire bullets. I have the enemy ships moving around the screen, and the bullet firing programmed the right way, but when I press Z (action 1) the game file just crashes and stops responding. Any help?
0 12 years, 1 month ago
yag2309
Hi, when i add the new event for bullets, select keyboard and then have to select the control 'action1', there is no 'action1' for me to select.. just up down left right jump and enter.. any ideas people?

Thanks!

-1 12 years, 1 month ago
flava621
@Squigly you must have the Keyboard event set to 'Does not Repeat'. Look in your event list and look at the icon to the right of the keyboard event. It will be a blue lined arrow with a red X. Click that icon to change it to a blue line arrow with no red X
0 12 years, 1 month ago

" id="comment_form">



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