| Change Y by () | |
change y by () | |
| Category | Motion |
| Type | Stack |
| Introduced in | 11Jun04 (0.x) |
The change y by () block is a Motion block and a stack block. The block moves its sprite's Y position by the specified number.
Example Uses
Often in games, the player controls a sprite and moves it around, such as with velocity. In this way, the change y by () block (and the change x by () block) can become very useful. The Y axis ranges from -180 to 180, but sprites can be moved further.
- In this script, which could control a paddle in Pong, a yVelocity variable controls the sprite's up and down movement. This allows the sprite to accelerate and decelerate.
set [yVelocity v] to [0]
forever
if <key (up arrow v) pressed?> then
change [yVelocity v] by (2)
end
if <key (down arrow v) pressed?> then
change [yVelocity v] by (-2)
end
change y by (yVelocity)
if < (yVelocity) > [0] > then
change [yVelocity v] by (-1)
end
if < (yVelocity) < [0] > then
change [yVelocity v] by (1)
end
end
- This block is also commonly used in jump scripts, as shown below:
repeat (8)
change y by (5)
wait (0.1) seconds
end
wait (1) seconds
repeat until <touching (Ground v)>
change y by (-5)
wait (0.1) seconds
end
However, smoother jumping can be achieved by using this script:
glide (0.5)secs to x: ( x position ) y: ( ( y position ) + ( 40 ) )
wait (1) seconds
repeat until <touching (Ground v)>
glide (0.125) secs to x:( x position ) y: ( ( y position ) - ( 10 ) )
end
Workaround
- Main article: List of Block Workarounds
This block can be replicated by setting the y position instead of changing it.
set y to ( ( y position ) + ( change ) )