Which class to use for your game objects?

An overview over the most commonly used FunGrid game object classes:

If the game object is not going to change its position it’s best to use the HardCoreGameObject.as.
This class basiclly has a DisplayObject, a position (Point) and a scrollFactor (Point).
The scrollFactor is usually (1,1), meaning that when we move the viewport the displayObject on screen will move the same distance.
Values below 1 will move slower than the viewport, and is commonly used for slower moving backgrounds to make a parallax scrolling effect. A value of zero will not move the displayObject on screen when the viewport moves. A backgound gradient sky will for instance often have the scrollFactor.x set to 0 to make it only move vertically.

Extends HardCoreGameObject.as, adding the properties for speed (Point) and acceleration (Point).
Speed is measured in pixels pr frame, and accelration is mesured in change of speed pr frame.

Extends CoreGameObjects, adding properties for repeatSettings, rotationSpeed, rotationAcceleration, and possibilities for depth sorting. Setting the repeatSettings makes the game object repeat itself on the x and/or y axis, in a given interval. It’s nice if you want to keep the number of game objects as low as possible.
Unless you care very much about performace it is ok to use GameObject instead of Core- or HardCoreGameOject, even if you don’t use the GameObject specific features.

Extends CoreGameObjects. This class is useful for game objects with a limited life span. You can make them destroy themselves after a certain number of frames, or when they leave the viewport or another specific rectangle.

Extends GameObject. This objects has a polyLine, consisting of 2 more Points. PhysicsGameObjects will be able to land and move along the polyLine. You can use the polyLineCollision(…) method in the FunGrid class to check if a given line is intersecting any PolyLineGameObject. One common use of this class is to make platforms in a platform game.

Extends GameObject. Has a polyLine with 3 or more Points defining a zone. You can use the zonesContainsPoint(…) method in the FunGrid class to ckeck if a point is inside any ZoneGameObject.

Extends RectangleGameObject. Has a rectangle zone. You can use the rectZonesIntersectsRectangle(…) method in the FunGrid class to ckeck if a rectangle is instersecting any RectangleZoneGameObject.

Extends GameObject. This object can be free falling using speed/acceleration inherited from CoreGameObject, or it could be grounded to a PolyLineGameObject (plgo) if it lands on one (or is assigned to one). The parameter value defines where on the plgo it is located. When connected to a PolyLineGameObject it can along the polyline using parameterSpeed and parameterAcceleration.

It’s very common to write your own classes extending from the classes above, to get exactly the functionality you want.

Grind Your Way – A Game Made with FunGrid

The game Grind Your Way was just released.
Grind your way is an advergame developed for ProffPartner. ProffParnter sells tools, machinery, supplies and safety equipment for the professional market. The purpose of the game is to promote ProffPartners scholarship and products for students in vocational schools.
The game is in Norwegian, but it should be easily playable for English speaking people as well. Use ARROW KEYS to move the man or the grinder and hold down SPACE to grind!

In the game you are controlling a man with a angle grinder, and the goal is to grind down some heavy machinery as fast as possible.
The man is controlled by a class that extends PhysicsGameObject.as and using the KeyController.as class to handle keyboard inputs.

The barrels, boxes and machinery to grind is calculated by Box2D via FunGridBox2DManager.as and those objects extends Box2DGameObject.as.

The 1200 simultaneous sparks from the angle grinder extends BlitParticleGameObject.as and are blitted in a separate bitmap. The rest of the graphics are MovieClips and Bitmaps on the display list.

Exploding parts of the objects extends ParticleGameObject.as.

All sounds and music are controlled by the FunGrid EasySoundManager.as.

The character in Grind Your Way has previously been used in the platform action game
Handy Man, also a FunGrid game.

Scene from Handy Man

Simple Physics and Keyboard Input Tutorial

This demo/tutorial is an example on how you can move a game object with the arrow keys, jumping from plaform to platform.
The brick platforms (PolyLineGameObjects) are repeating themselves on the grid, so we only use 3 platforms to create an infinite playing area.
The moving planet is also a platform, defined by a 4 point PolyLine.

Get Adobe Flash player

Click inside the demo above, and use arrow keys to move the monster!

Download the files here! (FunGrid Full Package)

Assets in the PhysicsAndKeyboard.fla:

Check out the code below, and play around with it for yourself!


