Skip to content


APE – Driving Demo – Part III

I fixed a couple issues from the previous demo such fixing the calculated position of the Truck object so one of the wheels will not fall below the movie. I also replaced the CircleParticles at the top positions of the truck with WheelParticles. Mainly because with WheelParticles I can add a weight and a friction parameter that will help create a more rigid vehicle.

Here is the main document class:

package {

	import org.cove.ape.*;

	import flash.display.Stage;
	import flash.display.Sprite;

	import flash.events.Event;
	import flash.ui.Keyboard;
	import flash.events.KeyboardEvent;

	public class Main extends Sprite {
		private var truck:Truck;
		private var floor:Floor;

		private var rightBuild:uint;

		public function Main() {
			rightBuild = 1;

			stage.frameRate = 55;
			addEventListener(Event.ENTER_FRAME, run);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);  

			APEngine.init(1/4);
			APEngine.container = this;
			APEngine.addMasslessForce(new Vector(0,10));

			truck = new Truck();

			getFloor(350,690);

			APEngine.addGroup(truck);
		}

		private function run(evt:Event):void {
			APEngine.step();
			APEngine.paint();
			APEngine.container.x = -truck.px + 350;

			if (truck.px > (700 * rightBuild))
			{
				floor.px = truck.px;

				if (Math.floor(Math.random() * 6) == 1)	floor.BuildJump(truck.px + 300);
				floor.init();

				rightBuild++;

			}
		}

		private function getFloor(xfloor:Number, yfloor:Number):void
		{
			floor = new Floor(xfloor, yfloor);
			APEngine.addGroup(floor);
			truck.addCollidable(floor);
		}

		private function keyPressed(event:KeyboardEvent):void {
            var keySpeed:Number = 0.5;  

            if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.RIGHT)
            {
                truck.speed = keySpeed;
            }  

            if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.LEFT)
            {
                truck.speed = -keySpeed;
            }
        }  

        private function keyReleased(event:KeyboardEvent):void
        {
            truck.speed = 0;
        }
	}
}

The floor class:

package
{
	import flash.display.Sprite;
	import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.net.URLRequest;
	import org.cove.ape.*;

	public class Floor extends Group
	{
		private var floor:RectangleParticle;
		private var floorimage:Loader;

		public function Floor(xFloor:Number=700, yFloor:Number=690)
		{
			floor = new RectangleParticle(xFloor,yFloor,1400,100,0,true, 1, .2,0);
			floor.init();

			floorimage = new Loader();
			floorimage.load(new URLRequest("http://manewc.com/projects/flash/DriveDemo2/Grass.jpg"));

			floor.setDisplay(floorimage, -350, -50);

			addParticle(floor);
		}

		public function BuildJump(jx:Number)
		{
			// jump
			var jumpL:RectangleParticle = new RectangleParticle(jx + 400,625,100,1,-.3,true);
			addParticle(jumpL);
		}

		public function get px():Number
		{
			return floor.px;
		}

		public function set px(value:Number):void
		{

			floor.px = value;
		}

		public function get center():Vector
		{
			return floor.center;
		}
	}
}

The truck class:

package {

	import org.cove.ape.*;
	import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.net.URLRequest;  

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

		private var wheelAimage:Loader;
		private var wheelBimage:Loader;

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

			// Load in the image
            wheelAimage = new Loader();  

            wheelAimage.load(new URLRequest("http://manewc.com/projects/flash/DriveDemo2/wheel.png"));

			wheelParticleA = new WheelParticle(50,300,50,false,.8);

			/*
			============================
			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.setDisplay(wheelAimage, -50, -50);
			addParticle(wheelParticleA);

			// Load in the image
            wheelBimage = new Loader();
            wheelBimage.load(new URLRequest("http://manewc.com/projects/flash/DriveDemo2/wheel.png"));

			wheelParticleB = new WheelParticle(250,300,50,false,.8);

			/*
			============================
			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)
			============================
			*/

			wheelParticleB.setDisplay(wheelBimage, -50, -50);

			addParticle(wheelParticleB);

			// connect the front and rear wheels
			var axelConnector:SpringConstraint = new SpringConstraint(wheelParticleA, wheelParticleB, .7, true, 8);
			axelConnector.setStyle(0, colorOne, 1, colorTwo);
			addConstraint(axelConnector);

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

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

			// connect the top wheels
			var topWheelsConn:SpringConstraint = new SpringConstraint(wheelTL, wheelTR, .7, true, 8);
			topWheelsConn.setStyle(0, 0x336699, 1, 0x336699);
            addConstraint(topWheelsConn);

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

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

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

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

		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.