From Test-Scratch-Wiki
| It has been suggested that this page's contents be merged with the page Simulating Gravity. You can discuss this on the page's talk page. |
With the right backdrop and scripts, programming a sprite to "fly" may be fairly simple, but adding gravity helps with making platformers as well as jetpack games. The following script can help with the gravity implementation in a flying engine:[factually inaccurate?]
when green flag clicked
forever
if <key [space v] pressed?> then//You're flying
glide (0.4) secs to: x: (x position) y: ((y position) + (40))
else//You're falling
glide (0.4) secs to: x: (x position) y: (-110)
Or you can simply use Velocity:
when green flag clicked
forever
if <key [space v] pressed?> then
change [Y Velocity v] by (0.1)
else
if <(Y Velocity) > (-8)>//Maximum falling speed
change [Y Velocity v] by (-0.1)//Gravity
end
if <touching [ground v]?> then //A ground sprite
set [Y Velocity v] to (0)
end
end
change y by (Y Velocity)
Example Projects
The following projects are examples of the above or a similar engine:
- http://scratch.mit.edu/projects/16777123/
- http://scratch.mit.edu/projects/14875141/
- http://scratch.mit.edu/projects/15598102/
- http://scratch.mit.edu/projects/16156895/