package  {
	import flash.display.MovieClip;
	import no.agens.fungrid.KeyboardControlledPhysicsGameObject;
	import flash.geom.Point;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import no.agens.fungrid.FunGrid;
	import no.agens.fungrid.PolyLineGameObject;
	import no.agens.fungrid.PolyLine;
	import no.agens.fungrid.RepeatSettings;
	import no.agens.fungrid.ParticleGameObject;
	public class PhysicsAndKeyboardMain extends MovieClip {
		var funGrid:FunGrid;
		var monster:KeyboardControlledPhysicsGameObject;
		public function PhysicsAndKeyboardMain() {
			// Create FunGrid and define viewport
			funGrid = new FunGrid(this, new Rectangle(0,0,550,400));
			// Add repeating brick platforms
			var platform1:PolyLineGameObject = new PolyLineGameObject("platform1", new PlatformMovieClip(), new Point(-150,100), new PolyLine(new Point(0,0), new Point(300,0)),null,false);
			platform1.repeatSettings = new RepeatSettings(900,undefined,undefined,500);
			var platform2:PolyLineGameObject = new PolyLineGameObject("platform2", new PlatformMovieClip(), new Point(150,200), new PolyLine(new Point(0,0), new Point(300,0)),null,false);
			platform2.repeatSettings = new RepeatSettings(900,undefined,undefined,500);
			var platform3:PolyLineGameObject = new PolyLineGameObject("platform3", new PlatformMovieClip(), new Point(-450,300), new PolyLine(new Point(0,0), new Point(300,0)),null,false);
			platform3.repeatSettings = new RepeatSettings(900,undefined,undefined,500);
			// Add a arrow key controlled monster
			// PhysicsGameObjects collide and moves on PolyLineGameObjects
			monster = new KeyboardControlledPhysicsGameObject(this.stage,"monster",new MonsterMovieClip(),new Point(0,0));
			monster.acceleration.y = 0.6;
			// Add a moving PolyLine planet with repeat settings.
			var planet:PolyLineGameObject = new PolyLineGameObject("planet", new PlanetMovieClip(), new Point(900,150), new PolyLine(new Point(0,70), new Point(70,20), new Point(140,20),new Point(210,70)));
			planet.speed.x = 2;
			planet.speed.y = 1;
			planet.repeatSettings = new RepeatSettings(1200,undefined,undefined,1000);
			// Call gameLoop on every frame
			addEventListener(Event.ENTER_FRAME, gameLoop);
		private function gameLoop(e:Event):void {
			// If the monster is not on a platform (PolyLineGameObject - plgo), lets add some particles:
			if (!monster.plgo) {
				funGrid.addGameObject(new ParticleGameObject("particle", new ParticleMovieClip(), new Point(monster.pos.x,monster.pos.y-10), new Point(Math.random()*10-5, Math.random()*10-5), new Point(0,1),50,true));
			// Let the viewport follow the monster:
			// Update FunGrid

Comments and questions are very welcome!

Scrolling Demo Tutorial

This demo/tutorial is an example on how you can add game objects to FunGrid, and make the viewport follow a spesific game object.

Get Adobe Flash player

(There are no interaction in the swf above.)

Download the files here! (FunGrid Full Package)

Assets in the ScrollingDemo.fla:

Check out the code below, and play around with it for yourself!


package  {
	import flash.display.MovieClip;
	import no.agens.fungrid.FunGrid;
	import flash.geom.Rectangle;
	import flash.events.Event;
	import no.agens.fungrid.GameObject;
	import no.agens.fungrid.RepeatSettings;
	import flash.geom.Point;
	public class ScrollingDemoMain extends MovieClip {
		var funGrid:FunGrid;
		var chopper:GameObject;
		public function ScrollingDemoMain() {
			// Create FunGrid and define viewport
			funGrid = new FunGrid(this, new Rectangle(0,0,550,400));
			// Add background:
			// scrollFactor.x = 0 - no horisontal scrolling, scrollFactor.x = 0.2 - slow vertical scrolling
			funGrid.addGameObject(new GameObject("background", new BackgroundMovieClip(),new Point(0,0),new Point(0,0.2)));
			// Add two horisontal repeating palms
			funGrid.addGameObject(new GameObject("palm", new PalmMovieClip(),new Point(0,480),new Point(0.2,0.2),true,new RepeatSettings(710)));
			funGrid.addGameObject(new GameObject("palm", new PalmMovieClip(),new Point(400,510),new Point(0.2,0.2),true,new RepeatSettings(800)));
			// Add two horisontal repeating clouds:
			funGrid.addGameObject(new GameObject("cloud1", new CloudMovieClip(),new Point(200,600),new Point(1,1),true,new RepeatSettings(700)));
			funGrid.addGameObject(new GameObject("cloud2", new CloudMovieClip(),new Point(500,800),new Point(1,1),true,new RepeatSettings(800)));
			// Create a chopper GameObject:
			chopper = new GameObject("chopper", new ChopperMovieClip(), new Point(300,550));
			// Give x speed to the chopper:
			chopper.speed.x = 10;
			// Add the chopper to funGrid:
			// Move the viewport so the chopper is in the center:
			funGrid.moveViewportCenter(chopper.pos.x, chopper.pos.y);
			// Call gameLoop on every frame
			addEventListener(Event.ENTER_FRAME, gameLoop);
		// Updates on every frame
		private function gameLoop(e:Event):void {
			// Change the y speed of the chopper with a Sine function
			chopper.speed.y = 5*Math.sin(funGrid.updateCounter/40);
			// Adjust the rotation to the speed direction
			chopper.displayObject.rotation = Math.atan2(chopper.speed.y, chopper.speed.x)*180/Math.PI;
			// Let the viewport follow the chopper:
			// Update FunGrid

Feel free to comment and ask questions!