I decided to start cleaning up some issues that our code currently posseses. One being that rectangles can be drawn in any area of the stage, so I limited it so if the user scrolls within the area of the buttons that a rectangle will not be drawn. The second issue is that the more circles you add to the movie, the more processor intensive the movie becomes – so we need a way to essentially clean the stage. So with a few more buttons, the updated movie is below.
The Main Document Class:
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.Rectangle;
import fl.controls.Button;
import org.cove.ape.*;
public class Main extends Sprite {
private var beginX:Number; // top left x position
private var beginY:Number; // top left y position
private var endX:Number; // bottom right x position
private var endY:Number; // bottom right y position
private var dynRect:RectangleParticle;
private var circle:CircleParticle;
private var circleGroup:Group;
private var defaultGroup:Group;
public function Main()
{
init();
}
private function init():void
{
stage.frameRate = 55;
// Initialize the engine. The argument here is the time step value.
// Higher values scale the forces in the sim, making it appear to run
// faster or slower. Lower values result in more accurate simulations.
APEngine.init(1/4);
// set up the default diplay container
APEngine.container = this;
APEngine.addMasslessForce(new Vector(0,8));
defaultGroup = new Group();
defaultGroup.collideInternal = true;
circleGroup = new Group();
circleGroup.collideInternal = true;
var rect:RectangleParticle = new RectangleParticle(345, 680, 710, 40, 0, true);
defaultGroup.addParticle(rect);
APEngine.addGroup(defaultGroup);
APEngine.addGroup(circleGroup);
// make the groups collidable
defaultGroup.addCollidable(circleGroup);
addEventListener(Event.ENTER_FRAME, run);
stage.addEventListener(MouseEvent.MOUSE_DOWN, beginRectDraw);
stage.addEventListener(MouseEvent.MOUSE_UP, stopRectDraw);
// build the UI
buildUI();
}
private function beginRectDraw(m:MouseEvent):void
{
beginX = mouseX;
beginY = mouseY;
}
private function stopRectDraw(m:MouseEvent):void
{
// set the end points
endX = mouseX;
endY = mouseY;
// remove the listeners
stage.addEventListener(MouseEvent.MOUSE_DOWN, beginRectDraw);
stage.addEventListener(MouseEvent.MOUSE_UP, stopRectDraw);
// now draw the rectangle
drawRectangle();
}
private function drawRectangle():void
{
if ( beginY < 660 && endY < 660 )
{
dynRect = new RectangleParticle(beginX + ( (endX - beginX)/2 ), beginY + ((endY - beginY)/2), endX - beginX, endY - beginY, 0, true);
defaultGroup.addParticle(dynRect);
}
}
private function addBall( m:MouseEvent ):void
{
circle = new CircleParticle(Math.random() * stage.stageWidth, Math.random() * 100, Math.random() * 25 + 5);
circleGroup.addParticle(circle);
}
private function run(e:Event):void
{
APEngine.step();
APEngine.paint();
}
private function removeLastBall( m:MouseEvent ):void
{
if ( circle ) circle.cleanup();
}
private function clearBoard( m:MouseEvent ):void
{
APEngine.removeGroup(defaultGroup);
APEngine.removeGroup(circleGroup);
init();
}
private function buildUI():void
{
// NEW BUTTONS
var btnAddBall:Button = new Button();
btnAddBall.width = 65;
btnAddBall.height = 25;
btnAddBall.move ( 5, stage.stageHeight - 20 - btnAddBall.height / 2 );
btnAddBall.label = "Add Ball";
addChild(btnAddBall);
btnAddBall.addEventListener(MouseEvent.CLICK, addBall);
var btnRemoveBall:Button = new Button();
btnRemoveBall.width = 120;
btnRemoveBall.height = 25;
btnRemoveBall.move ( 75, stage.stageHeight - 20 - btnRemoveBall.height / 2 );
btnRemoveBall.label = "Remove Last Ball";
addChild(btnRemoveBall);
btnRemoveBall.addEventListener(MouseEvent.CLICK, removeLastBall);
var btnClearBoard:Button = new Button();
btnClearBoard.width = 120;
btnClearBoard.height = 25;
btnClearBoard.move ( 575, stage.stageHeight - 20 - btnClearBoard.height / 2 );
btnClearBoard.label = "Clear Board";
addChild(btnClearBoard);
btnClearBoard.addEventListener(MouseEvent.CLICK, clearBoard);
}
}
}
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.