Notice that this is the same snippet from the examples in the Overlap Events section.
Activity: Info Events
The ||info:Info||
category has three values that it can keep track of by default:
a player’s ||info:score||
and ||info:health||
,
as well as a ||info:countdown||
for the game.
In addition to keeping track of those values,
it also allows for a few interesting events:
||info:on life zero||
and ||info:on countdown end||
.
These events allow you to override the default behavior of ending
the game when the values hit zero.
Concept: ||info:Life||
, ||info:Score||
, and ||info:Countdown||
s
The values in the ||info:info||
can be modified using a few different methods.
Example #1a: Setting the Score (and Life)
- Review the code below
- Identify which sections are used to modify the
||info:score||
- Identify which sections are used to modify the
||info:life||
info.setScore(0);
info.setLife(5);
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
info.changeScoreBy(1);
});
controller.B.onEvent(ControllerButtonEvent.Pressed, function () {
info.changeLifeBy(-1);
});
Example #1b: Counting Down
- Review the code below
- Identify which sections are used to create a
||info:countdown||
- Identify how the game is played: what is the goal?
info.setScore(0);
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
info.changeScoreBy(1);
});
info.startCountdown(15);
Student Task #1: Losing Life
- Review the code below, and copy it into a new project
- Set the
||info:life||
to 100 to start - Modify the
||sprites:Overlap||
event so that it will||info:info.changeScoreBy||
-1 when the||sprites:Player||
||sprites:overlaps||
with the||sprites:Enemy||
- Challenge: currently, the
||sprites:overlap||
event will continue to take away life each||game:game update||
where the||sprites:sprites||
overlap. To fix this, make one of the||sprites:sprites||
a||sprites:Ghost||
,||loops:pause||
for 500 ms, and then turn off||sprites:Ghost||
, so that the||sprites:overlap||
event will only trigger once every 500 ms
let mySprite = sprites.create(img`
1 1 1
`, SpriteKind.Player);
controller.moveSprite(mySprite, 100, 100);
let enemy = sprites.create(img`
5 2 5
2 5 2
5 2 5
`, SpriteKind.Enemy);
enemy.x += 50;
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite: Sprite, otherSprite: Sprite) {
sprite.say("ouch!", 250);
});
Concept: ||info:Info||
Events
By default, getting down to 0 ||info:lives||
or
running out of time for a ||info:countdown||
causes a ||game:game over||
.
If a game requires a different behavior for this situation,
then this behavior can be handled using one of two events:
||info:info.onLifeZero||
and ||info:info.onCountdownEnd||
.
Example #2: Running out of Lives
- Review the code below
- Identify how the
||info:info.onLifeZero||
changes the behavior of the game
info.setScore(0);
info.setLife(5);
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
info.changeScoreBy(1);
});
controller.B.onEvent(ControllerButtonEvent.Pressed, function () {
info.changeLifeBy(-1);
});
info.onLifeZero(function () {
if (game.ask("Do you want to continue?")) {
info.setLife(5);
} else {
game.over();
}
});
Student Task #2: Loss Animation
- Start with the code from task #1
- Add an
||info:on life zero||
event, which turns||variables:mySprite||
into a||sprites:Ghost||
- Use
||game:game.splash||
to display “Oh no, I have lost…” - After the
||game:splash screen||
, end the game with||game:game.over||
Student Task #3: More Time!
- Start with the code from task #2
- Add a
||info:countdown||
that starts at 20 seconds - Add an
||info:on countdown end||
event - In the
||info:on countdown end||
event, make||variables:mySprite||
||sprites:say||
“Oops, I need more time!” for 1000 ms - In the
||info:on countdown end||
event, start a new||info:countdown||
with 20 seconds
What did we learn?
- How are the properties in the
||info:Info||
category different from the||variables:Variables||
we create? - How do the
||info:Info||
events add more options to the games we create?