Skip to content


APE – Driving Demo – Part IV

The only change for today was to apply a buggy image to my truck class (I’ll keep the class name of Truck.. even though it is a Buggy for now), so I will just post the Truck class code as this is the only thing that got updated. I had an issue with applying an image to a SpringConstraint object, but with a deeper look at the SpringConstraint class I noticed that when I set the collideable parameter to false on the SpringConstraint I was then allowed to apply the image via the setDisplay method. Because of this I added a few more wheel particles to surround my image to create a more collidable effect.. Note that this is still not working properly as the image will contort to unrealistic positions. So, for now:

Here is the updated Truck.as file:

package {

	import org.cove.ape.*;

	import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.net.URLRequest;
	import flash.display.MovieClip;

	public class Truck extends Group
	{
		private var wheelParticleA:WheelParticle;
		private var wheelParticleB:WheelParticle;
		private var wheelTL:WheelParticle;
		private var wheelTR:WheelParticle;

		private var wheelimage:Loader;
		private var wheelimage2:Loader;
		private var buggyimage:Loader;
		private var holder:MovieClip;

		public function Truck(colorOne:uint=0x333333, colorTwo:uint=0x333333) {

			// Load in the images
            wheelimage = new Loader();
			wheelimage.load(new URLRequest("http://manewc.com/projects/flash/i/wheel.png"));
			wheelimage2 = new Loader();
			wheelimage2.load(new URLRequest("http://manewc.com/projects/flash/i/wheel.png"));

			buggyimage = new Loader();
			buggyimage.load(new URLRequest("http://manewc.com/projects/flash/i/buggy2.png"));

			/*
			============================
			Assigns a DisplayObject to be used when painting this particle.
			Parameters

				d:DisplayObject
				offsetX:Number (default = 0)
 				offsetY:Number (default = 0)
 				rotation:Number (default = 0)
			============================
			*/
			wheelParticleA = new WheelParticle(50,300,50,false,.8);
			wheelParticleA.setDisplay(wheelimage, -50, -50);
			addParticle( wheelParticleA );

			wheelParticleB = new WheelParticle(250,300,50,false,.8);
			wheelParticleB.setDisplay(wheelimage2, -50, -50);
			addParticle( wheelParticleB );

			// connect the front and rear wheels
			var axelConnector:SpringConstraint = new SpringConstraint(wheelParticleA, wheelParticleB, .7, true, 8);
			axelConnector.visible = false;
			addConstraint( axelConnector );

			// add the top left wheel
			wheelTL = new WheelParticle(0,220,10,false,.5,.3,.7);
			addParticle( wheelTL );

			// add the top right wheel
			wheelTR = new WheelParticle(300,220,10,false,.5,.3,.7);
			addParticle( wheelTR );

			// connect the top wheels
			var topWheelsConn:SpringConstraint = new SpringConstraint(wheelTL, wheelTR, .7, false, 8);
			topWheelsConn.setDisplay(buggyimage, -150, -70);
            addConstraint( topWheelsConn );

			// connect top left wheel to bottom left wheel
			var leftWheelsConn:SpringConstraint = new SpringConstraint(wheelTL,wheelParticleA,.7,true, 8);
			leftWheelsConn.visible = false;
			addConstraint ( leftWheelsConn );

			// connect top right wheel to bottom right wheel
			var rightWheelsConn:SpringConstraint = new SpringConstraint(wheelTR,wheelParticleB,.7,true, 8);
			rightWheelsConn.visible = false;
			addConstraint ( rightWheelsConn );

			// connect top left wheel to bottom right wheel
			var xLeftConn:SpringConstraint = new SpringConstraint(wheelTL,wheelParticleB,.7,true, 8);
			xLeftConn.visible = false;
			addConstraint ( xLeftConn );

			// connect top right wheel to bottom left wheel
			var xRightConn:SpringConstraint = new SpringConstraint(wheelTR,wheelParticleA,.7,true, 8);
			xRightConn.visible = false;
			addConstraint ( xRightConn );

			// stabilizers
			var stableL:WheelParticle = new WheelParticle(50,190,10,false,.3);
			addParticle ( stableL );

			var stableR:WheelParticle = new WheelParticle(180,200,10,false,.3);
			addParticle ( stableR );

			var stableConn:SpringConstraint = new SpringConstraint(stableL,stableR,.7,true, 8);
			stableConn.visible = false;
			addConstraint ( stableConn );

			var stableConnL:SpringConstraint = new SpringConstraint(stableL,wheelTL,.7,true, 8);
			stableConnL.visible = false;
			addConstraint ( stableConnL );

			var stableConnL2:SpringConstraint = new SpringConstraint(stableL,wheelParticleA,.7,true, 8);
			stableConnL2.visible = false;
			addConstraint ( stableConnL2 );

			var stableConnR:SpringConstraint = new SpringConstraint(stableR,wheelTR,.7,true, 8);
			stableConnR.visible = false;
			addConstraint ( stableConnR );

			var stableConnR2:SpringConstraint = new SpringConstraint(stableR,wheelParticleB,.7,true, 8);
			stableConnR2.visible = false;
			addConstraint ( stableConnR2 );
		}

		public function set speed(s:Number):void {
			wheelParticleA.angularVelocity = s;
			wheelParticleB.angularVelocity = s;
		}

		public function get px():Number {
			if ( wheelParticleB.px > wheelParticleA.px )
			{
				return wheelParticleB.px;
			}
			else
			{
				return wheelParticleA.px;
			}
		}

	}
}

Posted in APE - Actionscript Physics Engine, Actionscript 3.


0 Responses

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



Some HTML is OK

or, reply to this post via trackback.