A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [F8] Bouncing Ball Help

  1. #1
    Junior Member
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    6

    [F8] Bouncing Ball Help

    I want to make a ball that bounces, but each bounce is lower than the previous. I made a scene, created the floor movie clip and the ball movie clip, giving the ball the instance name ball. In the ball movieclip i put this code:

    onClipEvent(enterFrame) = function() {
    var ymov:Number = 0;
    var gravity:Number = 1;
    ymov += gravity;
    ball._y += ymov;
    if (this.hitTest(floor)) {
    ymov *= -1;
    }
    }

    but when I test for errors I get this:

    Description Source
    '{' expected onClipEvent(enterFrame) = function() {

    What am I doing wrong?

  2. #2
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    PHP Code:
    onClipEvent(load){
        var 
    ymov:Number 0;
        var 
    gravity:Number 1;
    }
    onClipEvent(enterFrame){
        
    ymov += gravity// increment value
        
    this._y += ymov// move the ball
        
    if (this.hitTest(_root.floor)) {
            
    this._y -= ymov// move the ball BACK one frame so we don't hit the floor twice
            
    ymov *= -0.8// increment value
        
    }

    Syntax error causing the compile time error.

    I also changed it to work the way you want it to work. You were declaring the values for ymov every frame so it would end up just moving at speed 1 every frame.

    Where I have the line:
    this._y -= ymov; // move the ball BACK one frame so we don't hit the floor twice

    What you REALLY want to do there is to move the ball to whatever position is just BARELY not touching the floor movieclip. I didn't know your ball radius or where your registration points were for the ball and the floor so I could not do it right. As it is now it will appear to bounce just before it hits the ground.


    Oh, also:
    ymov *= -0.8; // increment value

    Change -0.8 to your liking. A value of 1 will never lose height, a value of 0 will not bounce at all. You want something in between depending on taste. A value greater than 1 will make the ball bounce HIGHER on each bounce.
    Last edited by Alluvian; 03-03-2008 at 06:02 PM.

  3. #3
    Junior Member
    Join Date
    Jan 2008
    Posts
    13
    Quote Originally Posted by Alluvian
    Oh, also:
    ymov *= -0.8; // increment value

    Change -0.8 to your liking. A value of 1 will never lose height, a value of 0 will not bounce at all. You want something in between depending on taste. A value greater than 1 will make the ball bounce HIGHER on each bounce.
    Is the reason that the y movement will stay the same when it is 1 because gravity is set to 1. Or is that just a general rule?

  4. #4
    Junior Member
    Join Date
    Feb 2008
    Location
    Ohio
    Posts
    6
    Ok the ball moves now but it goes straight through the floor movieclip. I have uploaded the .fla file here http://rapidshare.com/files/97108593/BouncingBall.fla

    P.S.- The floor movie clip blends in with the floor. Its a thin line in the middle of the floor on the floor layer.

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