# Bounce not Working

**URL:** <https://forum.kirupa.com/t/bounce-not-working/290906>\
**Category:** flash\
**Created:** [June 22, 2009, 6:08am UTC](https://forum.kirupa.com/t/bounce-not-working/290906 "2009-06-22T06:08:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sumitjaswal](https://avatars.discourse-cdn.com/v4/letter/s/e56c9b/32.png) [@sumitjaswal](https://forum.kirupa.com/u/sumitjaswal)\
**Post date:** [June 22, 2009, 6:08am UTC](https://forum.kirupa.com/t/bounce-not-working/290906/1 "2009-06-22T06:08:11Z")

</div>

Hello there

I have recently started Learning Actionscript 3.0 out of my own interest.  
I was trying on making a ball bounce off the edges of the stage using the Actionscript 3.0. But the code not working.

The Ball happens to move on X and Y, till it hits the end of the stage, and then it just does not reverse its direction to give the effect of bounce, it gets stuck to the X axis and moves out of the stage.

I tried to make changes, but failed.  
If some can tell me what may be the error, coz i am really not able to figure it out.  
Thanks a lot in Advance

Here is the code…

[FONT=Courier New]------------------Code-------------------[/FONT]  
[FONT=Courier New][COLOR=blue]function bounceBall(evt:Event):void {[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]var vx:Number=Math.random()\*10+5;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]var vy:Number=Math.random()\*10+5;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]var radius:Number=mcBall.height/2;[/COLOR][/FONT]

[FONT=Courier New][COLOR=blue]var top:Number=0;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]var bottom:Number=stage.stageHeight;[/COLOR][/FONT]

[FONT=Courier New][COLOR=blue]var left:Number=0;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]var right:Number=stage.stageWidth;[/COLOR][/FONT]

[FONT=Courier New][COLOR=blue]var bounce:Number=0.7;[/COLOR][/FONT]

[FONT=Courier New][COLOR=blue]mcBall.x+=vx;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]mcBall.y+=vy;[/COLOR][/FONT]

[FONT=Courier New][COLOR=blue]if (mcBall.y+radius\>bottom) {[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]mcBall.y=bottom-radius;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]vy\*=-1 \* bounce;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]} else if (mcBall.y-radius\<top) {[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]mcBall.y=top+radius;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]vy\*=-1\* bounce;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]}[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]if (mcBall.x+mcBall.radius\>right) {[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]mcBall.x=right-mcBall.radius;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]vx\*=-1\* bounce;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]} else if (mcBall.x - mcBall.radius \< left) {[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]mcBall.x=left+mcBall.radius;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]vx\*=-1\* bounce;[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]}[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]}[/COLOR][/FONT]  
[FONT=Courier New][COLOR=blue]mcBall.addEventListener(Event.ENTER\_FRAME, bounceBall);[/COLOR][/FONT]  
[FONT=Courier New]------------------Code-------------------[/FONT]
