Ball Thrower

I found this in my old file archive, figured I’d give it to you guys to mess around with. The code is pretty intuitive as far as naming conventions go… enjoy ;).


var ballCount:Number = 0;

var GRAVITY:Number = 0.98;
var BOUNCE:Number = -0.9;

var MAX_VELOCITY:Number = 5;

var left:Number = 0;
var top:Number = 0;
var right:Number = Stage.width;
var bottom:Number = Stage.height;

var myBall:MovieClip = createBall();

myBall.vx = 0;
myBall.vy = 0;
myBall.radius = myBall._width/2;
myBall.oldX = 0;
myBall.oldY = 0;

myBall.onEnterFrame = fall;
myBall.onPress = throwBall;
myBall.onRelease = myBall.onReleaseOutside = drop;

myBall._x = Math.random() * right - left;
myBall._y = bottom;

function calculateVelocity()
{
    var dx:Number = this._x - this.oldX;
    var dy:Number = this._y - this.oldY;
    
    this.vx = Math.min(MAX_VELOCITY, dx);
    this.vy = Math.min(MAX_VELOCITY, dy);
    
    this.oldX = this._x;
    this.oldY = this._y;
}
function drag()
{
    this._x = _xmouse;
    this._y = _ymouse;
    
    updateAfterEvent();
}
function throwBall()
{
    this.oldX = this._x;
    this.oldY = this._y;
    
    delete this.onEnterFrame;
    this.onEnterFrame = calculateVelocity;
    
    this.onMouseMove = drag;
}
function drop()
{
    delete this.onMouseMove;
    
    this.onEnterFrame = fall;
}

function fall()
{
    this.vy += GRAVITY;
    
    this._y += this.vy;
    this._x += this.vx;
    
    if (this._x - this.radius < left)
    {
        this._x = left + this.radius;
        this.vx *= BOUNCE;
    } else if (this._x + this.radius > right)
    {
        this._x = right - this.radius;
        this.vx *= BOUNCE;
    }
    if (this._y + this.radius > bottom)
    {
        this._y = bottom - this.radius;
        this.vy *= BOUNCE;
    }
}

function createBall() : MovieClip
{
    var mc:MovieClip = this.createEmptyMovieClip("ball" + ballCount, ballCount);
    
    mc.lineStyle(10, 0x0, 100);
    mc.lineTo(0, 1);
    
    ballCount++;
    return mc;
}

I’ll still have fun with this thing, even all this time later. Take Care.
_Michael

could you post a .swf file? I’m at school so I can’t use flash…

Yeah, an example can be found here:

http://www.createage.com/experiments/ThrowBall.html

Take Care.
_Michael

Not bad, i notice there is no friction between the ground and ball, it just keeps rolling after it finishes bouncing. Minor thing though… =D

lol, seriously this thing is so old, I have no intentions on updating it… .feel free to do what you’d like.

nice, could be useful in the game engine im making saves me some time :stuck_out_tongue:

Do as you’d like with this ;)… it’s why I put it up here

This reminds me of the cards falling after someone has won a game of solitaire.

wow… it does!!! :slight_smile:

Its very very Old… lol

huh? lol