pxt-common-packages 9.4.2 → 9.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/built/common-sim.d.ts +8 -0
- package/built/common-sim.js +74 -0
- package/libs/azureiot/built/debug/binary.js +461 -461
- package/libs/color/built/debug/binary.js +8 -8
- package/libs/color/colors.ts +11 -0
- package/libs/color-sensor/built/debug/binary.js +8 -8
- package/libs/controller/built/debug/binary.js +6578 -6578
- package/libs/controller---none/built/debug/binary.js +6558 -6558
- package/libs/datalogger/built/debug/binary.js +63 -63
- package/libs/edge-connector/built/debug/binary.js +8 -8
- package/libs/esp32/built/debug/binary.js +462 -462
- package/libs/game/built/debug/binary.js +6497 -6497
- package/libs/game/docs/reference/scene/set-tile-at.md +2 -2
- package/libs/game/docs/reference/scene/set-tilemap.md +89 -0
- package/libs/game/docs/reference/scene/tile-at-location-equals.md +1 -1
- package/libs/game/docs/reference/scene.md +4 -2
- package/libs/game/docs/reference/sprites/sprite/ax.md +34 -3
- package/libs/game/docs/reference/sprites/sprite/ay.md +34 -3
- package/libs/game/docs/reference/sprites/sprite/fx.md +123 -0
- package/libs/game/docs/reference/sprites/sprite/fy.md +123 -0
- package/libs/game/docs/reference/sprites.md +2 -0
- package/libs/game/renderText.ts +74 -11
- package/libs/game/spritesay.ts +204 -51
- package/libs/game/tilemap.ts +1 -1
- package/libs/lcd/built/debug/binary.js +8 -8
- package/libs/light-spectrum-sensor/built/debug/binary.js +8 -8
- package/libs/lora/built/debug/binary.js +8 -8
- package/libs/matrix-keypad/built/debug/binary.js +8 -8
- package/libs/mqtt/built/debug/binary.js +176 -176
- package/libs/net/built/debug/binary.js +176 -176
- package/libs/net-game/built/debug/binary.js +8084 -8084
- package/libs/palette/built/debug/binary.js +6492 -6492
- package/libs/pixel/built/debug/binary.js +8 -8
- package/libs/power/built/debug/binary.js +8 -8
- package/libs/proximity/built/debug/binary.js +8 -8
- package/libs/radio/built/debug/binary.js +8 -8
- package/libs/radio-broadcast/built/debug/binary.js +8 -8
- package/libs/rotary-encoder/built/debug/binary.js +8 -8
- package/libs/screen/built/debug/binary.js +50 -50
- package/libs/servo/built/debug/binary.js +8 -8
- package/libs/storyboard/built/debug/binary.js +6496 -6496
- package/libs/text-to-speech/pxt.json +14 -0
- package/libs/text-to-speech/shims.d.ts +47 -0
- package/libs/text-to-speech/sim/tts.ts +72 -0
- package/libs/text-to-speech/tts.ts +70 -0
- package/package.json +1 -1
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
Set a tile at a location in the tilemap.
|
|
4
4
|
|
|
5
5
|
```sig
|
|
6
|
-
tiles.setTileAt(
|
|
6
|
+
tiles.setTileAt(tiles.getTileLocation(0, 0), null)
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
You can set a tile at a specific column and row in the tilemap using a tile location object. Specify a tile to set in the tilemap and a location for it.
|
|
10
10
|
|
|
11
11
|
## Parameters
|
|
12
12
|
|
|
13
|
-
* **tile**: the to set in the tilemap.
|
|
14
13
|
* **loc**: a tile location in the tilemap.
|
|
14
|
+
* **tile**: the to set in the tilemap.
|
|
15
15
|
|
|
16
16
|
## Example #example
|
|
17
17
|
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# set Tilemap
|
|
2
|
+
|
|
3
|
+
Set a tilemap as the current tilemap for the game scene.
|
|
4
|
+
|
|
5
|
+
```sig
|
|
6
|
+
tiles.setTilemap(null)
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
A tilemap is complex data object that contains the dimensions, layers, and tile list for a tile mapping to set as the scene for a game. A game program can have more than one tilemap defined and the game's scene or _level_ can change by setting a different tilemap at certain times during a game.
|
|
10
|
+
|
|
11
|
+
Tilemaps aren't coded by the user but are created using a tilemap editor. Each tilemap in a game project is a named resource and is contained within the project's **assets** collection. A particular tilemap is specified with it's resource name like this:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
tiles.setTilemap(tilemap`level1`)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Parameters
|
|
18
|
+
|
|
19
|
+
* **tilemap**: the tilemap data containing the tilemap layout and tiles to set for the game scene. The tilemap can be specified by it's resource name, such as: `` tilemap`level1` ``.
|
|
20
|
+
|
|
21
|
+
## Example #example
|
|
22
|
+
|
|
23
|
+
Create a maze tilemap for a sprite to travel through. Set the tilemap as the scene for the game. Create a sprite and start it travelling in the maze.
|
|
24
|
+
|
|
25
|
+
```blocks
|
|
26
|
+
tiles.setTilemap(tilemap`level1`)
|
|
27
|
+
let mySprite = sprites.create(img`
|
|
28
|
+
. . . . . . . . . . . . . . . .
|
|
29
|
+
. . . . . . . . . . . . . . . .
|
|
30
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
31
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
32
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
33
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
34
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
35
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
36
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
37
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
38
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
39
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
40
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
41
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
42
|
+
. . . . . . . . . . . . . . . .
|
|
43
|
+
. . . . . . . . . . . . . . . .
|
|
44
|
+
`, SpriteKind.Player)
|
|
45
|
+
mySprite.setPosition(0, 104)
|
|
46
|
+
mySprite.vx = 16
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## See also #seealso
|
|
50
|
+
|
|
51
|
+
[set tile at](/reference/scene/set-tile-at)
|
|
52
|
+
|
|
53
|
+
```jres
|
|
54
|
+
{
|
|
55
|
+
"transparency16": {
|
|
56
|
+
"data": "hwQQABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==",
|
|
57
|
+
"mimeType": "image/x-mkcd-f4",
|
|
58
|
+
"tilemapTile": true
|
|
59
|
+
},
|
|
60
|
+
"tile1": {
|
|
61
|
+
"data": "hwQQABAAAADu7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7u7g==",
|
|
62
|
+
"mimeType": "image/x-mkcd-f4",
|
|
63
|
+
"tilemapTile": true,
|
|
64
|
+
"displayName": "myTile0"
|
|
65
|
+
},
|
|
66
|
+
"tile2": {
|
|
67
|
+
"data": "hwQQABAAAABERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERA==",
|
|
68
|
+
"mimeType": "image/x-mkcd-f4",
|
|
69
|
+
"tilemapTile": true,
|
|
70
|
+
"displayName": "myTile1"
|
|
71
|
+
},
|
|
72
|
+
"level1": {
|
|
73
|
+
"id": "level1",
|
|
74
|
+
"mimeType": "application/mkcd-tilemap",
|
|
75
|
+
"data": "MTAwYTAwMDgwMDAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMDAwMDAwMDAwMDAwMDAwMDEwMTAwMDIwMjAyMDAwMjAyMDAwMTAxMDAwMjAwMDIwMDAyMDIwMDAwMDEwMDAyMDAwMjAwMDIwMDAwMDEwMTAwMDIwMDAyMDIwMjAwMDIwMTAwMDAwMDAwMDAwMDAyMDAwMDAxMDEwMTAxMDEwMTAxMDEwMTAxMDEyMjIyMjIyMjIyMDIwMDAwMDAyMDAyMjIwMjIyMjAwMjAyMDIyMjAwMDIwMjAyMDIyMDAyMDIyMjAyMjIwMDAwMDAwMjIwMjIyMjIyMjIyMg==",
|
|
76
|
+
"tileset": [
|
|
77
|
+
"myTiles.transparency16",
|
|
78
|
+
"myTiles.tile1",
|
|
79
|
+
"myTiles.tile2"
|
|
80
|
+
],
|
|
81
|
+
"displayName": "maze"
|
|
82
|
+
},
|
|
83
|
+
"*": {
|
|
84
|
+
"mimeType": "image/x-mkcd-f4",
|
|
85
|
+
"dataEncoding": "base64",
|
|
86
|
+
"namespace": "myTiles"
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
@@ -16,7 +16,8 @@ scene.backgroundImage()
|
|
|
16
16
|
## Tiles and Tilemaps
|
|
17
17
|
|
|
18
18
|
```cards
|
|
19
|
-
tiles.
|
|
19
|
+
tiles.setTilemap(null)
|
|
20
|
+
tiles.setTileAt(tiles.getTileLocation(0, 0), null)
|
|
20
21
|
tiles.setWallAt(tiles.getTileLocation(0, 0), false)
|
|
21
22
|
tiles.getTileLocation(0, 0)
|
|
22
23
|
tiles.getTilesByType(null)
|
|
@@ -24,7 +25,7 @@ tiles.placeOnTile(null, tiles.getTileLocation(0, 0))
|
|
|
24
25
|
tiles.placeOnRandomTile(null, null)
|
|
25
26
|
scene.onOverlapTile(SpriteKind.Player, null, function (sprite, location) {})
|
|
26
27
|
scene.onHitWall(SpriteKind.Player, function (sprite, location) { })
|
|
27
|
-
tiles.tileAtLocationEquals(
|
|
28
|
+
tiles.tileAtLocationEquals(tiles.getTileLocation(0, 0), null)
|
|
28
29
|
sprites.create(null).isHittingTile(CollisionDirection.Right)
|
|
29
30
|
sprites.create(null).tileKindAt(TileDirection.Right, null)
|
|
30
31
|
```
|
|
@@ -52,6 +53,7 @@ scene.cameraShake(4,500)
|
|
|
52
53
|
[set background image](/reference/scene/set-background-image),
|
|
53
54
|
[background color](/reference/scene/background-color),
|
|
54
55
|
[background image](/reference/scene/background-image),
|
|
56
|
+
[set tilemap](/reference/scene/set-tilemap),
|
|
55
57
|
[set tile at](/reference/scene/set-tile-at),
|
|
56
58
|
[set wall at](/reference/scene/set-wall-at),
|
|
57
59
|
[get tile location](/reference/scene/get-tile-location),
|
|
@@ -36,13 +36,15 @@ mySprite.ax = 0
|
|
|
36
36
|
|
|
37
37
|
* **value**: the new horizontal acceleration for the sprite in pixels per second, per second.
|
|
38
38
|
|
|
39
|
-
## Sprite acceleration
|
|
39
|
+
## Sprite horizontal acceleration
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
The value for acceleration determines how quickly the speed of the sprite will change. Acceleration of a sprite makes it speed up in a direction toward the right or left. A positive value causes the sprite's speed in the `right` direction to increase. A negative value causes the sprite's speed in the `left` direction to increase.
|
|
42
|
+
|
|
43
|
+
Acceleration can be used as an opposing force too. If a sprite is travelling toward the right at a certain speed and a negative horizontal acceleration is applied, the sprite will keep moving to the right but it will slow down. When the sprite's speed reaches `0`, the sprite will begin to move to the left.
|
|
42
44
|
|
|
43
45
|
### ~ hint
|
|
44
46
|
|
|
45
|
-
|
|
47
|
+
#### How speed changes
|
|
46
48
|
|
|
47
49
|
Speed, or velocity, is how much distance an object moves during some period of time. Distance in your game is measured in pixels so the speed of a sprite is in _pixels per second_. Speed is changed by _accelerating_ an object. In your game, a sprite is accelerated by some amount of change in speed per second. So, acceleration is measured in _pixels per second per second_.
|
|
48
50
|
|
|
@@ -131,6 +133,35 @@ game.onUpdateInterval(1000, function () {
|
|
|
131
133
|
})
|
|
132
134
|
```
|
|
133
135
|
|
|
136
|
+
### Opposite force #ex3
|
|
137
|
+
|
|
138
|
+
Make a sprite move from the left side of the screen to the right side. Apply acceleration in the opposite direction to slow the sprite down and send back to the left.
|
|
139
|
+
|
|
140
|
+
```blocks
|
|
141
|
+
let mySprite = sprites.create(img`
|
|
142
|
+
. . . . . . . . . . . . . . . .
|
|
143
|
+
. . . . . . . . . . . . . . . .
|
|
144
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
145
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
146
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
147
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
148
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
149
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
150
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
151
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
152
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
153
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
154
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
155
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
156
|
+
. . . . . . . . . . . . . . . .
|
|
157
|
+
. . . . . . . . . . . . . . . .
|
|
158
|
+
`, SpriteKind.Player)
|
|
159
|
+
mySprite.setStayInScreen(true)
|
|
160
|
+
mySprite.left = 0
|
|
161
|
+
mySprite.vx = 100
|
|
162
|
+
mySprite.ax = -35
|
|
163
|
+
```
|
|
164
|
+
|
|
134
165
|
## See also #seealso
|
|
135
166
|
|
|
136
167
|
[ay](/reference/sprites/sprite/ay),
|
|
@@ -36,13 +36,16 @@ mySprite.ay = 0
|
|
|
36
36
|
|
|
37
37
|
* **value**: the new vertical acceleration for the sprite in pixels per second, per second.
|
|
38
38
|
|
|
39
|
-
## Sprite acceleration
|
|
39
|
+
## Sprite vertical acceleration
|
|
40
|
+
|
|
41
|
+
The value for acceleration determines how quickly the speed of the sprite will change. Acceleration of a sprite makes it speed up in a direction toward the bottom or top. A positive value causes the sprite's speed in the `bottom` direction to increase. A negative value causes the sprite's speed in the `top` direction to increase.
|
|
42
|
+
|
|
43
|
+
Acceleration can be used as an opposing force too. If a sprite is travelling toward the bottom at a certain speed and a negative vertical acceleration is applied, the sprite will keep moving to the bottom but it will slow down. When the sprite's speed reaches `0`, the sprite will begin to move to the top.
|
|
40
44
|
|
|
41
|
-
Acceleration of a sprite makes it speed up or slow down. The value for acceleration determines how quickly the speed of the sprite will change. A positive value makes the sprite speed up as it moves. A negative value causes the sprite to slow down while it moves.
|
|
42
45
|
|
|
43
46
|
### ~ hint
|
|
44
47
|
|
|
45
|
-
|
|
48
|
+
#### How speed changes
|
|
46
49
|
|
|
47
50
|
Speed, or velocity, is how much distance an object moves during some period of time. Distance in your game is measured in pixels so the speed of a sprite is in _pixels per second_. Speed is changed by _accelerating_ an object. In your game, a sprite is accelerated by some amount of change in speed per second. So, acceleration is measured in _pixels per second per second_.
|
|
48
51
|
|
|
@@ -129,6 +132,34 @@ game.onUpdateInterval(1000, function () {
|
|
|
129
132
|
interval += 1
|
|
130
133
|
})
|
|
131
134
|
```
|
|
135
|
+
### Opposite force #ex3
|
|
136
|
+
|
|
137
|
+
Make a sprite move from the top of the screen to the bottom. Apply acceleration in the opposite direction to slow the sprite down and send back to the top.
|
|
138
|
+
|
|
139
|
+
```blocks
|
|
140
|
+
let mySprite = sprites.create(img`
|
|
141
|
+
. . . . . . . . . . . . . . . .
|
|
142
|
+
. . . . . . . . . . . . . . . .
|
|
143
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
144
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
145
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
146
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
147
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
148
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
149
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
150
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
151
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
152
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
153
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
154
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
155
|
+
. . . . . . . . . . . . . . . .
|
|
156
|
+
. . . . . . . . . . . . . . . .
|
|
157
|
+
`, SpriteKind.Player)
|
|
158
|
+
mySprite.setStayInScreen(true)
|
|
159
|
+
mySprite.top = 0
|
|
160
|
+
mySprite.vy = 80
|
|
161
|
+
mySprite.ay = -35
|
|
162
|
+
```
|
|
132
163
|
|
|
133
164
|
## See also #seealso
|
|
134
165
|
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# fy (property)
|
|
2
|
+
|
|
3
|
+
Get or set the friction opposing a sprite's motion in the vertical direction.
|
|
4
|
+
|
|
5
|
+
## Get
|
|
6
|
+
|
|
7
|
+
Get the vertical friction on the sprite.
|
|
8
|
+
|
|
9
|
+
```block
|
|
10
|
+
let mySprite: Sprite = null
|
|
11
|
+
|
|
12
|
+
let horzAccel = mySprite.fy
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```typescript-ignorelet
|
|
16
|
+
horzAccel = mySprite.fy
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Returns
|
|
20
|
+
|
|
21
|
+
* a [number](/types/number) that is the current vertical friction of the sprite.
|
|
22
|
+
|
|
23
|
+
## Set
|
|
24
|
+
|
|
25
|
+
Set the vertical friction for the sprite.
|
|
26
|
+
|
|
27
|
+
```block
|
|
28
|
+
let mySprite: Sprite = null
|
|
29
|
+
|
|
30
|
+
mySprite.fy = 0
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```typescript-ignore
|
|
34
|
+
mySprite.fy = 0
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Parameter
|
|
38
|
+
|
|
39
|
+
* **value**: the new vertical friction opposing the sprite's motion in pixels per second, per second.
|
|
40
|
+
|
|
41
|
+
## Sprite vertical friction
|
|
42
|
+
|
|
43
|
+
Friction is an opposing force against the motion of a sprite. If a sprite has a vertical velocity (`vy`), it's friction will slow the sprite down until it's vertical velocity becomes `0`. This is similar to setting an opposite vertical acceleration but the opposite acceleration goes away once the sprite stops.
|
|
44
|
+
|
|
45
|
+
## Examples #example
|
|
46
|
+
|
|
47
|
+
### Measure stopping time #ex1
|
|
48
|
+
|
|
49
|
+
Move the sprite from top to bottom across the screen. Use the correct amount of friction (`fy`) to make the sprite stop at `3` seconds.
|
|
50
|
+
|
|
51
|
+
```blocks
|
|
52
|
+
let stopTime = 0
|
|
53
|
+
let counting = true
|
|
54
|
+
let mySprite = sprites.create(img`
|
|
55
|
+
. . . . . . . . . . . . . . . .
|
|
56
|
+
. . . . . . . . . . . . . . . .
|
|
57
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
58
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
59
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
60
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
61
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
62
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
63
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
64
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
65
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
66
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
67
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
68
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
69
|
+
. . . . . . . . . . . . . . . .
|
|
70
|
+
. . . . . . . . . . . . . . . .
|
|
71
|
+
`, SpriteKind.Player)
|
|
72
|
+
mySprite.setStayInScreen(true)
|
|
73
|
+
mySprite.top = 0
|
|
74
|
+
mySprite.fy = 20
|
|
75
|
+
mySprite.vy = 60
|
|
76
|
+
game.onUpdateInterval(100, function () {
|
|
77
|
+
if (counting) {
|
|
78
|
+
stopTime += 0.1
|
|
79
|
+
if (mySprite.vy == 0) {
|
|
80
|
+
counting = false
|
|
81
|
+
mySprite.sayText("" + stopTime + "sec", 5000, false)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Skipppng sprite #ex2
|
|
88
|
+
|
|
89
|
+
Make a sprite move from the top to the bottom. Make it skip to the right once every second.
|
|
90
|
+
|
|
91
|
+
```blocks
|
|
92
|
+
let mySprite = sprites.create(img`
|
|
93
|
+
. . . . . . . . . . . . . . . .
|
|
94
|
+
. . . . . . . . . . . . . . . .
|
|
95
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
96
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
97
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
98
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
99
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
100
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
101
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
102
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
103
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
104
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
105
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
106
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
107
|
+
. . . . . . . . . . . . . . . .
|
|
108
|
+
. . . . . . . . . . . . . . . .
|
|
109
|
+
`, SpriteKind.Player)
|
|
110
|
+
mySprite.setStayInScreen(true)
|
|
111
|
+
mySprite.top = 0
|
|
112
|
+
mySprite.fy = 60
|
|
113
|
+
mySprite.vy = 60
|
|
114
|
+
for (let index = 0; index < 3; index++) {
|
|
115
|
+
mySprite.vy = 60
|
|
116
|
+
pause(1000)
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## See also #seealso
|
|
121
|
+
|
|
122
|
+
[fx](/reference/sprites/sprite/fx),
|
|
123
|
+
[ay](/reference/sprites/sprite/ay)
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# fy (property)
|
|
2
|
+
|
|
3
|
+
Get or set the friction opposing a sprite's motion in the horizontal direction.
|
|
4
|
+
|
|
5
|
+
## Get
|
|
6
|
+
|
|
7
|
+
Get the horizontal friction on the sprite.
|
|
8
|
+
|
|
9
|
+
```block
|
|
10
|
+
let mySprite: Sprite = null
|
|
11
|
+
|
|
12
|
+
let horzAccel = mySprite.fx
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```typescript-ignorelet
|
|
16
|
+
horzAccel = mySprite.fx
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
### Returns
|
|
20
|
+
|
|
21
|
+
* a [number](/types/number) that is the current horizontal friction of the sprite.
|
|
22
|
+
|
|
23
|
+
## Set
|
|
24
|
+
|
|
25
|
+
Set the horizontal friction for the sprite.
|
|
26
|
+
|
|
27
|
+
```block
|
|
28
|
+
let mySprite: Sprite = null
|
|
29
|
+
|
|
30
|
+
mySprite.fx = 0
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```typescript-ignore
|
|
34
|
+
mySprite.fx = 0
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Parameter
|
|
38
|
+
|
|
39
|
+
* **value**: the new horizontal friction opposing the sprite's motion in pixels per second, per second.
|
|
40
|
+
|
|
41
|
+
## Sprite horizontal friction
|
|
42
|
+
|
|
43
|
+
Friction is an opposing force against the motion of a sprite. If a sprite has a horizontal velocity (`vx`), it's friction will slow the sprite down until it's horizontal velocity becomes `0`. This is similar to setting an opposite horizontal acceleration but the opposite acceleration goes away once the sprite stops.
|
|
44
|
+
|
|
45
|
+
## Examples #example
|
|
46
|
+
|
|
47
|
+
### Measure stopping time #ex1
|
|
48
|
+
|
|
49
|
+
Move the sprite from left to right across the screen. Use the correct amount of friction (`fx`) to make the sprite stop at `3` seconds.
|
|
50
|
+
|
|
51
|
+
```blocks
|
|
52
|
+
let stopTime = 0
|
|
53
|
+
let counting = true
|
|
54
|
+
let mySprite = sprites.create(img`
|
|
55
|
+
. . . . . . . . . . . . . . . .
|
|
56
|
+
. . . . . . . . . . . . . . . .
|
|
57
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
58
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
59
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
60
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
61
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
62
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
63
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
64
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
65
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
66
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
67
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
68
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
69
|
+
. . . . . . . . . . . . . . . .
|
|
70
|
+
. . . . . . . . . . . . . . . .
|
|
71
|
+
`, SpriteKind.Player)
|
|
72
|
+
mySprite.setStayInScreen(true)
|
|
73
|
+
mySprite.left = 0
|
|
74
|
+
mySprite.fx = 30
|
|
75
|
+
mySprite.vx = 90
|
|
76
|
+
game.onUpdateInterval(100, function () {
|
|
77
|
+
if (counting) {
|
|
78
|
+
stopTime += 0.1
|
|
79
|
+
if (mySprite.vx == 0) {
|
|
80
|
+
counting = false
|
|
81
|
+
mySprite.sayText("" + stopTime + "sec", 5000, false)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Skipppng sprite #ex2
|
|
88
|
+
|
|
89
|
+
Make a sprite move from the left to the right. Make it skip to the right once every second.
|
|
90
|
+
|
|
91
|
+
```blocks
|
|
92
|
+
let mySprite = sprites.create(img`
|
|
93
|
+
. . . . . . . . . . . . . . . .
|
|
94
|
+
. . . . . . . . . . . . . . . .
|
|
95
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
96
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
97
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
98
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
99
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
100
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
101
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
102
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
103
|
+
. . 5 5 5 5 5 5 5 5 5 5 5 5 . .
|
|
104
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
105
|
+
. . . 5 5 5 5 5 5 5 5 5 5 . . .
|
|
106
|
+
. . . . . 5 5 5 5 5 5 . . . . .
|
|
107
|
+
. . . . . . . . . . . . . . . .
|
|
108
|
+
. . . . . . . . . . . . . . . .
|
|
109
|
+
`, SpriteKind.Player)
|
|
110
|
+
mySprite.setStayInScreen(true)
|
|
111
|
+
mySprite.left = 0
|
|
112
|
+
mySprite.fx = 80
|
|
113
|
+
mySprite.vx = 80
|
|
114
|
+
for (let index = 0; index < 3; index++) {
|
|
115
|
+
mySprite.vx = 80
|
|
116
|
+
pause(1000)
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## See also #seealso
|
|
121
|
+
|
|
122
|
+
[fy](/reference/sprites/sprite/fy),
|
|
123
|
+
[ax](/reference/sprites/sprite/ax)
|
|
@@ -58,6 +58,8 @@ sprites.onOverlap(0, 0, function (sprite, otherSprite) {})
|
|
|
58
58
|
* [**vy- velocity y**](/reference/sprites/sprite/vy)
|
|
59
59
|
* [**ax - acceleration x**](/reference/sprites/sprite/ax)
|
|
60
60
|
* [**ay - acceleration y**](/reference/sprites/sprite/ay)
|
|
61
|
+
* [**fx - friction x**](/reference/sprites/sprite/fx)
|
|
62
|
+
* [**fy - friction y**](/reference/sprites/sprite/fy)
|
|
61
63
|
|
|
62
64
|
### Image and Attributes
|
|
63
65
|
|
package/libs/game/renderText.ts
CHANGED
|
@@ -61,6 +61,8 @@ namespace sprites {
|
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
calculatePartialHeight(startLine: number, lengthToDraw: number) {
|
|
64
|
+
if (this.linebreaks.length === 0) return this.font.charHeight;
|
|
65
|
+
|
|
64
66
|
let current = 0;
|
|
65
67
|
|
|
66
68
|
for (let i = startLine; i < this.linebreaks.length + 1; i++) {
|
|
@@ -93,9 +95,9 @@ namespace sprites {
|
|
|
93
95
|
return total;
|
|
94
96
|
}
|
|
95
97
|
|
|
96
|
-
|
|
97
|
-
const prevEnd =
|
|
98
|
-
let end =
|
|
98
|
+
lineEnd(lineIndex: number) {
|
|
99
|
+
const prevEnd = lineIndex > 0 ? this.linebreaks[lineIndex - 1] : 0;
|
|
100
|
+
let end = lineIndex < this.linebreaks.length ? this.linebreaks[lineIndex] : this.text.length;
|
|
99
101
|
let didMove = false;
|
|
100
102
|
|
|
101
103
|
// Trim trailing whitespace
|
|
@@ -115,8 +117,8 @@ namespace sprites {
|
|
|
115
117
|
return didMove ? end + 1 : end;
|
|
116
118
|
}
|
|
117
119
|
|
|
118
|
-
|
|
119
|
-
let start =
|
|
120
|
+
lineStart(lineIndex: number) {
|
|
121
|
+
let start = lineIndex > 0 ? this.linebreaks[lineIndex - 1] : 0;
|
|
120
122
|
|
|
121
123
|
// Trim leading whitespace
|
|
122
124
|
while (start < this.text.length) {
|
|
@@ -133,6 +135,29 @@ namespace sprites {
|
|
|
133
135
|
|
|
134
136
|
return start;
|
|
135
137
|
}
|
|
138
|
+
|
|
139
|
+
widthOfLine(lineIndex: number, fullTextOffset?: number) {
|
|
140
|
+
if (fullTextOffset != undefined) {
|
|
141
|
+
return (Math.min(this.lineEnd(lineIndex), fullTextOffset + 1) - this.lineStart(lineIndex)) * this.font.charWidth;
|
|
142
|
+
}
|
|
143
|
+
return (this.lineEnd(lineIndex) - this.lineStart(lineIndex)) * this.font.charWidth;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
widthOfLines(lineStartIndex: number, lineEndIndex: number, offset?: number) {
|
|
147
|
+
if (this.linebreaks.length === 0) return this.widthOfLine(0, offset);
|
|
148
|
+
|
|
149
|
+
let width = 0;
|
|
150
|
+
let fullTextOffset: number;
|
|
151
|
+
for (let i = lineStartIndex; i < Math.min(lineEndIndex, this.linebreaks.length + 1); i++) {
|
|
152
|
+
if (offset != undefined) {
|
|
153
|
+
fullTextOffset = this.lineStart(i) + offset;
|
|
154
|
+
offset -= this.lineEnd(i) - this.lineStart(i);
|
|
155
|
+
}
|
|
156
|
+
if (fullTextOffset !== undefined && this.lineStart(i) > fullTextOffset) break;
|
|
157
|
+
width = Math.max(width, this.widthOfLine(i, fullTextOffset));
|
|
158
|
+
}
|
|
159
|
+
return width;
|
|
160
|
+
}
|
|
136
161
|
}
|
|
137
162
|
|
|
138
163
|
|
|
@@ -203,6 +228,9 @@ namespace sprites {
|
|
|
203
228
|
protected pageLine: number;
|
|
204
229
|
protected timer: number;
|
|
205
230
|
protected pauseMillis: number;
|
|
231
|
+
protected onTickCB: () => void;
|
|
232
|
+
protected onEndCB: () => void;
|
|
233
|
+
protected prevOffset: number;
|
|
206
234
|
|
|
207
235
|
constructor(public text: RenderText, public height: number) {
|
|
208
236
|
this.state = RenderTextAnimationState.Idle;
|
|
@@ -232,17 +260,21 @@ namespace sprites {
|
|
|
232
260
|
}
|
|
233
261
|
|
|
234
262
|
currentHeight() {
|
|
235
|
-
const
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
263
|
+
const minHeight = this.text.lineHeight();
|
|
264
|
+
const maxHeight = Math.max(
|
|
265
|
+
Math.min(
|
|
266
|
+
Math.idiv(this.height, this.text.lineHeight()) + 1,
|
|
267
|
+
this.text.linebreaks.length + 1 - this.pageLine
|
|
268
|
+
) * this.text.lineHeight(),
|
|
269
|
+
minHeight
|
|
270
|
+
);
|
|
239
271
|
|
|
240
272
|
|
|
241
273
|
if (this.state === RenderTextAnimationState.Printing) {
|
|
242
|
-
return Math.min(
|
|
274
|
+
return Math.max(Math.min(
|
|
243
275
|
this.text.calculatePartialHeight(this.pageLine, this.currentOffset()),
|
|
244
276
|
maxHeight
|
|
245
|
-
)
|
|
277
|
+
), minHeight)
|
|
246
278
|
}
|
|
247
279
|
else if (this.state === RenderTextAnimationState.Pausing) {
|
|
248
280
|
return maxHeight
|
|
@@ -252,10 +284,34 @@ namespace sprites {
|
|
|
252
284
|
}
|
|
253
285
|
}
|
|
254
286
|
|
|
287
|
+
currentWidth() {
|
|
288
|
+
return this.text.widthOfLines(
|
|
289
|
+
this.pageLine,
|
|
290
|
+
this.pageLine + Math.idiv(this.currentHeight(), this.text.lineHeight()) + 1,
|
|
291
|
+
this.state === RenderTextAnimationState.Printing ? this.currentOffset() : undefined
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
255
295
|
currentOffset() {
|
|
256
296
|
return Math.idiv(control.millis() - this.timer, this.tickPeriod)
|
|
257
297
|
}
|
|
258
298
|
|
|
299
|
+
isDone() {
|
|
300
|
+
return this.state === RenderTextAnimationState.Idle;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
cancel() {
|
|
304
|
+
this.state = RenderTextAnimationState.Idle;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
onCharacterPrinted(cb: () => void) {
|
|
308
|
+
this.onTickCB = cb;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
onAnimationEnd(cb: () => void) {
|
|
312
|
+
this.onEndCB = cb;
|
|
313
|
+
}
|
|
314
|
+
|
|
259
315
|
draw(canvas: Image, left: number, top: number, color: number) {
|
|
260
316
|
if (this.state === RenderTextAnimationState.Idle) return;
|
|
261
317
|
else if (this.state === RenderTextAnimationState.Printing) {
|
|
@@ -269,6 +325,10 @@ namespace sprites {
|
|
|
269
325
|
this.pageLine + Math.idiv(this.height, this.text.lineHeight()) + 1
|
|
270
326
|
);
|
|
271
327
|
|
|
328
|
+
if (this.onTickCB && this.prevOffset !== this.currentOffset()) {
|
|
329
|
+
this.onTickCB();
|
|
330
|
+
}
|
|
331
|
+
|
|
272
332
|
if (pageFinished) {
|
|
273
333
|
this.state = RenderTextAnimationState.Pausing;
|
|
274
334
|
this.timer = this.pauseMillis
|
|
@@ -290,6 +350,7 @@ namespace sprites {
|
|
|
290
350
|
this.pageLine += Math.idiv(this.height, this.text.lineHeight()) + 1;
|
|
291
351
|
if (this.pageLine > this.text.linebreaks.length) {
|
|
292
352
|
this.state = RenderTextAnimationState.Idle;
|
|
353
|
+
if (this.onEndCB) this.onEndCB();
|
|
293
354
|
}
|
|
294
355
|
else {
|
|
295
356
|
this.state = RenderTextAnimationState.Printing;
|
|
@@ -297,6 +358,8 @@ namespace sprites {
|
|
|
297
358
|
}
|
|
298
359
|
}
|
|
299
360
|
}
|
|
361
|
+
|
|
362
|
+
this.prevOffset = this.currentOffset();
|
|
300
363
|
}
|
|
301
364
|
}
|
|
302
365
|
}
|