Skip to content

Instantly share code, notes, and snippets.

@lamberta
Created April 16, 2010 03:58
Show Gist options
  • Save lamberta/367984 to your computer and use it in GitHub Desktop.
Save lamberta/367984 to your computer and use it in GitHub Desktop.
BouncingBall AS3Flex demo
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
width="450" height="350"
layout="vertical"
verticalAlign="middle"
horizontalAlign="center"
verticalScrollPolicy="off"
horizontalScrollPolicy="off"
applicationComplete="{initApp()}">
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
import classes.BouncingBall;
private function initApp():void {
var bouncingBall:UIComponent = new UIComponent();
bouncingBall.addChild(new BouncingBall(myCanvas.width, myCanvas.height));
myCanvas.addChild(bouncingBall);
}
]]>
</mx:Script>
<mx:Panel title="AS3 Class in Flex!" layout="vertical">
<mx:Canvas id="myCanvas"
height="275" width="375"
scrollRect="{new Rectangle(0, 0, myCanvas.width, myCanvas.height)}"/>
</mx:Panel>
</mx:Application>
/* classes/Ball.as
*/
package classes {
import flash.display.Sprite;
public class Ball extends Sprite {
public var radius:Number;
private var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 1;
public function Ball(radius:Number=40, color:uint=0xff0000) {
this.radius = radius;
this.color = color;
init();
}
public function init():void {
graphics.beginFill(color);
graphics.drawCircle(0, 0, radius);
graphics.endFill();
}
}
}
/* classes/BouncingBall.as
*/
package classes {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
public class BouncingBall extends Sprite {
private var ball:Ball;
private var vx:Number;
private var vy:Number;
private var bounce:Number = -0.7;
private var gravity:Number = .5;
private var oldX:Number; //for tracking throw velocity
private var oldY:Number;
private var canvasWidth:Number;
private var canvasHeight:Number;
public function BouncingBall(width:Number, height:Number) {
canvasWidth = width;
canvasHeight = height;
init();
}
private function init():void {
ball = new Ball();
ball.x = canvasWidth / 2; //initial placement
ball.y = canvasHeight / 2;
vx = Math.random() * 10 - 5; //initial velocity
vy = -10;
addChild(ball);
//listen for drag
ball.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
//render frames
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
vy += gravity; //apply velocities
ball.x += vx;
ball.y += vy;
//set boundries
var left:Number = 0;
var right:Number = canvasWidth;
var top:Number = 0;
var bottom:Number = canvasHeight;
//check if ball hits boundry, apply negative force and subtract bounce
if(ball.x + ball.radius > right) {
ball.x = right - ball.radius; //move off wall first
vx *= bounce; //apply bounce dampening, turn negative
}
else if(ball.x - ball.radius < left) {
ball.x = left + ball.radius;
vx *= bounce;
}
if(ball.y + ball.radius > bottom) {
ball.y = bottom - ball.radius;
vy *= bounce;
}
else if(ball.y - ball.radius < top) {
ball.y = top + ball.radius;
vy *= bounce;
}
}
private function onMouseDown(event:MouseEvent):void {
oldX = ball.x; //remember old position
oldY = ball.y;
//listen for release
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
ball.startDrag();
//stops main rendering code
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
//start tracking velocity every frame
addEventListener(Event.ENTER_FRAME, trackVelocity);
}
private function onMouseUp(event:MouseEvent):void {
//remove listeners and stop dragging
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
ball.stopDrag();
removeEventListener(Event.ENTER_FRAME, trackVelocity);
//start up rendering code again
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function trackVelocity(event:Event):void { //for every frame
/** calculating dragging velocity is almost the opposite of applying
velocity. Instead of adding velocities, we subtract to find how
many pixels it's moved per frame.
I found it's a little fast without a modifier. */
vx = (ball.x - oldX) * 0.6; //subtract old position from current position
vy = (ball.y - oldY) * 0.6;
oldX = ball.x; //make current position the old one
oldY = ball.y;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment