# Changing ball direction

**URL:** https://forum.kirupa.com/t/changing-ball-direction/282519
**Category:** Uncategorized
**Created:** [February 23, 2009, 4:36am UTC](https://forum.kirupa.com/t/changing-ball-direction/282519 "2009-02-23T04:36:06Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Bubloobasu](https://avatars.discourse-cdn.com/v4/letter/b/b9bd4f/32.png) [@Bubloobasu](https://forum.kirupa.com/u/Bubloobasu)
#### Post date: [February 23, 2009, 4:36am UTC](https://forum.kirupa.com/t/changing-ball-direction/282519/1 "2009-02-23T04:36:06Z")

</div>

How to change the **[COLOR=#ff0000]direction[/COLOR]** of the **[COLOR=#ff0000]ball[/COLOR]** when it hits the slanting line…  
Like in mini golf game, the **[COLOR=#ff0000]ball[/COLOR]** should move in some **[COLOR=#ff0000]direction[/COLOR]** when it hits the wall…  
i did it, but it works well for rectangle like shape,  
but for diagonal its not working ie. the **[COLOR=#ff0000]ball[/COLOR]** goes away in one **[COLOR=#ff0000]direction[/COLOR]**…  
i need to change the **[COLOR=#ff0000]ball[/COLOR]** **[COLOR=#ff0000]direction[/COLOR]** randomly but it should not go away from the wall…

package  
{  
import flash.display.Sprite;  
import flash.display.MovieClip;  
import flash.events.Event;  
import flash.events.KeyboardEvent;  
import flash.events.MouseEvent;  
import flash.geom.Rectangle;

public class ballMove extends MovieClip  
{  
private var dx:Number;  
private var dy:Number;  
private var dist:Number;  
private var vx:Number;  
private var vy:Number;  
private var br:Number;  
private var ballrolling:Boolean = false;  
private var friction:Number = 0.98;  
private var power:Number = 0.05;  
private var hitAccuracy:Number = 5;  
private var hitdistance:Number = 5;  
private var xpos:Number;  
private var ypos:Number;  
private var angle:Number;  
private var temppower:Number;  
private var bounceAngle:Number;  
private var bounceSpeed:Number;  
private var container\_mc:container;  
private var wall1Rect:Rectangle;  
private var wall2Rect:Rectangle;  
private var wall3Rect:Rectangle;  
private var wall4Rect:Rectangle;  
private var ballRect:Rectangle;  
public function ballMove()  
{

container\_mc= new container();  
addChild(container\_mc);  
container\_mc.x=400;  
container\_mc.y=300;

br= container\_mc.ball.width / 2;

container\_mc.addEventListener(Event.ENTER\_FRAME, onEnter)  
container\_mc.addEventListener(MouseEvent.MOUSE\_UP, mouseup)

}

private function onEnter(e:Event)  
{

if (ballrolling)  
{

container\_mc.ball.x += vx;  
container\_mc.ball.y += vy;  
//  
vx \*= friction;  
vy \*= friction;  
//

if(container\_mc.wall1.hitTestPoint((container\_mc.b all.x+400),(container\_mc.ball.y+300),true))  
{  
container\_mc.ball.x = container\_mc.ball.x + friction;  
vx \*= -1;  
}  
else if(container\_mc.wall2.hitTestPoint((container\_mc.b all.x+400),(container\_mc.ball.y+300),true))  
{  
container\_mc.ball.x = container\_mc.ball.x - friction;  
vx \*= -1;  
}  
else if(container\_mc.wall3.hitTestPoint((container\_mc.b all.x+400),(container\_mc.ball.y+300),true))  
{  
container\_mc.ball.y = container\_mc.ball.y + friction;  
vy \*= -1;  
}  
else if(container\_mc.wall4.hitTestPoint((container\_mc.b all.x+400),(container\_mc.ball.y+300),true))  
{  
container\_mc.ball.y = container\_mc.ball.y - friction;  
vy \*= -1;  
}

if (vx \< 0.3 && vy \< 0.3 && vx \> -0.3 && vy \> -0.3)  
{  
ballrolling = false;  
}

}  
}  
private function mouseup(e:Event)  
{  
if (!ballrolling)  
{  
ballrolling = true;  
rollBall();  
}  
}

private function rollBall()  
{  
dx = mouseX - (container\_mc.ball.x+400);  
dy = mouseY - (container\_mc.ball.y+300);  
temppower = Math.sqrt((dx \* dx) + (dy \* dy)) \* power;  
angle = Math.atan2(dy, dx);  
vx = -1 \* Math.cos(angle) \* temppower;  
vy = -1 \* Math.sin(angle) \* temppower;  
}  
}  
}

Thanks 4 help:angel:
