The auto-complete feature in the editor can be helpful when writing button events.
Button Events
As shown in blocks, ||controller:button events||
are very important for games,
as they serve as a way for the user to interact with the world the developer creates.
||controller:Button events||
in Arcade have a number of options that
allow for precise control of how the user will player the games you make.
Example #1: Basic Input
This example shows a single ||controller:button event||
,
and breaks down the difference pieces that make up the event.
- Review the code below
- Identify what happens when a button is pressed
- Review the descriptions below to identify how the code snippet works
controller.anyButton.onEvent(ControllerButtonEvent.Pressed, function () {
// Code that runs when the button is pressed
game.splash("A button was pressed");
});
Breaking down the event
||controller:controller||
: namespace which contains functions and values relating to the controller||controller:anyButton||
: the button to set up the event for.||controller:anyButton||
will trigger the event whenever a button is pressed||controller:onEvent||
: the function used to assign an event handler, similar to||game:game.onUpdate||
||controller:ControllerButtonEvent||
: an enum (like||sprites:SpriteKind||
) to distinguish between the different types of button events||controller:Pressed||
: the property of the||controller:ControllerButtonEvent||
that was selected. This indicates that the event should occur when the given button is pressed
Student Task #1: Press ||controller:A||
!
- Copy the code from the example above
- Change the message that is
||game:splash||
ed to “The A button was pressed” - Modify the event to instead only occur when the
||controller:A||
button is pressed, instead of||controller:anyButton||
Concept: ||sprites:Sprites||
and ||controller:Buttons||
A ||sprites:Sprite||
will often serve as the main character of a game.
As such, ||controller:button||
events will often be used to control the player.
In previous lessons, ||controller:controller.moveSprite||
was introduced
to easily control ||sprites:sprites||
,
but it is important to be able to control them using simple events as well.
This way, you can change the way the sprite behaves to your liking,
instead of having to stick with the default behavior.
Example #2: To the Left
- Review the code snippet below
- Identify what code gets run when the event is triggered
- Identify which
||controller:button||
(s) cause the event to trigger
let mySprite: Sprite = sprites.create(sprites.castle.princessFront0, SpriteKind.Player);
controller.left.onEvent(ControllerButtonEvent.Pressed, function () {
mySprite.x -= 5;
});
Student Task #2: To the Right
- Start with the code from example #2
- Add another
||controller:button event||
that makes the||sprites:sprite||
move to the right when then||controller:right||
button is pressed - Move the
||sprites:sprite||
back and forth to verify that the
Concept: Pressed, Released, and Repeated
There are three different types of ||controller:ControllerButtonEvents||
,
that occur based off the different states of the ||controller:Buttons||
.
Example #3: Button Set Up
- Review the code snippet below and open it in the simulator
- Press any button quickly, and identify what happens
- After the
||sprites:sprite.say||
finishes, press and hold the||controller:A||
button. Identify which events occurs - Release the button, and identify which event occurs
let topSprite: Sprite = sprites.create(sprites.castle.princessFront0, SpriteKind.Player);
let middleSprite: Sprite = sprites.create(sprites.castle.princessFront0, SpriteKind.Player);
let bottomSprite: Sprite = sprites.create(sprites.castle.princessFront0, SpriteKind.Player);
topSprite.y = 30;
middleSprite.y = 60;
bottomSprite.y = 90;
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
topSprite.say("Pressed", 1000);
});
controller.A.onEvent(ControllerButtonEvent.Repeated, function() {
middleSprite.say("Repeated", 1000);
});
controller.A.onEvent(ControllerButtonEvent.Released, function () {
bottomSprite.say("Released", 1000);
});
Student Task #3: Play Catch
- Start with the code from example #3
- When the
||controller:down||
button is||controller:Pressed||
, create a||sprites:projectile||
that starts at the||variables:bottomSprite||
and flies upwards - When the
||controller:up||
button is||controller:Released||
, create a||sprites:projectile||
that starts at the||variables:topSprite||
and flies downwards - When the
||controller:right||
button is held (||controller:Repeated||
), increase the||info:score||
by 1
What did we learn?
- How are button events different than other events?
- How is the specific button specified when creating a button event?
- What is the difference between the three types of
||controller:ControllerButtonEvent||
s,||controller:Pressed||
,||controller:Released||
, and||controller:Repeated||
?