Hi, i am building bouncing application , here is the document class
package
{
import flash.display.*;
import flash.events.*;
public class BasketBall extends Sprite
{
private var IsMouseDown:Boolean = false;
private var angle:Number;
private var arrow:Arrow;
private var currXpos:Number;
private var currYpos:Number;
private var speed:Number;
private var ball:Ball;
private var xCord:Number;
private var yCord:Number;
public function BasketBall ():void
{
addObjects();
addListeners();
}
private function addObjects():void
{
//create arrow
arrow = new Arrow();
arrow.visible = false;
addChild(arrow);
//create ball
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
ball = new Ball(280,500);
addChild(ball);
}
private function addListeners():void
{
this.addEventListener(MouseEvent.MOUSE_DOWN, startAngleCalculation);
this.addEventListener(MouseEvent.MOUSE_UP, startAngleCalculation);
this.addEventListener(Event.ENTER_FRAME, doEnterFrame);
}
private function startAngleCalculation(eve:MouseEvent):void
{
if (eve.type == MouseEvent.MOUSE_DOWN)
{
IsMouseDown = true;
currXpos = mouseX;
currYpos = mouseY;
//place arrow...
arrow.visible = true;
if (currXpos > 620)
{
currXpos = 620;
}
arrow.x = currXpos;
arrow.y = currYpos;
}
else if (eve.type == MouseEvent.MOUSE_UP)
{
IsMouseDown = false;
arrow.visible = false;
moveBall(xCord,yCord);
}
}
private function doEnterFrame(eve:Event):void
{
//calculate angle
if (IsMouseDown)
{
rotateArrow();
}
checkBounce();
}
private function rotateArrow():void
{
var dx:Number = currXpos - mouseX;
var dy:Number = currYpos - mouseY;
speed = Math.round(Math.sqrt(dx*dx + dy*dy));
var radians:Number = Math.atan2(dy, dx);
//var radians:Number = Math.atan(dy/dx);
var angleValue:Number = Math.floor(radians * 180 / Math.PI);
//Rotate Arrow....
arrow.rotation = angleValue;
if (speed > 150)
{
speed = 150;
}
arrow.bar.width = speed;
//trace (Math.round(Math.abs(Math.sin(angleValue)*speed)));
xCord = Math.round(Math.abs(Math.sin(angleValue)*speed));
yCord = Math.round(Math.abs(Math.cos(angleValue)*speed));
}
private function moveBall(xc:Number, yc:Number):void
{
//trace (xc +" : "+ yc);
ball.xSpeed = xc/3;
ball.ySpeed = -yc/3;
}
private function checkBounce():void
{
if ((ball.x + ball.width/2) > stage.stageWidth || (ball.x - ball.width/2) < 0 )
{
ball.xSpeed *= -1;
}
if (ball.hitTestObject(ground_mc))
{
ball.ySpeed *= -1;
ball.bounces++
}
if (ball.hitTestObject(basket_mc.pole))
{
ball.xSpeed *= -1;
if (ball.y > basket_mc.y)
{
ball.ySpeed *= -1;
ball.bounces++
}
}
if (ball.hitTestObject(basket_mc.basket_area))
{
if ((ball.y+ball.width/2) > basket_mc.y)
{
ball.xSpeed = 1;
}
}
}
}
}
Here is the class for the ball
package {
import flash.display.*;
import flash.events.*;
public class Ball extends MovieClip {
private var XS:Number = 0;
private var YS:Number = 0;
public var maxSpeed:Number = 10;
public var gravity:Number = 0.98;
public var bounces:Number = 0;
public var NumOfBounce:Number = 7;
public var friction:Number = 0.97;
private var stageRef:Stage;
private var originX:Number;
private var originY:Number;
//Adjust this to increase or decrease the
//number of bounces
var decay:Number = 0.6;
public function Ball(xPos:Number, yPos:Number)
{
//start position
this.x = xPos;
this.y = yPos;
//set the orignal position of ball
originX = this.x;
originY = this.y;
this.addEventListener(Event.ADDED_TO_STAGE, doAddedToStage);
}
private function doAddedToStage(eve:Event):void
{
stageRef = this.stage;
init();
addListeners();
}
//setters and getters for public variable
public function set xSpeed (speed:Number):void
{
XS = speed;
}
public function get xSpeed ():Number
{
return XS;
}
public function set ySpeed (speed:Number):void
{
YS = speed;
}
public function get ySpeed ():Number
{
return YS;
}
private function addListeners():void
{
this.addEventListener(Event.ENTER_FRAME, doEnterFrame);
}
private function doEnterFrame(eve:Event):void
{
if (XS != 0)
{
YS += gravity;
XS *= friction;
YS *= friction;
}
if (bounces >= NumOfBounce)
{
friction = 0.001;
resetPosition();
}
this.x += XS;
this.y += YS;
}
private function resetPosition():void
{
//reset default position;
this.x = originX;
this.y = originY;
//reset friction and XS and YS
friction = 0.97;
XS = 0;
YS = 0;
//reset bounces back to 0
bounces = 0;
}
}
}
Now the problem is that the “ball” clip some times does not bounce properly and it is also not getting right angle… what could be wrong…!