# Bounce and randome velocity

**URL:** <https://forum.kirupa.com/t/bounce-and-randome-velocity/261428>\
**Category:** flash\
**Created:** [May 27, 2008, 3:33pm UTC](https://forum.kirupa.com/t/bounce-and-randome-velocity/261428 "2008-05-27T15:33:42Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![nater](https://avatars.discourse-cdn.com/v4/letter/n/35a633/32.png) [@nater](https://forum.kirupa.com/u/nater)\
**Post date:** [May 27, 2008, 3:33pm UTC](https://forum.kirupa.com/t/bounce-and-randome-velocity/261428/1 "2008-05-27T15:33:42Z")

</div>

Hi everyone,  
I am trying to make a simple game with a bouncing movie clip. the function is initialized else where. I have hit a few road blocks and i cannot seem to get the item to: go in random directions or bounce when it reaches the sides of the stage.

I think i need to incorperate another vector into the movement but im not sure how.

All help is welcome!  
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

var radius:Number =10;  
var vx:Number = (Math.random()\*8-4);  
var vy:Number = (Math.random()\*8-4);  
var bounce:Number = -1;

function onLoop(event:Event):void {  
spermA.x += vx;  
spermA.y += vy;  
var \_vx = spermA.x + vx;  
var \_vy = spermA.y + vy;

```
if (spermA.x + spermA.radius &gt; stage.stageWidth) {
    spermA.x = stage.stageWidth - radius;
    _vx *= bounce;
    trace(sperm.A.x);
} else if (spermA.x - radius &lt; 0) {
    spermA.x = radius;
    _vx *= bounce;
}
if (spermA.y + spermA.radius &gt; stage.stageHeight) {
    spermA.y = stage.stageHeight - radius;
    _vy *= bounce;
} else if (spermA.y - radius &lt; 0) {
    spermA.y = radius;
    _vy *= bounce;
}

```

}

////////////////////////////////////////////////////////////////////////////////////////////////  
Thanks!
