Find the block
The ||sprites:stay in screen||
block is in the Sprites menu.
Motion is the change in position. To get sprites moving, we will change their position using a game pad event. The game pad has ||controller:controller events||
for the ||controller:up||
, ||controller:down||
, ||controller:left||
and ||controller:right||
buttons.
We can use those events to change sprite location, and to make the sprite move. We will also see how to give a sprite a speed of motion, or velocity. Velocity is the rate of change of our position - in real life, this is often measured as kilometers per hour or miles per hour.
When the velocities of a sprite are not zero, then the sprite will be in motion.
In these activities, the student will use:
||sprites:x||
and ||sprites:y||
coordinates||sprites:vx||
and ||sprites:vy||
velocity||sprites:stay in screen||
let agent: Sprite = null
controller.right.onEvent(ControllerButtonEvent.Pressed, function () {
agent.x += 3
})
controller.left.onEvent(ControllerButtonEvent.Pressed, function () {
agent.x += -3
})
agent = sprites.create(img`
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . 6 6 6 6 . . . . . .
. . . . . 6 6 6 6 6 6 . . . . .
. . . . 6 6 6 6 6 6 6 . . . . .
. . . . 6 6 6 6 6 6 6 6 . . . .
. . . 6 6 6 6 6 6 6 6 6 . . . .
. . . 6 6 6 6 6 6 6 6 6 . . . .
. . . . . 6 6 6 6 6 6 . . . . .
. . . . . . 6 6 6 . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
`, SpriteKind.Enemy)
Y
direction) motions using the controller ||controller:A||
button event to move the sprite to the center of the game screen||controller:B||
button event to make the sprite “jump” (move) 15 pixelsVelocity is speed in a particular direction. In our games we typically track movement in X
and Y
directions.
If we have a positive X
velocity, for example, then our sprite will continue to increase in X
, making it move to the right across the screen.
let agent: Sprite = null
controller.right.onEvent(ControllerButtonEvent.Pressed, function () {
agent.vx += 1
})
controller.left.onEvent(ControllerButtonEvent.Pressed, function () {
agent.vx += -1
})
agent = sprites.create(img`
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . 6 6 6 6 . . . . . .
. . . . . 6 6 6 6 6 6 . . . . .
. . . . 6 6 6 6 6 6 6 . . . . .
. . . . 6 6 6 6 6 6 6 6 . . . .
. . . 6 6 6 6 6 6 6 6 6 . . . .
. . . 6 6 6 6 6 6 6 6 6 . . . .
. . . . . 6 6 6 6 6 6 . . . . .
. . . . . . 6 6 6 . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
`, SpriteKind.Enemy)
Y
direction) velocities using the controller ||controller:A||
button event move the sprite to the center of the game screen||controller:B||
button event to stop the sprite (all velocities = 0)||controller:dx||
/||controller:dy||
approachWe have created motion by capturing the key pad events and incrementing (or decrementing) a location coordinate or a velocity. Now that we have seen how this works for the four directional buttons, we can use a shorter method to handle this.
||controller:dx||
/ ||controller:dy||
code method||game:on game update||
; this is used to assign code to run whenever the game updates||game:on game update||
let ball: Sprite = null
scene.setBackgroundColor(1)
ball = sprites.create(img`
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . 6 6 6 6 . . . . . .
. . . . . 6 6 6 6 6 6 . . . . .
. . . . 6 6 6 6 6 6 6 . . . . .
. . . . 6 6 6 6 6 6 6 6 . . . .
. . . 6 6 6 6 6 6 6 6 6 . . . .
. . . 6 6 6 6 6 6 6 6 6 . . . .
. . . . . 6 6 6 6 6 6 . . . . .
. . . . . . 6 6 6 . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
`, SpriteKind.Enemy)
game.onUpdate(function () {
ball.x += controller.dx()
ball.y += controller.dy()
})
||controller:move mySprite with buttons||
and select the plus sign in the block to see ||controller:vx 100 and vy 100||
||game:on start||
By making a mirror flip of a sprite we can simulate walking by making each leg appear differently.
Flipping an image creates a mirror image when we use ||images:flip horizontal||
block. This can be useful in creating a simple 2 frame walking animation.
||sprites:mySprite image||
block in the Sprites menu – this is the image that is flippedfunction flipHorizontal() {
mySprite.image.flipX()
pause(200)
}
controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
flipHorizontal()
})
scene.setBackgroundColor(6)
let mySprite: Sprite = sprites.create(img`
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . 2 . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . 2 2 2 2 2 . . . . . . . . . . . . . . .
. . . . . . . . . . . 2 2 2 2 2 2 2 . . . . . . . . . . . . . .
. . . . . . . . . . 2 2 2 2 2 2 2 2 2 . . . . . . . . . . . . .
. . . . . . . . . . 2 2 7 2 2 2 7 2 2 . . . . . . . . . . . . .
. . . . . . . . . 2 2 2 2 2 8 2 2 2 2 2 . . . . . . . . . . . .
. . . . . . . . . . 2 2 2 2 8 8 2 2 2 . . . . . . . . . . . . .
. . . . . . . . . . 2 2 2 2 2 2 2 2 2 . . . . . . . . . . . . .
. . . . . . . . . . . 2 2 9 9 2 2 2 . . . . . . . . . . . . . .
. . . . . . . 4 4 4 4 4 2 2 2 2 2 4 4 4 4 4 . . . . . . . . . .
. . . . . . . 4 4 4 4 4 4 4 2 4 4 4 4 4 4 4 . . . . . . . . . .
. . . . . . . 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 . . . . . . . . . .
. . . . . . . 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 . . . . . . . . . .
. . . . . . . 4 4 4 . 4 4 4 4 4 4 4 . 4 4 4 . . . . . . . . . .
. . . . . . . 4 4 4 . 4 4 4 4 4 4 4 . 4 4 4 . . . . . . . . . .
. . . . . . . 4 4 4 . 4 4 4 4 4 4 4 . 4 4 4 . . . . . . . . . .
. . . . . . . 4 4 4 . 4 4 4 4 4 4 4 . 4 4 4 . . . . . . . . . .
. . . . . . . 4 4 4 . 4 4 4 4 4 4 4 . a a a . . . . . . . . . .
. . . . . . . 4 4 4 . 4 4 4 4 4 4 4 . a . a . . . . . . . . . .
. . . . . . . a a a . 4 4 4 4 4 4 4 . a a a . . . . . . . . . .
. . . . . . . a . a . 4 4 4 4 4 4 4 . . . . . . . . . . . . . .
. . . . . . . a a a . 7 7 . . . . 7 7 . . . . . . . . . . . . .
. . . . . . . . . . . 7 7 . . . . 7 7 . . . . . . . . . . . . .
. . . . . . . . . . . 7 7 . . . . 7 7 . . . . . . . . . . . . .
. . . . . . . . . . . 7 7 . . . . 7 7 . . . . . . . . . . . . .
. . . . . . . . . . . 7 7 . . . . 7 7 . . . . . . . . . . . . .
. . . . . . . . . . . 7 7 . . . . 7 7 . . . . . . . . . . . . .
. . . . . . . . . . . 7 7 . . . . 7 7 . . . . . . . . . . . . .
. . . . . . . . . . d d d . . . . 7 7 . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . 7 7 . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . d d d . . . . . . . . . . . . .
`, SpriteKind.Player)
||controller:up||
, ||controller:down||
, ||controller:left||
, ||controller:right||
buttons on the controller||controller:A||
button to ||images:flip vertically||
to flip the image upside down.||images:flip||
in task #4, ||variables:mySprite||
is not what is flipped. What is actually flipped? Explain how you know.