KeyEvent super class ?

I need help sending events to and from a super class

Page 1 of 1

0 Replies - 465 Views - Last Post: 09 March 2009 - 08:55 AM

#1 coden4fun  Icon User is offline

  • if(coden4fun.NotTired){ ContinueToCode(); }
  • member icon

Reputation: 27
  • View blog
  • Posts: 696
  • Joined: 27-February 09

KeyEvent super class ?

Post icon  Posted 09 March 2009 - 08:55 AM

Hi, I'm having a little hard time understanding how I can successfully have an abstract class use a Keyboard event, and have it's children be able to use it correctly?

For instance I have the following

Game Object entity base class for game
package
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	
	/*
		The base class for all objects in the game.
	*/
	public class GameObject
	{
		// object position
		public var position:Point = new Point(0, 0);
		// higher zOrder objects are rendered on top of lower ones
		public var zOrder:int = 0;
		// the bitmap data to display	
		public var graphics:GraphicsResource = null;
		// true if the object is active in the game
		public var inuse:Boolean = false;
		
		public function GameObject()
		{
			
		}
		
		public function startupGameObject(graphics:GraphicsResource, position:Point, z:int = 0):void
		{
			if (!inuse)
			{
				this.graphics = graphics;
				this.zOrder = z;
				this.position = position.clone();
				this.inuse = true;
				
				EntityManager.Instance.addGameObject(this);
				//GameObjectManager.Instance.addGameObject(this);
			}
		}
		
		//change image if needed
		public function setGraphic(graphics:GraphicsResource)
		{
			this.graphics = graphics;
		}
		
		public function shutdown():void
		{
			if (inuse)
			{				
				graphics = null;
				inuse = false;
				
				EntityManager.Instance.removeGameObject(this);
				//GameObjectManager.Instance.removeGameObject(this);
			}
		}
		
		//copy image to the back buffer.
		public function copyToBackBuffer(db:BitmapData):void
		{
			db.copyPixels(graphics.bitmap, graphics.bitmap.rect, position, graphics.bitmapAlpha, new Point(0, 0), true);
		}
		
		//update frames
		public function enterFrame(dt:Number):void
		{
		
		}
		
		//get events
		public function getEvents(event:KeyboardEvent):void
		{
			
		}

	}
}



