238,069
Views
Beginner
Level
130
Comments
Crash Course 2: Invaders - Part 12
by Hectate (Updated on 2015-05-02)
Part 12: Destroying the Enemy Ships
Now we put those bullets to use! We want to give the enemy some health so it can take a few hits, and then destroy it once it takes too much damage.
Step 83: While still in the Bullet's events we should be looking at the Actor - Type collision event. We're going to use the same collision to also tell the Enemy Ship that it was hit by a bullet.
From the palette, find the Trigger Event [text] in all behaviors for [Actor] block (found in Behavior > Triggers) and snap it above the Kill Self block (we want it to be triggered first).
Type "Hit" into the text field, and then grab the blue Actor Of Type block from the collision event wrapper above and insert it into the Actor spot. The event should look like the image below.

Step 84: Switch to the Enemy Ship actor and access the Events tab for it. Click Add Event and select Advanced > Custom Event.

Step 85: The event wrapper will appear, in the blank field type "Hit". This will cause the previous "tell" block we added (to the bullet) to trigger any code that we put in this event when triggered.
Now, if the enemy was just going to take one hit and be destroyed we could put a kill self block in here and be done with it. However, we actually want the enemy ship to take multiple hits. To track how many hits it can take, we're going to need an attribute to store a "Health" value.
Step 86: Create a new Attribute, select Number for its type, and name it Health Points (click the Attributes button on the Palette, then the Create an Attribute button).


Step 87: Add another event to the Enemy Ship. Create a When Creating Event (click + Add Event, then mouse over Basics, then click When Creating).
Step 88: Now lets actually set the initial Health value. Click on Attributes -> Setters in the Palette and drag over the Set Health Points to [ ] block, snap it inside the When Created Event, and type 3 into the empty field.

Step 89: Now that each Enemy Ship will have 3 health points when they're created, lets do some damage to them when they're hit.
Switch back to the Enemy Ship's Custom Event, and then grab the Increment Number By [ ] block (found in Numbers & Text > Math in the pallete) and put it inside the custom event. Switch the "increment" field to "decrement", and select "Health Points" in the number field (if not there already). Finally, in empty field type "1".
Step 90: Finally, we need to add an Event that checks the number of Health Points the Enemy Ship has left and destroys it if it has 0 (or fewer, in case it gets hit more than once very quickly).
To start, click the + Add Event button, mouse over Basics again, and select When Updating. Next, drag an if block inside the Always block (in Flow -> Conditions in the Palette).

Step 91: Now we add a "Less Than Or Equal To" block (under Flow -> Conditions again, it looks contains a "<=" between two numbers) to the empty field in the if block and then check whether Health Points (found in Attributes > Getters) is less than or equal to 0 or not.
Step 92: Here's where we put the Kill Self block (under Actor > Properties, if you recall).

Now our enemy ships will be killed if they are hit by three or more bullets! Feel free to test this out now. That said, its not very fun for them to just disappear; let's fix that!
Step 92: We want to use our Explosion sound effect we uploaded earlier. If we add a [Play] [Sound] block (found in Sound & Images > Sound) and select our Explosion sound effect from the dropdown menu (click the block and then select the Explosion sound we uploaded earlier), that sound will play right as our Actor is removed from the Scene.

Step 93: Now we need to add an effect to our Enemy Ship Actor Type so the player can tell when it’s hit. Switch back to the Custom Event. We’re going to use blocks under Actor -> Effects in the Palette
For the effects, grab these three blocks:
- Apply Effect [ ] to [Actor]
- Make Negative
- Remove All Effects From [Actor]
We also need to have a Do After timer block (Flow > Time in the palette).

Put the Make Negative block inside the Apply Effect block's blank space. Snap the Do After block in just below this and type ".1" into the blank for the time. Finally, put the Remove All Effects block inside the timer.

With all this in place, test the game. If everything is working, it’s time to create a way for our player to win the game.
130 Comments
Hi all,
I've just completed this tutorial which was great, a few niggles which were hard to follow at first and going off some of the comments made, got it to work as intended. Got to say it was good apart from some parts of the tutorial kinda got confusing and had to play around a bit and work things out for myself, which in it's self teaches you..Other than that great tutorial. Looking forward to learn more.
0
Shinobody's comment is exactly correct -- the loop needs to know to only execute the death sequence one single time, and yes, the kill->self command should be in the "do after __ seconds" block, not the sound effect. Also, the sound effect is not nearly 1.4 seconds long (unless the sound effect included in the download has changed since this tutorial was written. I changed it to "do after .2 seconds" and it worked perfectly.
1
I did all the instruction's that the Crash course told me to do and now i can't even see my own enemies in the actual game, but in the editor i can. Someone help if they can.
0
Hey, I'm kinda new to Stencyl here and I'm having a little trouble with this tutorial. For some reason, everything works except that when I fire the bullets at the enemies, the enemies just don't destroy themselves. I checked the collision groups and I tried changing the first event from Member Of Group to Actor of Type, but I'm still having the same problem. HELP!
1
Despite whatever it tells you, you need to place the "play sound" OUTSIDE "do after" block, otherwise game will wait BEFORE playing the sound.
Also, it makes event trigger every frame (that's what "when updating" means) as long as Enemy Ship exists, so this event will fire every frame of this 1.4 second delay (distorting the sound and slowing down game). I got around it by adding a boolean attribute DIED, made creation event (same one that sets HEALTH) set it to FALSE, death event only firing if attribute DIED=FALSE, also death event would set DIED to TRUE after firing, thus preventing enemy from dying more than once (lol).
4
We had an issue with the bullets not destroying the ships - we did what theatmospherium Suggested and IT WORKED!
theatmospherium: "If you're STILL having collision problems, make sure your Enemy Ship and Bullet actors are assigned to the right collision group under "Properties," not just in "Settings."
1
There is a problem here.. When i Test the game..this error occured.. if i go to left The Bullet Launch from high (And More and More) and if i go left the bullet launches lower and lower (and little more left, it's not shooting)
0
wow, how trying, anyway, finally got the game working, except the explosion and missile fire SOUND.....done alot of trial and error did evrything( tut..programs says.).so Im missing something I guess..help please
0
If you're STILL having collision problems, make sure your Enemy Ship and Bullet actors are assigned to the right collision group under "Properties," not just in "Settings."
1
@gpyles: I did the same thing at it works much better that way without the 1.4 sec delay. It was so bad with the do after 1.4 that I could hit the enemy with a 4th bullet before it was gone.
0






