|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Ball bouncing off of a box
I have a movie clip (square) that moves from the top of the page to the bottom of the page, then another one (a ball) bouncing all over the place. I have it so when they hit, the ball bounces off depending on which part of the square it hits (top, bottom, left, right). The problem is, the way it picks which way to bounce (if it hits the top or bottom side it reverses it's y direction, left and right side chang x direction) doesn't seem to be working. The code is attached, and you can find the swf at www.madskater.com/randfiles/brokenswf.swf (just check the second checkbox on the initial frame, and set the speed to 5 or so to slow it down). It works probably 60-70% of the time, but for some reason when it should bounce off of the bottom, for example, it will act as if it had bounced off of the side. Help would be appreciated.
if (Math.abs(this._y-(_root["fallingblock"+k]._y))>Math.abs(this._x-(_root["fallingblock"+k]._x))) { if (this._y>(_root["fallingblock"+k]._y) && yspeed<0) { yspeed *= -1; } else { if (this._y<(_root["fallingblock"+k]._y) && yspeed>0) { yspeed *= -1; } else { xspeed *= -1; } } } else { if (this._x>(_root["fallingblock"+k]._x) && xspeed<0) { xspeed *= -1; } else { if (this._x<(_root["fallingblock"+k]._x) && xspeed>0) { xspeed *= -1; } else { yspeed *= -1; } } } |
|
#2
|
|||
|
|||
|
Hi, I think it's a speed problem. When it was slowed to 5 it happened almost all the time but when it's faster it works fine. Maybe the ball isnt clearing the square fast enough so when it appears to have contacted the bottom first it then touches the left hand side (for example) and shoots off left. It's a bit hard to explain but I hope you get what I mean. You could set a value on the ball or in the main movie which holds the name of the last object to touch the ball. Then when a contact is made check whether the current contact is the same as the last one. if thats the case dont do anything so it keeps on it's original trajectory.
|
![]() |
| Viewing: Dev Shed Forums > Web Design > Flash Help > Ball bouncing off of a box |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|