Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » bouncing with x, y velocities
- MasterScratchC
- Scratcher
49 posts
bouncing with x, y velocities
I have a sprite with an x velocity and a y velocity variable, and when It hits the edge of the screen I need it to change the x and y velocities to make it appear it bounces off of the edge of the screen, similar to the if on edge, bounce block. how can I do this?
I can't use the direction variable, so i can't use the if on edge, bounce block
I can't use the direction variable, so i can't use the if on edge, bounce block
when green flag clicked
forever
change x by (x velocity)
change y by (y velocity)
if <touching [edge v] ?> then
do something to
end
end
define me
ask [do you like bananas?] and wait
if <(answer) = [yes]> then
say (join [me too! ] (conversation about bananas)
else
if <(answer) = [no]> then
show list [why bananas are good v]
start discussion on (why bananas are good) ::sound
end
end
- The_Imaginarium
- Scratcher
1000+ posts
bouncing with x, y velocities
when green flag clicked
forever
if <[180] < ([abs v] of (y position))> then
set [y velocity v] to ((0)-(y velocity))
end
if <[240] < ([abs v] of (x position))> then
set [x velocity v] to ((0)-(x velocity))
end
change x by (x velocity)
change y by (y velocity)
end
Last edited by The_Imaginarium (Aug. 1, 2021 18:09:14)
- awesome-llama
- Scratcher
1000+ posts
bouncing with x, y velocities
You can find which edge the sprite has touched and then change the x or y velocity relevant to that wall. If it is the side walls, make the x velocity opposite of what it was before
The specifics of this are up to you to decide since there are so many ways to solve it.
Example
The specifics of this are up to you to decide since there are so many ways to solve it.
Example
when green flag clicked
forever
if <([abs v] of (x position)) > [240]> then
set [x velocity v] to ((0) - (x velocity))
end
if <([abs v] of (y position)) > [180]> then
set [y velocity v] to ((0) - (y velocity))
end
change x by (x velocity)
change y by (y velocity)
end
- CompassesHelpMe
- Scratcher
61 posts
bouncing with x, y velocities
You need to fine tune the x and y values of the bounce to the size of your ball.
Last edited by CompassesHelpMe (Aug. 1, 2021 18:08:38)
yes
- MasterScratchC
- Scratcher
49 posts
bouncing with x, y velocities
thank you! this worked perfectly
define me
ask [do you like bananas?] and wait
if <(answer) = [yes]> then
say (join [me too! ] (conversation about bananas)
else
if <(answer) = [no]> then
show list [why bananas are good v]
start discussion on (why bananas are good) ::sound
end
end
- Discussion Forums
- » Help with Scripts
- » bouncing with x, y velocities