# Bouncing Ball... Help - Macromedia Flash Sitedesign

I used to have a problem controling a ball with the key up, then someone gave me the following script to resolve the problem (thank's TRALFAZ) : onLoad = function() { dir = -1; // up inMotion = 0; } // move ball from 350 y to 150 y when key up is pressed onEnterFrame = function() { // up key only works when ball is not in motion if(!inMotion && Key.isDown(Key.UP)) inMotion = 1; if(inMotion) { // simulated gravity. // slow down when approaching apex at 150 // minimum speed is 2 speed = 2 + (ball._y - 150) ...

1. ## Bouncing Ball... Help

I used to have a problem controling a ball with the key up, then someone
gave me the following script to resolve the problem (thank's TRALFAZ) :

dir = -1; // up
inMotion = 0;
}

// move ball from 350 y to 150 y when key up is pressed
onEnterFrame = function() {
// up key only works when ball is not in motion
if(!inMotion && Key.isDown(Key.UP))
inMotion = 1;

if(inMotion)
{
// simulated gravity.
// slow down when approaching apex at 150
// minimum speed is 2
speed = 2 + (ball._y - 150) * .3;

ball._y += (speed * dir);
if(ball._y <= 150) // at the apex, change direction
{
dir = 1; // change direction to down
}
else if(ball._y >= 350) // at the bottom, stop (should bounce)
{
inMotion = 0; // stop motion
dir = -1; // changed direction to up
}
}
}

....and it did, but now i can't make the ball land in the same _y twice, as I
inteded ( it should always land on _y = 250)...

much appreciated

zecka3 Guest 2. ## Re: Bouncing Ball... Help

> ...and it did, but now i can't make the ball land in the same _y twice, as
I
> inteded ( it should always land on _y = 250)...
The example code made the object bounce up from 350y up to 150y and back
down to 350y. If you want the bottom at 250, just change all the 350s to
250s. You can set the 150s to a smaller number if you want the ball to move
higher.
good luck,
tf

tralfaz Guest 3. ## Re: Bouncing Ball... Help

"tralfaz" <tralfazmxyahoo.cam> wrote in message
news:buig46\$1ge\$1forums.macromedia.com...
>
> > ...and it did, but now i can't make the ball land in the same _y twice,
as
> I
> > inteded ( it should always land on _y = 250)...
>
> The example code made the object bounce up from 350y up to 150y and back
> down to 350y. If you want the bottom at 250, just change all the 350s to
> 250s. You can set the 150s to a smaller number if you want the ball to
move
> higher.
> good luck,
> tf
>
I'm sorry for trouble you but, the thing is that the freeking ball
doesn't land in the same _y pos twice , it keeps on changing if you don't
mind i put an example here [url]http://jdf.planetaclix.pt/exp.html[/url] if you could
take a look to it I�d appreciated.

Thank�s

zecka3 Guest 4. ## Re: Bouncing Ball... Help

When the movieclip is rotating it affects the _Y value if the registration
point is not exactly in the center. If you can't easily fix that, you could
try using a variable to track the y position like this:

dir = -1; // up
pos = 300; // position tracking variable
}

// move ball from 300 y to 150 y when key up is pressed
onEnterFrame = function() {
// up key only works when ball is not in motion
if(!inMotion && Key.isDown(Key.UP))
inMotion = 1;

if(inMotion)
{
// simulated gravity.
// slow down when approaching apex at 150
// minimum speed is 2
speed = 2 + (ball._y - 150) * .3;
pos += (speed * dir); // update the tracking var
ball._y = pos; // make the clip the same as pos
if(pos <= 150)
{
dir = 1; // change direction to down
}
else if(pos >= 300) // check the var instead of the clip
{
ball._y = pos; // force the clip to position 300
inMotion = 0; // stop motion
dir = -1; // changed direction to up
}
}
}

I hope that will do it for ya,
tf
> I'm sorry for trouble you but, the thing is that the freeking ball
> doesn't land in the same _y pos twice , it keeps on changing if you
don't
> mind i put an example here [url]http://jdf.planetaclix.pt/exp.html[/url] if you could
> take a look to it I�d appreciated.
>
> Thank�s

tralfaz Guest #### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•