From Test-Scratch-Wiki
Glide () Secs to X: () Y: () | |
Category | Motion |
Type | Stack |
The Glide () Secs to X: () Y: () block is a Motion Block and a Stack Block. The block moves its sprite steadily to the specified X and Y position in the specified amount of seconds - this is like pointing the sprite in a direction and repeatedly using Move () Steps, but with more precision. A disadvantage of the glide block, however, is that it pauses the script while the sprite is moving, preventing the script from doing other things while the sprite is gliding. Also, a glide can only be interrupted by a stopping scripts block, and the If on Edge, Bounce block will fail to work its intended function while a sprite is gliding.
Example Uses
The block is used whenever a sprite needs to glide - some common uses are:
- Fish moving in a tank
forever glide (pick random (1) to (2)) secs to x: (pick random (-240) to (240)) y: (pick random (-100) to (100))
- Obstacle sprites are created and glide toward the edge of the screen, as in Frogger
when I receive [StartCars v] hide set x to (240) repeat until <(gameOver) = [1]> create clone of [myself v] wait (3) secs end when I start as a clone show glide (5) secs to x:(-240) y:(y position) delete this clone
- Falling objects
set y to (180) glide (1) secs to x:(x position) y:(-180)
- A sprite moving to another
glide (1) secs to x: ([x position v] of [Sprite2 v]) y: ([y position v] of [Sprite2 v])
Side-by-side Comparison of the Glide() and Move() Steps Blocks
This script uses the Move () Steps block to steadily move the sprite from the left edge to the right edge in eight seconds.
set x to (-240) repeat (80) move (6) steps wait (0.1) secs end
This script does the same thing by gliding.
set x to (-240) glide (8) secs to x:(240) y:(y position)
Gliding with Ease Out Effect
The Glide block uses a purely linear tweening method, meaning that the sprite's motion does not ease in or out. However, it is fairly easy to replicate the "ease out" effect that is heavily used in graphic design, where the object slows down as it approaches its target.
when I receive [tween v] repeat until <<([abs v] of ((target x) - (x position))) < [.25]> and <([abs v] of ((target y) - (y position))) < [.25]>> change x by (((target x) - (x position)) / (2)) change y by (((target y) - (y position)) / (2)) end go to x: (target x) y: (target y)
Alternatively, you can use the following script, which, as well as doing an ease out effect, lets you choose how long should it take to get to the position you want. The 200 is the position (In this case, in the X axis) which the sprite should glide to. The two X position blocks could be replaced by the Y position one if you are willing the sprite to move by the Y axis. The number 10 is the number of deciseconds it takes to reach the position, or easier; the number of seconds multiplied by 10.[factually inaccurate?]
when I receive [tween v] repeat until <(round (x position)) = [200]> change x by (((200) - (x position)) / (10)) end