Skip to content


[AS 3] AS Physics Engine (APE) – Swinging Platform

I have been wondering how the swinging platform was created, so I built this little demo.

Here is the main Document Class APESwingingPlatform:

package {
	import org.cove.ape.*;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class APESwingingPlatform extends Sprite {
		private var swingingPlatform:SwingingPlatform;

		public function APESwingingPlatform() {
			// 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;

			// gravity -- particles of varying masses are affected the same
			APEngine.addMasslessForce(new Vector(0, 3));

			// groups - this class extends group
			var swingingPlatform:SwingingPlatform = new SwingingPlatform(Math.random() * 0xffffff);
			APEngine.addGroup(swingingPlatform);

			var capsule:Capsule = new Capsule(Math.random() * 0xffffff);
			APEngine.addGroup(capsule);

			capsule.addCollidableList(new Array(swingingPlatform));

			addEventListener(Event.ENTER_FRAME, run);
		}		

		private function run(evt:Event):void {
			APEngine.step();
			APEngine.paint();
		}
	}
}

I used a class that is in the working examples folder of the APE code, Capsule.as:

package {
	import org.cove.ape.*;

	public class Capsule extends Group {

		public function Capsule(colC:uint) {

			var capsuleP1:CircleParticle = new CircleParticle(350,0,14,false,1.3,0.4);
			capsuleP1.setStyle(0, colC, 1, colC);
			addParticle(capsuleP1);

			var capsuleP2:CircleParticle = new CircleParticle(375,0,14,false,1.3,0.4);
			capsuleP2.setStyle(0, colC, 1, colC);
			addParticle(capsuleP2);

			var capsule:SpringConstraint = new SpringConstraint(capsuleP1, capsuleP2, 1, true, 24);
			capsule.setStyle(5, colC, 1, colC, 1);
			addConstraint(capsule);
		}
	}
}

I modified another class to come up with the SwingingPlatform Class:

package {

	import org.cove.ape.*;

	public class SwingingPlatform extends Group {

		public function SwingingPlatform(colE:uint) {	

			// setting collideInternal allows the arm to hit the hidden stoppers.
			// you could also make the stoppers its own group and tell it to collide
			// with the SwingDoor
			collideInternal = true;

			// the tip of the swing
			var swingDoorP1:CircleParticle = new CircleParticle(350,10,7);
			swingDoorP1.mass = 0.001;
			swingDoorP1.setStyle(1, colE, 1, colE);
			addParticle(swingDoorP1);

			// the swing anchor
			var swingDoorP2:CircleParticle = new CircleParticle(200,200,10,true);
			swingDoorP2.setStyle(1, colE, 1, colE);
			addParticle(swingDoorP2);

			// connects the the above
			var swingDoor:SpringConstraint = new SpringConstraint(swingDoorP1, swingDoorP2, 1, true, 13);
			swingDoor.setStyle(2, colE, 1, colE);
			addConstraint(swingDoor);

			var swingDoorAnchor:CircleParticle = new CircleParticle(600,200,2,true);
			swingDoorAnchor.visible = true;
			swingDoorAnchor.collidable = false;
			addParticle(swingDoorAnchor);

			var swingDoorSpring:SpringConstraint = new SpringConstraint(swingDoorP1, swingDoorAnchor, 0.02);
			swingDoorSpring.restLength = 40;
			swingDoorSpring.visible = true;
			addConstraint(swingDoorSpring);

		}
	}
}

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. Davidy says

    Great job,

    But…
    How can we manage to make a “flipper” with this swing ?
    Pleaaaaase, could you make a tuto next time ?
    Best regards.

    David

  2. admin says

    Hi David,

    I am a little confused with what you mean by “flipper” – If you could describe a little more detail I would like to help out!

    Thanks for looking, Morgan

  3. Davidy says

    Hi Morgan,

    Sorry for my bad english…
    I mean a right (or left) flip on a pinball table.

    O-> <-O

    …and by the way, if you can explain on the same tuto : how making a “plunger” with A.P.E (the thing that can launch the ball)

    Cheer from France !

  4. admin says

    Ah, I see what you mean now. I think that is an awesome idea to make a little game.. things are a little busy right now, I am thinking this will be a good thing to do over the next week (maybe I can draw it out this weekend). So I guess.. stay tuned..

  5. Matt Howarth says

    did you ever manage to write a flipper tutorial?

    Brand new to APE, but stunned by it’s potential and fancied starting with a pinball game, but came unstuck very quickly.

    Any assistance gratefully received



Some HTML is OK

or, reply to this post via trackback.