A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: bouncing ball pong sort of thing

Hybrid View

  1. #1
    Junior Member
    Join Date
    May 2006
    Posts
    6

    bouncing ball pong sort of thing

    greeting all you wonderfull flash people.

    Ill get to the point fast, i realy need help whit som flash math.

    It is a bouncing ball pong sort of thing i'm trying to make.

    so on my stage i got... well,
    a ball_mc and a block_mc and the problem is i have no clue how to get ball_mc to bounce off the block _mc, can anyon help me,

    herer is the fla
    http://www.box.net/public/n0b2o4d6c8
    Code:
    init();
    function init() {
        left = 0;
        right = 140;
        top = 0;
        bottom = 380;
        ball_mc.onEnterFrame = move;
        ball_mc.onPress = drag;
        block_mc.onPress = drag;
        ball_mc.onRelease = ball_mc.onReleaseOutside=noDrag;
        block_mc.onRelease = block_mc.onReleaseOutside=noDrag;
        friction = .98;     // constant drag on momentum
        restitution = .99;  // bounciness
        grav = 1;            // +Y force
        smoosh=1,5 ;
        squishLimit=30;     // larger values=more squish
        jiggle=.25;            // larger values=more jiggle
    }
    
    function drag() {
        this.startDrag();
        this.dragging = true;
    }
    function noDrag() {
        this.stopDrag();
        this.dragging = false;
    }
    
    
    function move() {
        // + JIGGLE
        Xsquishforce+=(100-this._xscale);
        Ysquishforce+=(100-this._yscale);
        
        Xsquishforce*=jiggle;
        Ysquishforce*=jiggle;
        
        this._xscale+=Xsquishforce;
        this._yscale+=Ysquishforce;
        // - JIGGLE
        
        if (!this.dragging) {
            this.velY += grav;
            this.velX *= friction;
            this.velY *= friction;
            this._x += this.velX;
            this._y += this.velY;
            
            if(ball_mc, hitTest(block_mc)) {
            this.velY += grav;
            this.velX *= friction;
            this.velY *= friction;
            this._x += this.velX;
            this._y += this.velY;
    
    
            
            collision=0; // init
    
            if (ball_mc._x>right-(this._width/2)) {
                collision=Math.abs(ball_mc.oldX-this._x);
                if(collision<3){collision=0;}
                
                ball_mc._yscale+=smoosh*collision;
                ball_mc._xscale-=smoosh*collision;
                
                ball_mc._x = right-(ball_mc._width/2);
                ball_mc.velX *= -1*restitution;
    
                 }
                if (ball_mc._x<left+(ball_mc._width/2)) {
                collision=Math.abs(ball_mc.oldX-ball_mc._x);
                if(collision<3){collision=0;}
                
                ball_mc._yscale+=smoosh*collision;
                ball_mc._xscale-=smoosh*collision;
                
                ball_mc._x=left+(ball_mc._width/2);
                ball_mc.velX *= -1*restitution;
            
    
            }
    
            if (this._y>bottom-(this._height/2)) {
                collision=Math.abs(this.oldY-this._y);
                if(collision<3){collision=0;}
                
                this._xscale+=smoosh*collision;
                this._yscale-=smoosh*collision;
                        
                this._y = bottom-(this._height/2);
                this.velY *= -1*restitution;
            
    
                }
            if (this._y<top+(this._height/2)) {
                collision=Math.abs(this.oldY-this._y);
                if(collision<3){collision=0;}
                
                this._xscale+=smoosh*collision;
                this._yscale-=smoosh*collision;
                
                this._y = top+(this._height/2);
                this.velY *= -1*restitution;
            
    
            }
            if(this._xscale<100-squishLimit){this._xscale=100-squishLimit;}
            else if(this._xscale>100+squishLimit){this._xscale=100+squishLimit;}
            
            if(this._yscale<100-squishLimit){this._yscale=100-squishLimit;}
            else if(this._yscale>100+squishLimit){this._yscale=100+squishLimit;}
        } else {
            hit="null";
            this.velX = this._x-this.oldX;
            this.velY = this._y-this.oldY;
            
        }
        this.oldX = this._x;
        this.oldY = this._y;
    
    }
    }

  2. #2
    Member
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    56
    Hey. All you need to do is make a hitTest between the ball and the dragable level movieclip. And if the hitTest is true then you make the fallspeed in your case
    velY into velY = -velY and then

    so it would look somthing like this

    if(ball_mc.hitTest(block_mc)){
    ball_mc.velY = -velY
    }

    or

    if(ball_mc.hitTest(block_mc)){
    this.velY = -velY
    }

    depends where you put it.

  3. #3
    Junior Member
    Join Date
    May 2006
    Posts
    6
    thanks
    but your suggestion is not working?

    I'm stuck whit this...HEEEELP

  4. #4
    Member
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    56
    K, ill try and edit yours so it will do it...

    EDIT: actualy, ill just make 1 from scratch but ill just skip the smoosh part.
    Last edited by georgegowan; 03-14-2007 at 06:55 AM.

  5. #5
    Member
    Join Date
    Oct 2006
    Location
    Australia
    Posts
    56
    http://files-upload.com/129778/ball_bounce.zip.html

    there tis. the only problem (there as always a catch ;D) it only will bounce of the top edge of the platform. ill have a look into bouncing of all edges correctly if you want.

  6. #6
    Banned
    Join Date
    Dec 2006
    Posts
    43
    i have this bidea to discomplicate you

    for the hittest detection in a ball
    you must
    ps. the point in the ball must be in the center

    detect=(floor.hittest(ball._x+ball.width*0.5,ball. _y,true))||(floor.hittest(ball._x-ball.width*0.5,ball._y,true))||(floor.hittest(ball ._y+ball.height*0.5,ball._x,true))||(floor.hittest (ball._y-ball.height*0.5,ball._x,true))

    if(detect){
    //invert speeds of the ball
    }

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