149,608

Views

Beginner

Level

58

Comments

Crash Course 2: Invaders - Part 14

by Hectate (Updated on 2015-05-02)


Part 14: Winning the Game

We now need to create an Event for our Scene that will let the player win. It will check how many Enemy Ships are left, and if the player destroys all of them, the Event will show a message on screen.

Step 95: To start, select our Level One Scene from the Dashboard, click the Events button, and Add Event for when Member of Group Is Created or Dies.

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

Step 96: Select the Enemies Group from one dropdown and the killed option from the other, as shown below.

Step 97: Next, create a Number type Attribute called Victory Counter.

Step 98: In the Event block, add an increment block, and set it to increase the Victory Counter Attribute by 1.

Step 99: Next, create a When Updated Event (which, as usual, creates an Always block). In it, add a boolean block (the "if" found under Flow > Conditions) that checks whether our Victory Counter Attribute is equal to the number of Enemies in our Scene.

NOTE: If you have fewer (or more) than 5 Enemies in the scene, be sure to set the number to that value, not 5!

Step 100: Now we need to create another Attribute that we will use to “switch on” the text that will display when the player kills all the Enemy Ships.

  1. Create an Attribute of type Boolean called Win?
  2. Select the Setter block for this Attribute (under Attributes -> Setters)
  3. Put a true block inside it

Step 101: Lastly, we need to add a When Drawing Event (under Basics). This will let us actually draw the text onto the screen.

  1. Add the When Drawing Event
  2. In the Drawing category, grab the set current font to [ ] block under the Styles subcategory and place it inside the When Drawing block.
  3. Drag over an if block, and place the Win? Attribute block in it.
  4. Lastly, drag over a draw text  [ ] at x [ ], y [ ] block and place it inside the if block. For the text to show, simply type in "You Win!"
  5. Set the coordinates for X and Y to 300 and 240 for now.

Notice that you can move your mouse cursor around in the Scene to find the best coordinates to choose - you can see the coordinates in the lower right of the Scene Editor (as shown below). Keep in mind that text positioning is determined by the left-side of the text so we are beginning our text at the center of the screen, not centering the middle of the text.

Save and test your game. You should end up with the following once you destroy your enemies.

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

Keep in mind that we're setting exact coordinates for where the text should appear. If you change the game window's (viewport's) size to something other than a Width of 640 and a Height of 480, you'll have to adjust the X and Y values shown so the text is located where desired. Also, there are better ways of aligning text so that it is automatically centered, but that is beyond the scope of this article.

Let's move on to Part 15.

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!

58 Comments

imkvan
:/ My comment was too long... I'll try to be more brief.

Drop the increment block into the "for each..." block, and set it to increment the Enemy Counter by 1. This will count all the enemies when the scene is created.
In step 88 change "increment" to "decrement".
Finally, in step 89 change the "5" to "0".
Add or remove an enemy from your scene to test it out.
Hope that was short enough and still clear!

4 9 years, 3 months ago
imkvan
If you want to have Stencyl count the number of enemies for you rather than manually entering the number of enemies in your scene, just follow these steps:
For step 87, call your new Number "Enemy Counter" instead of "Victory Counter" (this is just a technicality, but it is always good to give your values names that reflect their function; this will make debugging easier.)
Next we have a new step, but it will use a Scene event that you created earlier. Go to your "Created" event (the one that starts the music).
Click on Scene in your palette and grab the block that says "For each [Actor of Group]" (note: the block has a silhouette rather than the word "Actor")
Drop this block under the the "play" block, and set the group to "Enemies".
Now grab an "increment" block from the Math tab in your Numbers

5 9 years, 3 months ago
nszumowski
When testing, everything is fine, but when I export to Flash, I get no sound :/
0 9 years, 3 months ago
tweakmonkey
Step 86 (for me): Dashboard -> Scene -> Level One -> Events -> Add Event -> Actors -> Created or Dies -> Actor of Type.

...

Until I did this, Victory counter was not incrementing.
A quick way to check things like this is throw a "Play sound" w/ a random audio file :)

0 9 years, 4 months ago
officevalentin
But besides some few things you did a good job with all this tutorials :).
0 9 years, 4 months ago
officevalentin
For Step 86: Dashbord -> Scene -> Events -> Add Event -> Actors -> Member of Group.

I don't know why is so hard to explane this in a more perfect way than just imperfect sentences.

(To start, select our Scene from the Dashboard, and add an Actor Event (under Actors in the Event menu) ) ???

0 9 years, 4 months ago
Foxtacy
Yep, just closed and opened it, and there it is, just in case anyone runs into this :)
0 9 years, 5 months ago
Foxtacy
I cant find the drawing events, they're missing
0 9 years, 5 months ago

Sign In to Comment