238,072

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.

http://static.stencyl.com/help/images/trigger_event_hit.png

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

http://static.stencyl.com/help/images/cc2_create_custom_event.png

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.

http://static.stencyl.com/help/images/cc2_play_explosion.png

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.

http://static.stencyl.com/help/images/cc2_hit_effect.png

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.

 

Click here to go to Part 13.

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!

130 Comments

gpyles
Everything working as expected so far. Only thing that seemed a little 'off' was the part to put a 'do after 1.4 seconds' around the 'play explosion' 'kill self' block.

That causes the enemy to wait 1.4 seconds after the kill shot before it plays the explosion or dies. I tried playing the explosion immediately and only including the 'kill self' in the 'do after 1.4' seconds thinking it would play the explosion and then wait 1.4 seconds before killing (removing) the enemy.

This made the sound effect become distorted. Finally removed the 'do after 1.4 second' block entirely and this actually works the best.


1 13 years, 2 months ago
explosivebrick21
whenever I try to set the health points(set health points to ()) i get set health points to self instead of () and can't type in the number even when i click on it
1 13 years, 2 months ago
scjoye
This all worked fine for me when I listened to inventorsmith's comment:

Use actor of type. Not Group. It works fine.

0 13 years, 2 months ago
Keleborn1
I finally removed the game
-2 13 years, 3 months ago
Keleborn1
Really enjoyed this tutorial up to step 77. Bullets don't collide with enemies, just go right over the top of them with no effect. Tried all the fixes in the comments. Can't find help in Forums --- all I see is people who have moved on past this. Just another useless download til I get an answer.
-1 13 years, 3 months ago
Wifido
I really appreciate the tutorial. It is frustration as many have noted in that it lacks necessary details. One change I would make in this demo would be to highlight what the "apply effect" does coupled with the "make negative" ... um...modifier?

So that I could better understand, I removed the "remove all effects from Self" command and tested the program. In this case the ships colors would change to their negative values apparently...and would STAY that way...hence the need for the "do after .1 seconds".

I am however very impressed with the tool and development environment. It is going to take getting used to but I can definitely see building games MUCH faster than straight XCode for IOS development. I would like to have the option of getting into an editor to add the conditional logic personally rather than building the blocks. However, it is very cool so far.

0 13 years, 3 months ago
jsa005
Hmm... all of the enemies light up and die after the 3rd hit for me. So basically it works like it should except what should only happen to one affects them all, so I hit one, and they all light up, and on the third hit on any, they all die. Help.
0 13 years, 3 months ago
wilycodger
If hit points only decrement on bullet collision, why check them every frame instead of in the bullet collision event?
0 13 years, 3 months ago
WylieStyles
Stencyl seems like its a great product but this tutorial is a mess, and its only the second tutorial. Are the rest this bad? If so this scares me to invest time and or money into this product, like am I going to run into these same types of mixed up tutorials when I get deeper in the lessons? Yikes! Looking at the dates on these other posts it looks like it has been sitting in this condition for months. I have been looking at other engines and choose yours because it seemed to be the easiest to learn. But if you tutorials are always this bad I might as well invest the extra time in a more complex engine like Unity because the learning curve is just as steep with these semi-pro tutorials.

When making a tutorial you need to explain every single little thing, every input, explain fully what concepts we are using and why. For example why do you have us scripting all of these items when you have a drop down controls for actors? Or why set the boundary for the ship via code instead of drawing an invisible collision volume around the scene?

I'm really on the fence with moving forward with this engine after this.
*sigh* this engine has so much hope

2 13 years, 4 months ago
inventorsmith
Use actor of type. Not Group. It works fine.
1 13 years, 4 months ago

Sign In to Comment