I was trying to figure out a way to improve my initial movie that stop and starts the APE engine to allow for the user to drag an object. After mucking around with the original APE classes, I found in Google Groups that there are 2 new classes to allow the use to drag either a CircleParticle or a RectangleParticle.
You can grab the zip file here: http://ape-general.googlegroups.com/web/dragable_particles.zip and here is what can be produced:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import org.cove.ape.*;
public class Main extends Sprite {
private var circle:DragableCircleParticle;
private var circle2:DragableCircleParticle;
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));
var defaultGroup:Group = new Group();
defaultGroup.collideInternal = true;
var rect:RectangleParticle = new RectangleParticle(350, 348, 700, 20, 0, true);
defaultGroup.addParticle(rect);
circle = new DragableCircleParticle(350, 150, 25);
defaultGroup.addParticle(circle);
circle2 = new DragableCircleParticle(300, 150, 25);
defaultGroup.addParticle(circle2);
APEngine.addGroup(defaultGroup);
addEventListener(Event.ENTER_FRAME, run);
}
private function run(e:Event):void
{
APEngine.step();
APEngine.paint();
}
}
}
Here is the new class file located in org/cove/ape/DragableCircleParticle.as (relative to my Main.as file)
package org.cove.ape{
import flash.events.MouseEvent;
import org.cove.ape.*;
public class DragableCircleParticle extends CircleParticle{
private var mouseIsDown:Boolean;
public function DragableCircleParticle(
x:Number,
y:Number,
radius:Number,
fixed:Boolean = false,
mass:Number = 1,
elasticity:Number = 0.3,
friction:Number = 0){
super(x, y, radius, fixed, mass, elasticity, friction);
alwaysRepaint = fixed;
mouseIsDown = false;
sprite.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
sprite.stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
sprite.stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
}
private function mouseDownHandler(evt:MouseEvent):void{
mouseIsDown = true; // on mouse down set mouseIsDown to true
curr.setTo(evt.stageX,evt.stageY); // set the current position of the particle to the position of the mouse
prev.setTo(evt.stageX,evt.stageY); // set the previous position of the particle to the position of the mouse
}
private function mouseUpHandler(evt:MouseEvent):void{
mouseIsDown = false; // on mouse up set mouseIsDown to false
}
private function mouseMoveHandler(evt:MouseEvent):void{
if(mouseIsDown){ // On mouse move if the mouse is down
prev.copy(curr); // set the previous position to the curent position of the particle
curr.setTo(evt.stageX,evt.stageY); // set the current position to the position of the mouse
}
}
public override function update(dt2:Number):void {
if(!mouseIsDown){ // Don't update if the mouse is down
super.update(dt2);
}
}
}
}
Recent Comments