231,133
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
To fix random collision just do this 2 steps:
-remove collision event from bullet (leave only enters screen event)
-in enemy ship -> collision event -> between decrement HP by 1 and apply effect just add the block "kill last collided actor" (from actor->proprieties->kill self and change self to last collided actor)
This will resolve all problems!
3
There were 2 things that I tinkered with here to gain 100% consistency of the the bullets hitting the enemy ship
1. I took Sealhunt's (and apparently IsleInTheHeavens) advice on "Bullets randomly don't register the hit" or his #2 and delayed 'kill self' of the bullets for 0.01 seconds.
2. There is a tab that hasn't been talked about (yet) to the right of 'actors > events' called 'collision'. I adjusted the size of the collision box for the enemy ship to include the body of the ship excluding the arms. Also, I adjusted the size of the collision box for the bullets to surround both bullets.
Everything else is acceptable to me except maybe how quickly the enemy ship explodes after collision. I have ideas for how to make it quicker but I'm eager to learn more, so I'm leaving it as is.
Also, based on the comments I'm seeing below me it looks like we've gotten to the point in these tutorials where people with less of a capacity for problem solving seem to have given up.
0
Wow... you'd think somebody would fix this eventually. My problems are just like everyone else's... It started out just only randomly registering the hits, so I messed with it a bit (according to some comments) and now the bullets won't leave the screen. This is... annoying, but I think if I can't fix it, I'll just read on to get the most information I can, then find a different tutorial. It's a great program...
0
I agree with Fbarajas21 Crash course needs to be updated, for all those struggling with the following,
1. shots till on screen after enemy ship dies
2. collisions happen randomly between bullets and enemy ships
3. and explosion sound not activating heres what worked for me
look below in comments follow Fbarajas21, 3 steps save and test after you change each one and if your bullets are still on screen
then scroll further down and follow his earlier comment it worked for me
0
WHY F$#% CAN I NOT MOVE THE SHIP AND WHY THE F#$% CAN I MOVE THE LAST ENEMY SHIP ON THE RIGHT SIDE!?!?!?!?!?!?!?!?!!?!? HELP ME!!! AND MY BULLETS ARE FREAKING NOT KILLING THERE SELFS WHEN THERE HITTING THE ENEMYS!!!!
-3
Bullets hitting bad guys but hp not going down by proper number or bullet not dissapearing.
I found a much easyer way of decreasing hp and removing bullets
Enemy Ship Events
Enemy Ship Add Event > Basics > When Creating
when created
{set [Health] to ( 3 )
.in pallet make a attribute called Health(or HP/Health Points ect.)
and save ur game because it may not appear on your setter untill page is reloaded
Enemy Ship Add Event > Collisions > Actor of Type
When [Self] hits a [Bullet]
{kill [last colided actor} .this will kill the bullet the moment it hits ur bad guy while only making only one hit to Enemy ship
{IF} - [Health] > ( 1 ) .so health being your number atribute in Flow use the Equality 0>0 and hp is more than 1
- [Decrement] [Health] by ( 1 ) .use nember/text decrement this will reduce total Health by 1
{otherwise} - Kill [Self]
0
Issues and workarounds I encountered:
1. The sound plays over and over during the 1.4 second "do after x seconds" waiting time.
While the sound may last 1.4 seconds, only the first 0.x seconds have the initial hit (thump!) sound, the rest of the sound is just it fading out, so I figured it's not actually necessary. Also, from the tutorial blocks it would seem to me that the sound itself would play after 1.4 seconds after the last hit. Fixed by removing "do after x seconds" block. In an ideal scenario you would have an animated explosion while the sound plays anyway (right at the moment of the enemy being killed).
2. Bullets randomly don't register the hit.
It must have something to do with the bullet actor being killed before the hit registers for the enemy type - so it never actually removes the hp. I imagine in coding this could be remedied just by setting the order of actions to hit register enemy first, then the bullet afterwards.
Fixed by changing the collision event for bullet type to kill self after 0.01 sec after collision (thx IsleInTheHeavens)
3. The last bullet hitting the enemy when the enemy dies stays on screen.
Similarly to last issue, I think this one was related to the order of actions, I changed the event of enemy death from update to "When attribute becomes less than" (it should do the same thing, but just seems clearer to me).
Fixed by delaying enemy death by 0.015 sec. The sound effect still plays at the same time, but this can be changed in a variety of ways
Great program, seems fairly flexible, and i like the UI a lot.
You might want to update this crash course.
5
I got rid of the event that caused the player's bullet to kill itself when it hits an enemy. I think it was sometimes killing itself before the collision could register. I turned on the physics for the bullet and the enemy so the bullet bounces off and doesn't do continuous damage to the enemy. And I tell the bullet to kill itself after 2 seconds so it's range is limited.
0
Hi.
After following this tutorial I find that sometimes my bullets don't seem to affect the enemy. The bullets hit the enemy and disappear, but don't cause it to lose health points or flash negative. I have continuous collisions turned on. I have tried changing the collision shapes to circles and polygons, and that seemed to make it happen less often, but it still happens sometimes. Any ideas?
Thanks.
0
To fix the problem where the last bullet that collides with the enemy ship:
go to "Enemy Ship"
then to "Events"
then "updated", the event that was created
after that add "kill last collided actor" before "killed self"*
*this will only work in this type of games where the enemies aren't moving.
At least I believe it will just worked in this type of game.
You can also add the "kill last collided actor" right after "if health points<=0". I am new at this, therefore correct me if I am wrong but I believe that would worked much better. and even on games were the enemies are moving.
1