A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: bouncing ball

  1. #1
    Senior Member
    Join Date
    Sep 2002
    Posts
    106

    bouncing ball

    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.
    www.flashflow.be

  2. #2
    undead creature necromanthus's Avatar
    Join Date
    Feb 2002
    Location
    ROM
    Posts
    1,890
    Of course.
    Convert that circle into a movieclip and "translate" the Bounce Motion-Script for your particular case (Hilary did a great job there).

  3. #3
    Senior Member
    Join Date
    Sep 2002
    Posts
    106
    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;
    }
    }
    }
    Last edited by simpelendidi; 07-11-2003 at 05:49 PM.
    www.flashflow.be

  4. #4
    Senior Member
    Join Date
    Sep 2002
    Posts
    106
    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 !!
    Attached Files Attached Files
    www.flashflow.be

  5. #5
    Senior Member
    Join Date
    Jul 2000
    Posts
    5,087
    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...llisions2.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.

  6. #6
    Senior Member
    Join Date
    Sep 2001
    Location
    Australia
    Posts
    379
    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:

    Code:
    _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:
    Code:
    _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

    --
    Attached Files Attached Files

  7. #7
    Senior Member
    Join Date
    Sep 2002
    Posts
    106
    thanks. This realy helps a lot.
    www.flashflow.be

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center