Entity Manager class, manages, all entities here
package
{
	import flash.display.*;
	
	import mx.collections.*;
	import mx.controls.Text;
	import mx.core.*;
	
	public class EntityManager
	{
		// double buffer
		public var backBuffer:BitmapData;
		public var clearColor:uint = 0x333333;
		protected static var instance:EntityManager = null;
		protected var lastFrame:Date;
		protected var gameObjects:ArrayCollection = new ArrayCollection();
		protected var newGameObjects:ArrayCollection = new ArrayCollection();
		protected var removedGameObjects:ArrayCollection = new ArrayCollection();
		protected var nemousArray:Array = new Array();
		
		//if we don't exist create ourselve's
		static public function get Instance():EntityManager
		{
			if ( instance == null )
			instance = new EntityManager();
			return instance;
		}
		
		//constructor of EntityManager
		public function EntityManager()
		{
			if ( instance != null )
				throw new Error( "Only one Singleton instance should be instantiated" ); 
			
			//create the back buffer where we will draw all game entities on the screen	
			backBuffer = new BitmapData(Application.application.width, Application.application.height, false);
		}
		
		//startup to startup all gameObject game "Entities" 
		public function startup():void
		{
			lastFrame = new Date();
			
			//new Bounce().startupBounce();
			new Player().startupPlayer();
			
			for(var i:int = 0; i<4; i++)
			{
				var nemous:Nemous = new Nemous();
				nemous.setRandomPosition();
				nemousArray.push(nemous);
			}
		}
		
		//shutdown all entities
		public function shutdown():void
		{
			shutdownAll();
		}
		
		//update
		public function enterFrame():void
		{
			// Calculate the time since the last frame
			var thisFrame:Date = new Date();
			var seconds:Number = (thisFrame.getTime() - lastFrame.getTime())/1000.0;
			lastFrame = thisFrame;
			
			removeDeletedGameObjects();
			insertNewGameObjects();
			
			// now allow objects to update themselves
			for each (var gameObject:GameObject in gameObjects)
			{
				if (gameObject.inuse) 
					gameObject.enterFrame(seconds);
			}
			
			drawObjects();
		}
		
		//draw all objects onto the back buffer "double buffer"
		protected function drawObjects():void
		{
			backBuffer.fillRect(backBuffer.rect, clearColor);
			
			// draw the objects
			for each (var gameObject:GameObject in gameObjects)
			{
				if (gameObject.inuse) 
					gameObject.copyToBackBuffer(backBuffer);
			}
		}
		
		//add specific game object "Entity" to the array
		public function addGameObject(gameObject:GameObject):void
		{
			newGameObjects.addItem(gameObject);
		}
		
		//remove specific game object "Entity" to the array
		public function removeGameObject(gameObject:GameObject):void
		{
			removedGameObjects.addItem(gameObject);
		}
		
		//dispose of all objects by moving them to the remove object array, so we only shut them down once
		protected function shutdownAll():void
		{
			// don't dispose objects twice
			for each (var gameObject:GameObject in gameObjects)
			{
				var found:Boolean = false;
				for each (var removedObject:GameObject in removedGameObjects)
				{
					if (removedObject == gameObject)
					{
						found = true;
						break;
					}
				}
				
				if (!found)
					gameObject.shutdown();
			}
		}
		
		//inset entities "new gameObjects into the array for use
		protected function insertNewGameObjects():void
		{
			for each (var gameObject:GameObject in newGameObjects)
			{
				for (var i:int = 0; i < gameObjects.length; ++i)
				{
					if (gameObjects.getItemAt(i).zOrder > gameObject.zOrder ||
						gameObjects.getItemAt(i).zOrder == -1)
						break;
				}

				gameObjects.addItemAt(gameObject, i);
			}
			
			newGameObjects.removeAll();
		}
		
		//remove all game objects "Entites" in the array that need to be deleted from the removedGameObjects array
		protected function removeDeletedGameObjects():void
		{
			// insert the object acording to it's z position
			for each (var removedObject:GameObject in removedGameObjects)
			{
				var i:int = 0;
				for (i = 0; i < gameObjects.length; ++i)
				{
					if (gameObjects.getItemAt(i) == removedObject)
					{
						gameObjects.removeItemAt(i);
						break;
					}
				}	
			}
			
			removedGameObjects.removeAll();
		}

	}
}



player class
package
{
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	
	import mx.controls.Text;
	import mx.core.*;
	
	public class Player extends GameObject
	{
		protected static const speed:Number = 10;
		protected var direction:Point = new Point(5, 5);
		protected var score:Text = new Text();
		
		//constuctor of Player
		public function Player()
		{
			super();
		}
		
		//start the player
		public function startupPlayer():void
		{
			super.startupGameObject(ResourceManager.RobotLeftGraphics, new Point(2.0, 2.0));
			score.text = "Score: ";
		}
		
		//shutdown the player
		override public function shutdown():void
		{
			super.shutdown();
		} 
		
		//update frame for player
		override public function enterFrame(dt:Number):void
		{
			super.enterFrame(dt);
			
			position.x += Motion2D.CalculateVelocity(direction.x*speed, dt);
			position.y += Motion2D.CalculateVelocity(direction.y*speed, dt);
			//position.x += direction.x * speed * dt;
			//position.y += direction.y * speed * dt;
			
			if (position.x >= Application.application.width - graphics.bitmap.width)
				direction.x = -1;
			else if (position.x <= 0)
				direction.x = 1;
				
			if (position.y >= Application.application.height - graphics.bitmap.height)
				direction.y = -1;
			else if (position.y <= 0)
				direction.y = 1;
		}
		
		override public function getEvents(event:KeyboardEvent):void
		{
			super.getEvents(event);
			if(event.keyCode == 35) direction.x = -direction.x
			else direction.y = -direction.y;
			
		}
	}
}



Look closely at getEvents I'm having a difficult time getting this to work.

Now the way I see it is EntityManager should have a getEvent function that can be overriden, but only class objects that ovveride this function should be able to inherit this event listener, and check for the keyboard events.

however, I'm not sure exactly how to setup this up with the EntityManager, so any help will be great! ;)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1