159,864
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.
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.
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.
- Create an Attribute of type Boolean called Win?
- Select the Setter block for this Attribute (under Attributes -> Setters)
- 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.
- Add the When Drawing Event
- In the Drawing category, grab the set current font to [ ] block under the Styles subcategory and place it inside the When Drawing block.
- Drag over an if block, and place the Win? Attribute block in it.
- 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!"
- 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.
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.
60 Comments
I think there are simply too many versions of Stencyl floating around. There absolutely is not a "When enemies is killed" block in my version of Stencyl, and I just don't see anything that would have a comparable effect. ....so frustrating.
1
The tutorial forgot to mention that the font color should be changed, since right now it's black and is difficult to see.
- In step 90 -
So, after you set the current font to "Default Font":
- In the Drawing Category again, grab the "set font color to []" block, under the Styles subcategory, and place it right under the "set current font to []" block.
The text will show up in a yellow color as default.
1
likely lots of problems stem from group type and actor type mislabel. I'm still learning where to find these values, but what I did do is changed the 'when actor type-enemy is killed' vs. 'when group type-enemy is killed' and it worked.
also what i found helpful was just adding a 'draw text 'victory counter' x: y: so that the actual value appears on the screen and you can debug whether your counter is in fact working. That's how i found mine was not. I also used previous member's suggestion to place the victory count at screen width/2 so that it's somewhere in the middle.
hope that helps someone! : )
1
In step 90, don't forget to actually set the font to "Default Font." I can't tell if Joe is overestimating his audience or is just one of those guys who knows his software too well to teach it.
0
I would add after the line: "To start, select our Scene from the Dashboard, and add an Actor Event (under Actors in the Event menu) that checks whether Actors in a Collision Group are created or killed" the following: "(Add Event -> Actors -> Created or dies -> Members of Group)". I think it would clarify things more. just a suggestion...
3
Victory Counter - Save your game, exit srencyl then re-open it, victory counter will be there
that worked by me
0
response to CargoCultist:
I believe stencyl draws instances from their starting origin, which is by default the top left corner of the "You Win!" text (game development kits like this and GameMaker use an inverted Y-axis, with the down direction being positive, and up direction being negative).
When you use the room_width/2 draw location, the program is drawing the origin of your text starting from the exact middle of the room, which makes the text look off-center. Try instead to add an extra subtraction block:
x: (screen_width/2)-100
this will push the drawn text towards the left
0
I changed the "draw text" block from 'draw at x:300, y:240' to 'draw at x:(scene width / 2), y:(scene height / 2)' in order to have the game draw the victory message at the center of the screen regardless of its size, but the message keeps popping up in the same place despite the new instructions, as though I hadn't changed anything. Anyone else have this problem?
1
@snortch: when updating refers to every time the game makes a calculation, or, once every frame, in terms of framerate
0