Here I have created a circle particle that can be dragged. The issue is what to do when the MOUSE_UP event is called as the circle particle goes back to it’s final position. We’ll work on that in the future.
Here is the Main.as Document Class file:
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:CircleParticle;
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, 2, 0, true);
defaultGroup.addParticle(rect);
circle = new CircleParticle(350, 150, 25);
defaultGroup.addParticle(circle);
APEngine.addGroup(defaultGroup);
addEventListener(Event.ENTER_FRAME, run);
circle.sprite.addEventListener(MouseEvent.MOUSE_DOWN, beginDrag);
circle.sprite.addEventListener(MouseEvent.MOUSE_UP, releaseDrag);
}
private function run(e:Event):void
{
APEngine.step();
APEngine.paint();
}
private function beginDrag(m:Event):void
{
removeEventListener(Event.ENTER_FRAME, run);
circle.sprite.startDrag();
}
private function releaseDrag(m:Event):void
{
circle.sprite.stopDrag();
addEventListener(Event.ENTER_FRAME, run);
}
}
}
I love the simplicity of APE, very much so compared to Box2d. But the dragging and throwing of particles built into Box2d make it very fun to play with as well. It’s just quite confusing to set up anything.
Thanks for your APE posts, keep them coming.