238,068
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
The last bullet remain on the screen after the enemy ship was destroyed. I think the problem was because of the bullet might not know if it is touching the enemy or not (because the enemy is killed right at the exact moment the last bullet impact). So I tried insert another block to see if the bullet is touching its target, by add a 'Do after xx seconds' before the enemy is killed (So now the enemy is still remain on the screen for xx seconds before disappear.) And it works! Now the last bullet is not remain on the screen anymore.
always
> if [Health Point] <= [0]
> do after [0.0] seconds
> kill [Self]
Note that this also work eventhough you put 0.0 seconds.
2
1.) Thanks for the crash courses.
2.a) Thanks to michoscopic and Shinobody for figuring out a way around the audio glitch.
2.b) I found another way around the audio glitch by leaving everything as described in the tutorial but instead of checking the health points in the separate update function, I packed the whole term into the actor-group-collision function of the enemy ships. This way, the game only checks the health points when there is a collision.
Is there an urgent reason why I should go the way with the extra update function? Is writing everything into the collision function 'bad style'?
3.a) Similar to enginenumber9's problem from about 16 months ago, I found that enemy ships would sometimes show erratic reactions to collision with bullets. At seemingly random intervals a direct hit to an enemy ship wouldn't be counted. The bullet simply vanished (like it should) but nothing else happened. When that happened, the enemy ship became kinda 'invincible' and could only be hit by firing two, sometimes even three bullets in quick succession.
Did anybody else encounter such a problem?
3.b) After fiddling about with the different actors and events, I concluded that the cause must lie with the kill event in the bullet actor class. I figured that (for whatever reason) sometimes bullets would kill themselves before the enemy ships registered they'd been hit.
So i 'fixed' the problem by delaying the bullet's kill [self] event with "do after x seconds". I set x to be <= .1 seconds, so that a bullet would never touch an enemy ship for longer than one frame. This way, each bullet can still only inflict a maximum damage of 1 health point. After that change, the game worked like a charm.
Can anybody help me understand, WHY it worked? I just tried something and happened to hit the right spot but I still can't see why the problem would occur for some ships (and even then only for one or two health points) and not for others.
Any help is welcome!
Cheers,
IITH
1
Problem: The sound runs for every frame during the 1.4 second wait period before the enemy ship is killed, causing sound errors and lag.
Thanks to Shinobody for correcting this, and BMJ for confirming and adding to it. It took me a while to understand, but here are the amendments:
Step 79:
a. Create a new Attibute, name it Died, make it a Boolean type
b. In the Attributes-Setters, put "set Died to" below the "set Health points" and set it to False (Comparison-False)
Step 82:
a. Add an If block (Flow-Conditionals) at the top, then an <anything = anything> block (Flow-Conditionals).
b. Assign a Died attribute (Attributes-Getters) to the left, then a False to the right.
c. Place the already-created If block inside this.
d. Place the "Play-Explosion" block ABOVE the "do after" block
e. Change the "do after" duration from 1.4 to 0.2
f. Kill Self remains untouched
g. Add a "set Died to" (Attributes-Setters) BELOW the "do after" block. Set it to True.
Your logic should now be:
Always
If Died = false,
If Health points <= 0,
Play Explosion
do after .2 seconds
kill Self
(End do)
Set Died to True
(End If)
(End If)
(End Always)
Therefore the explosion sound only plays once. The exact frame the third bullet reduces the health to zero, (died is still false), the explosion sound plays, the timer starts for the kill self, and died is now true. The next frame, since died is true, none of the logic executes, and the timer continues to count down. 0.2 seconds later, Self is killed, and that's the end of this loop.
2
My bullets stay on the screen and if i move they kill the other enemies or the enemies fall off the screen... hep me please!
0
My bullets are not hitting the space ships! The bullet disappears right before it hits the ship.
Im new to Stencyl and can't get it to work. Could someone help please?
1
My Bullets won't do anything, I've followed the instructions to the letter... The Bullets will connect, disappearing slightly before reaching the Enemies. But the Bullets have no affect on them. Please help, I've been tinkering with it for hours.
0
Funnily Enough, I was having this same problem (enemy not 'negativing' nor decrementing in health) and started to mess around. I found that if you change the event from 'when self collides with 'member of group'' to 'actor of type' and selecting 'Bullet'
2
Got it to work, my bullets wren't set to the "Bullets" collision group. I had to make my own actors, I look like a nail firing meatballs at floating squares. Also, Stencyl is amazing and it's starting to make sense!
1






