Skip to content


APE – Multiple Draggable Particles

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);
			}
		}
	}
}

Posted in APE - Actionscript Physics Engine, Actionscript 3.


5 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. mike says

    Nice work Morgan – I have tried modifying the ape classes to work with flash cs4/player 10. The new Vector class in CS4 conflicts with the ape vector class. I get stuck on the collisionresolve class. Have you had any luck getting it to work in CS4?

  2. admin says

    Hi Mike, I haven’t had the time to start working with CS4 at the moment unfortunately, but hopefully soon. I would say to publish as v9 if possible.

  3. John says

    Hello,
    try this:
    http://justin.everett-church.com/fp10demos/Modified_APE.zip

    i use this modified version with flash cs4 and it works.

  4. Steve Smith says

    Thanks John. This is great – another of the AS3 physics engines ‘FisixEngine’ also has a Vector class in it which I believe clashes with the new CS4 classes (a list of which can be found here http://tinyurl.com/dfnugj)

  5. Steve Smith says

    Further to my last post I think it is possible to fix your APE source to work in FP10 by doing a find and replace of all occurences of ‘Vector’ using dreamweaver, on your org.cove.ape class directory (should modify about 12 of the classes). replace as ApeVector or something. Then of course rename Vector.as to ApeVector.as. Worked for me.



Some HTML is OK

or, reply to this post via trackback.