Click to See Complete Forum and Search --> : bouncing ball
simpelendidi
07-11-2003, 04:26 PM
Is it possible to make a circle act like a bouncing ball.
I mean, you could pick it up and make it bounce when you release it or even swing it.
necromanthus
07-11-2003, 06:31 PM
Of course.
Convert that circle into a movieclip and "translate" the Bounce Motion-Script for your particular case (Hilary did a great job there).
simpelendidi
07-11-2003, 06:40 PM
I've searched for an actionscript to do this, but I'm afraid it needs some modification to work in koolmoves. If anyone has some time left to try and make this work.
The basic idea is that you have a ball an a boundary box where the ball bounces in.
the actionscript looks like this :
onClipEvent (load) {
// Used to determine if object is being dragged
dragged = false;
// Give the object a random starting speed
xspeed = Math.round(Math.random()*4+2);
yspeed = Math.round(Math.random()*4+2);
// Define the boundaries based on the box
left = /boundary:_x-(/boundary:_width/2);
right = /boundary:_x+(/boundary:_width/2);
top = /boundary:_y-(/boundary:_height/2);
bottom = /boundary:_y+(/boundary:_height/2);
// Friction is percent of speed retained each frame
friction = 0.99;
// Constant which represents effect of gravity
gravity = 0.2;
}
onClipEvent (keyDown) {
// Allow the user to control the ball using keypad
if (Key.getCode() == Key.DOWN) {
yspeed++;
} else if (Key.getCode() == Key.UP) {
yspeed--;
} else if (Key.getCode() == Key.RIGHT) {
xspeed++;
} else if (Key.getCode() == Key.LEFT) {
xspeed--;
}
}
onClipEvent (mouseDown) {
// Is the user clicking on the ball? If so initiate a drag action.
if (this.hitTest(/:_xmouse, /:_ymouse, true)) {
dragged = true;
startDrag (this, false, left, top, right, bottom);
}
}
onClipEvent (mouseUp) {
// Let go of the ball
dragged = false;
stopDrag ();
}
onClipEvent (enterFrame) {
if (dragged) {
// Calculate the speed based on the position change per frame
xspeed = _x-lastx;
yspeed = _y-lasty;
lastx = _x;
lasty = _y;
} else {
// Reposition the ball based on speed, friction, and gravity
_x += Number(xspeed);
if (_x<left || _x>right) {
xspeed = -xspeed;
} else {
xspeed *= friction;
}
_y += Number(yspeed);
if (_y<top || _y>bottom) {
yspeed = -yspeed;
} else {
yspeed = (yspeed+gravity)*friction;
}
}
}
simpelendidi
07-13-2003, 05:55 AM
I've tried to adjust some of the code above. And I came up with this result. It's not perfect. I still want to be able to throw the ball.
Anyone who has an actionscript that can make this happen in koolmoves. Let me know !!
johnie
07-13-2003, 06:02 PM
Originally posted by simpelendidi
I've searched for an actionscript to do this, but I'm afraid it needs some modification to work in koolmoves. If anyone has some time left to try and make this work.
The basic idea is that you have a ball an a boundary box where the ball bounces in.
I'm pretty sure this was the code that Hilary Modified. There is an example on Hilary's website http://www.bridel.org/ . The code is in teh Collision example http://www.bridel.org/koolmoves/demo/collisions/collisions2.html
In order to throw the Ball... You would firts need to know in what direction the Ball was moving. You would do this by comparing _x values and _y values. This could be done with If statements.
Hmm... Let me think on this. I have some Ideas.
bridelh
07-14-2003, 07:11 AM
As Johnie said, you need to find the direction and speed of the object when clicked and draged with the mouse.
You can do that with something like this:
_global.getSpeed = function(){
this.newX = this._x;
this.newY = this._y;
this.speedX = (this.newX - this.oldX)* 0.5;
this.speedY = (this.newY - this.oldY)* 0.5;
this.oldX = this.newX;
this.oldY = this.newY;
}
Then when you release the mouse, you apply the speed to your movement portion of code something like this:
_global.moveIt = function(){
this._x += this.speedX;
this._y += this.speedY;
this.speedX *=this._parent.friction;
this.speedY *=this._parent.friction;
if (this._x >= stage.width){
this._x = 0;
}else if(this._x <=0){
this._x = stage.width;
}
if (this._y >= stage.height){
this._y = 0;
}else if(this._y <= 0){
this._y = stage.height;
}
}
I have put together a very quick demo. See attached...
Hilary
--
simpelendidi
07-14-2003, 10:18 AM
thanks. This realy helps a lot.
flashkit.com
Copyright WebMediaBrands Inc., All Rights Reserved.