As of late 2021, we are in the process of updating Stencylpedia and fixing up outdated & broken content. Thanks for your patience!

159,863

Views

Beginner

Level

60

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!

60 Comments

darrellsims99
HEEELLPPPP MMEEEEEEE!!!!!!!

Im new at this whole Stencyl thing and Im stuck on the very first step of this part!!!!! It says create an event from the Add Event button but I can figure out what type of event to make cuz there are tons of them. PLEEEEAAAASSSSEEEE help me out guys.

Thanks

3 11 years, 4 months ago
hadaicoc
Hello, I'm newbie here. How can I delete an Attribute that I created? I mistakenly named the Attribute so I want to delete it and create a new one.
Thanks.

0 11 years, 7 months ago
seravifer
My problem is that when I kill all the enemies not leave any message. It is assumed that when killed so everyone should resurrect up to 5 times (5 different games) and then get the message of "Ganeste the game."

How do I restart the game?

1 11 years, 8 months ago
GameDevNation
Couldn't get the You Win! message to show up until I changed the color of the font -- and changing it in the code block (right before drawing the message) did NOT work. I had to go back to Dashboard > Fonts and change the color there where I first defined the font. After that I was able to see the message.

Jay

1 12 years, 4 days ago
matthew38940
tried the:
drawText "You Win!" at
(screenWidth/2)-(get width for "You Win!" using current font/2),
(screenHeight/2)-(get height using current font/2)

and it puts it in top left

0 12 years, 2 weeks ago
Keleborn1
enginenumber9: AddEvent>Actor>Member of Group; then set When (Enemies) is (Killed). Thanks for the screen independent logic,wileycodger!
0 12 years, 1 month ago
wilycodger
To make the text look nice and centered, you can use

drawText "You Win!" at
(screenWidth/2)-(get width for "You Win!" using current font/2),
(screenHeight/2)-(get height using current font/2)

Even if you change the scene size later, the text will still draw in the middle.

2 12 years, 2 months ago
wilycodger
There's no need to check whether the win condition has been reached every frame (i.e. "when updating"); it's more efficient to test the value of victoryCounter inside the Enemies-killed block
2 12 years, 2 months ago
wblument
x=250 y=180 puts YOU WIN! pretty much dead center
1 12 years, 2 months ago
Lamiak
Hi, I have a problem: I've done everything you have wrote but when i test the game, the sentence "You Win" did not appear after i killed the enemies. I tried to restart Stencyl, delete the events and re-create, changed colors of the font but nothing works. Can someone help me?
0 12 years, 4 months ago

Sign In to Comment