XHanch Studio Log in | Register | Cart

Forum

Notifications
Clear all

[Free script] Walled bouncing ball with ActionScript

1 Posts
1 Users
0 Likes
2,203 Views
XHanch
(@xhanch-alt)
Posts: 2105
Member Admin
Topic starter
 

This Flash script will result in animation of a ball that will be bounced when it hit a barrier/wall. The area is a rectangle so that the ball will keep bouncing inside the rectangle. You can even move the ball. Click on it and hold to stop the bouncing and then release the mouse to continue bouncing. What we need to produce the animation is just a movie clip an a simple script.

Demo preview

http://xhanch.com/gallery/action-script/walled-bouncing-ball.swf

Source code

//By    : Xhanch Studio
//URL  : http://xhanch.com/action-script-walled-bouncing-ball/

_animate = true;
//Horizontal Speed
_vx = 2;
//Vertical Speed
_vy = 2;

//Bouncing Area
//Left
_bl = 50;
//Right
_br = 500;
//Top
_bt = 50;
//Bottom
_bb = 350;

this.onEnterFrame = function(){
    if (_animate){
        //Set the new ball position
        mc_ball._y += _vy;
        mc_ball._x += _vx;

        //Area limitation checker
        if (mc_ball._y > _bb)
            _vy = Math.abs(_vy)*-1;
        else if (mc_ball._y < _bt)
            _vy = Math.abs(_vy);

        if (mc_ball._x > _br)
            _vx = Math.abs(_vx)*-1;
        else if (mc_ball._x < _bl)
            _vx = Math.abs(_vx);
    }
}

//On press event, the ball will stop bouncing and drag mode is on
mc_ball.onPress = function(){
    _animate = false;
    mc_ball.startDrag(true);
}

//On release event, the ball will start bouncing and drag mode is off
mc_ball.onRelease = function(){
    _animate = true;
    mc_ball.stopDrag();
}

Download

http://xhanch.com/gallery/action-script/walled-bouncing-ball.zip

 
Posted : 14/04/2011 8:17 pm
Share:

× Close Menu