# Ball Thrower

**URL:** <https://forum.kirupa.com/t/ball-thrower/186442>\
**Category:** open source\
**Created:** [April 21, 2006, 8:09am UTC](https://forum.kirupa.com/t/ball-thrower/186442 "2006-04-21T08:09:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 21, 2006, 8:09am UTC](https://forum.kirupa.com/t/ball-thrower/186442/1 "2006-04-21T08:09:58Z")

</div>

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 ;).

```auto

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

---

<div class="post-metadata">

**Author:** ![danulf](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/danulf/32/555_2.png) [@danulf](https://forum.kirupa.com/u/danulf)\
**Post date:** [April 21, 2006, 8:25am UTC](https://forum.kirupa.com/t/ball-thrower/186442/2 "2006-04-21T08:25:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 21, 2006, 8:29am UTC](https://forum.kirupa.com/t/ball-thrower/186442/3 "2006-04-21T08:29:02Z")

</div>

Yeah, an example can be found here:

[http://www.createage.com/experiments/ThrowBall.html](http://www.createage.com/experiments/ThrowBall.html)

Take Care.  
\_Michael

---

<div class="post-metadata">

**Author:** ![phorte](https://avatars.discourse-cdn.com/v4/letter/p/77aa72/32.png) [@phorte](https://forum.kirupa.com/u/phorte)\
**Post date:** [April 21, 2006, 8:34am UTC](https://forum.kirupa.com/t/ball-thrower/186442/4 "2006-04-21T08:34:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 21, 2006, 9:13am UTC](https://forum.kirupa.com/t/ball-thrower/186442/5 "2006-04-21T09:13:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bombsledder](https://avatars.discourse-cdn.com/v4/letter/b/958977/32.png) [@bombsledder](https://forum.kirupa.com/u/bombsledder)\
**Post date:** [April 21, 2006, 10:42am UTC](https://forum.kirupa.com/t/ball-thrower/186442/6 "2006-04-21T10:42:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 21, 2006, 9:27pm UTC](https://forum.kirupa.com/t/ball-thrower/186442/7 "2006-04-21T21:27:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [April 21, 2006, 9:43pm UTC](https://forum.kirupa.com/t/ball-thrower/186442/8 "2006-04-21T21:43:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 21, 2006, 9:49pm UTC](https://forum.kirupa.com/t/ball-thrower/186442/9 "2006-04-21T21:49:55Z")

</div>

wow… it does!!! 🙂

---

<div class="post-metadata">

**Author:** ![vini](https://avatars.discourse-cdn.com/v4/letter/v/c77e96/32.png) [@vini](https://forum.kirupa.com/u/vini)\
**Post date:** [April 22, 2006, 11:57pm UTC](https://forum.kirupa.com/t/ball-thrower/186442/10 "2006-04-22T23:57:03Z")

</div>

Its very very Old… lol

---

<div class="post-metadata">

**Author:** ![MichaelxxOA](https://avatars.discourse-cdn.com/v4/letter/m/9dc877/32.png) [@MichaelxxOA](https://forum.kirupa.com/u/MichaelxxOA)\
**Post date:** [April 23, 2006, 12:09am UTC](https://forum.kirupa.com/t/ball-thrower/186442/11 "2006-04-23T00:09:19Z")

</div>

huh? lol
