Skip to content

Commit ad5d6b6

Browse files
committed
added animated versions
1 parent 3ef4607 commit ad5d6b6

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

custom.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,39 @@ namespace events {
3535

3636
//% color=#FF4D88 icon="\uf443" block="Characters"
3737
namespace characters {
38+
//% block="create $image | which is $type | moving left: $left | moving right: $right"
39+
//% image.shadow="screen_image_picker"
40+
//% type.shadow="MovingItemType.Nice"
41+
//% left.shadow="animation_editor"
42+
//% right.shadow="animation_editor"
43+
export function createAnimatedMovingItem(image = img``, type: MovingItemType, left = [img``], right = [img``]) {
44+
let kind = SpriteKind.Food
45+
46+
if (type == MovingItemType.Bad) {
47+
kind = SpriteKind.Enemy
48+
}
49+
50+
let x = randint(-50, 50)
51+
let sprite = sprites.createProjectileFromSide(image,x,randint(-50, 50))
52+
sprite.setKind(kind)
53+
54+
if (x < 0) {
55+
animation.runImageAnimation(
56+
sprite,
57+
left,
58+
200,
59+
true
60+
)
61+
} else {
62+
animation.runImageAnimation(
63+
sprite,
64+
right,
65+
200,
66+
true
67+
)
68+
}
69+
}
70+
3871
//% block="create $image | which is $type"
3972
//% image.shadow="screen_image_picker"
4073
//% type.shadow="MovingItemType.Nice"
@@ -53,6 +86,40 @@ namespace characters {
5386
player = sprites.create(image, SpriteKind.Player);
5487
player.setStayInScreen(true);
5588
}
89+
90+
//% block="Create animated player $image | moving left: $left | moving right: $right"
91+
//% image.shadow="screen_image_picker"
92+
//% left.shadow="animation_editor"
93+
//% right.shadow="animation_editor"
94+
export function createAnimatedPlayer(image = img``, left = [img``], right = [img``]) {
95+
player = sprites.create(image, SpriteKind.Player);
96+
player.setStayInScreen(true);
97+
controller.player1.onButtonEvent(ControllerButton.Left, ControllerButtonEvent.Pressed, function() {
98+
animation.runImageAnimation(
99+
player,
100+
left,
101+
200,
102+
true
103+
)
104+
});
105+
106+
controller.player1.onButtonEvent(ControllerButton.Right, ControllerButtonEvent.Pressed, function() {
107+
animation.runImageAnimation(
108+
player,
109+
right,
110+
200,
111+
true
112+
)
113+
});
114+
115+
controller.player1.onButtonEvent(ControllerButton.Left, ControllerButtonEvent.Released, function () {
116+
animation.stopAnimation(animation.AnimationTypes.All, player)
117+
})
118+
119+
controller.player1.onButtonEvent(ControllerButton.Right, ControllerButtonEvent.Released, function () {
120+
animation.stopAnimation(animation.AnimationTypes.All, player)
121+
})
122+
}
56123
}
57124

58125
//% color=#006400 icon="\uf11b" block="Controller"

ks1-catch-game.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ Click on the grey box
6666

6767
Then click **Gallery** again and change the picture if you want to.
6868

69+
Filling like a challenge? Try the animated version!
70+
6971
```blocks
7072
scene.setBackgroundImage(img``)
7173
characters.createPlayer(img``)
74+
75+
characters.createdAnimatedPlayer(img``, [img``], [img``])
7276
```
7377
## Controlling our player
7478

@@ -140,13 +144,22 @@ What might happen when we catch this item?
140144

141145
Remember to check the hints if you are unsure about your code.
142146

147+
Feeling like a challenge? Try the animated version!
148+
143149
```blocks
144150
events.everyNSeconds(5, function () {
145151
characters.createMovingItem(img``, MovingItemType.Nice)
146152
characters.createMovingItem(img``, MovingItemType.Bad)
147153
})
154+
155+
events.everyNSeconds(5, function () {
156+
characters.createAnimatedMovingItem(img``, MovingItemType.Nice, [img``], [img``])
157+
characters.createAnimatedMovingItem(img``, MovingItemType.Bad, [img``], [img``])
158+
})
148159
```
149160

161+
162+
150163
## Scoring points
151164

152165
Time for another **event**!

0 commit comments

Comments
 (0)