I'm having trouble. I'm trying to make a ball bounce off a sprite, but it's not working! I'm using the [blocks]<if on edge, bounce>[/blocks] block. But it's not working! Help!
Offline
The "if on edge, bounce" block only works if the sprite is touching the edge of the screen. I have a project that shows how to make a laser bounce off of a wall though: http://scratch.mit.edu/projects/MoreGamesNow/755062
This code should work (I haven't tested it though): Forever if touching wall
x-velocity=x-velocity*-1
Of course, this assumes you know how to use x-velocity to change the speed of a sprite. If not look at this (sorry if it looks like javascript)
When Flag Pressed
Forever
{
if (right arrow pressed)
{
change x-velocity by 0.1
}
if (left arrow pressed)
{
change x-velocity by -0.1
}
set x-velocity to x-velocity*0.9
}
You may need to play with the 0.9 and the 0.1 numbers to get the correct speed and friction.
Offline
yeah, but I want a ball to bounce off a wall.
Offline
MoreGamesNow wrote:
The "if on edge, bounce" block only works if the sprite is touching the edge of the screen. I have a project that shows how to make a laser bounce off of a wall though: http://scratch.mit.edu/projects/MoreGamesNow/755062
This code should work (I haven't tested it though): Forever if touching wall
x-velocity=x-velocity*-1
Of course, this assumes you know how to use x-velocity to change the speed of a sprite. If not look at this (sorry if it looks like javascript)
When Flag Pressed
Forever
{
if (right arrow pressed)
{
change x-velocity by 0.1
}
if (left arrow pressed)
{
change x-velocity by -0.1
}
set x-velocity to x-velocity*0.9
}
You may need to play with the 0.9 and the 0.1 numbers to get the correct speed and friction.
It would be easier to read if you inserted the Scratch blocks...
Offline
Last edited by MoreGamesNow (2010-12-31 19:28:47)
Offline
Yes! Just like that!
Offline
I don't know if you've downloaded the script but it is basically this:
<when green flag clicked>
<go to x 0 )y
0 )>
<set{ x-velocity }to( <pick random( -4 )to( 4 )>
<set{ y-velocity }to( 0 )>
<forever>
<if><touching[ floor ]?>
<set{ y-velocity }to( 0 )>
<else>
<change{ y-velocity }by( -0.1 )>
<end>
<if><< <touching[ Wall1 ]?> <or> <touching[ Wall2 ]?> >>
<set{ x-velocity }to( x-velocity*-1 )>
<end>
<change y by( y-velocity )>
<change x by( x-velocity )>
<end>
Offline
oh, ok! Thanks!
Offline