Difference between revisions of "How to move a sprite with directional inputs"
Jump to navigation
Jump to search
(Created page with "Directional (horizontal and vertical) inputs in Mini Micro are best detected using the <c>key.axis</c> method. This will respond to arrow keys, WASD, and any connecte...") |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
Directional (horizontal and vertical) inputs in [[Mini Micro]] are best detected using the <c>[[key.axis]]</c> method. This will respond to arrow keys, WASD, and any connected gamepad/joystick. This gives the player maximum flexibility in controlling their character. | Directional (horizontal and vertical) inputs in [[Mini Micro]] are best detected using the <c>[[key.axis]]</c> method. This will respond to arrow keys, WASD, and any connected gamepad/joystick. This gives the player maximum flexibility in controlling their character. | ||
− | <c>key.axis</c> returns a value from -1 to 1, with 0 being when no input is given. So, you can simply multiply this by the maximum amount you want to move the sprite, and add it to the sprite's x or y position. | + | <c>key.axis</c> returns a value from -1 (left/down) to 1 (right/up), with 0 being when no input is given. So, you can simply multiply this by the maximum amount you want to move the [[Sprite|sprite]], and add it to the sprite's x or y position. |
== Example == | == Example == | ||
Line 10: | Line 10: | ||
speed = 10 // pixels per frame | speed = 10 // pixels per frame | ||
while true | while true | ||
− | sp.x = | + | sp.x += key.axis("Horizontal") * speed |
− | sp.y = | + | sp.y += key.axis("Vertical") * speed |
yield | yield | ||
end while</ms> | end while</ms> | ||
+ | |||
+ | One possible drawback to the example above is that the sprite can move faster diagonally than when moving straight left/right/up/down. To avoid that, you can normalize the total inputs when their length is greater than 1, as shown below. | ||
+ | |||
+ | <ms>sp = new Sprite | ||
+ | sp.image = file.loadImage("/sys/pics/Wumpus.png") | ||
+ | display(4).sprites.push sp | ||
+ | speed = 10 // pixels per frame | ||
+ | while true | ||
+ | dx = key.axis("Horizontal") | ||
+ | dy = key.axis("Vertical") | ||
+ | dlen = sqrt(dx * dx + dy * dy) | ||
+ | if dlen > 1 then | ||
+ | dx = dx / dlen | ||
+ | dy = dy / dlen | ||
+ | end if | ||
+ | sp.x += dx * speed | ||
+ | sp.y += dy * speed | ||
+ | yield | ||
+ | end while</ms> | ||
+ | |||
+ | |||
+ | [[Category:Mini Micro]] [[Category:How To]] |
Latest revision as of 04:12, 14 January 2024
Directional (horizontal and vertical) inputs in Mini Micro are best detected using the key.axis
method. This will respond to arrow keys, WASD, and any connected gamepad/joystick. This gives the player maximum flexibility in controlling their character.
key.axis
returns a value from -1 (left/down) to 1 (right/up), with 0 being when no input is given. So, you can simply multiply this by the maximum amount you want to move the sprite, and add it to the sprite's x or y position.
Example
sp = new Sprite
sp.image = file.loadImage("/sys/pics/Wumpus.png")
display(4).sprites.push sp
speed = 10 // pixels per frame
while true
sp.x += key.axis("Horizontal") * speed
sp.y += key.axis("Vertical") * speed
yield
end while
One possible drawback to the example above is that the sprite can move faster diagonally than when moving straight left/right/up/down. To avoid that, you can normalize the total inputs when their length is greater than 1, as shown below.
sp = new Sprite
sp.image = file.loadImage("/sys/pics/Wumpus.png")
display(4).sprites.push sp
speed = 10 // pixels per frame
while true
dx = key.axis("Horizontal")
dy = key.axis("Vertical")
dlen = sqrt(dx * dx + dy * dy)
if dlen > 1 then
dx = dx / dlen
dy = dy / dlen
end if
sp.x += dx * speed
sp.y += dy * speed
yield
end while