Commits

Chris Logsdon committed 08c94ff Merge

Merge

  • Participants
  • Parent commits 89bf50f, 0da8d6c

Comments (0)

Files changed (8)

File lib/Library.xml

File contents unchanged.

File lib/mageWalk.png

Added
New image
 			
 			if (Input.isKeyDown(Input.LEFT))
 			{
+				man.graphic.play("walk");
 				man.x -= 7;
 				man.rotationY = 180;
 			}
-			
-			if (Input.isKeyDown(Input.RIGHT))
+			else if (Input.isKeyDown(Input.RIGHT))
 			{
+				man.graphic.play("walk");
 				man.x += 7;
 				man.rotationY = 0;
 			}
+			else
+			{
+				if (man.graphic.playing != "attack") man.graphic.play("stand", true);
+			}
 			
 			if (man.canJump)
 			{
 			
 			if (Input.isKeyDown(Input.D) )
 			{
+				man.graphic.play("attack");
 				man.shoot();
 			}
 			

File src/com/jacobalbano/Anim.as

+package com.jacobalbano
+{
+	internal final class Anim 
+	{
+		public var framerate:uint;
+		public var frames:Array;
+		public var loop:Boolean;
+		public var name:String;
+		
+		/**
+		 * This is a utility class used by Animation to store data. It cannot be extended.
+		 * @param	name		The name of the animation
+		 * @param	framerate	How many frames per second the animation runs at
+		 * @param	frames		An arbitrary array denoting frames
+		 * @param	loop		Whether the animation returns to the beginning after finishing
+		 */
+		public function Anim(name:String, framerate:uint, frames:Array, loop:Boolean) 
+		{
+			this.framerate = framerate
+			this.frames = frames;
+			this.loop = loop;
+			this.name = name;
+		}
+		
+	}
+
+}

File src/com/jacobalbano/Animation.as

+package com.jacobalbano
+{
+	import flash.display.Bitmap;
+	import flash.display.BitmapData;
+	import flash.display.MovieClip;
+	import flash.geom.Point;
+	import flash.geom.Rectangle;
+	import flash.events.Event;
+	import flash.events.MouseEvent;
+	import flash.display.Sprite;
+	import com.jacobalbano.Anim;
+
+	public class Animation extends Sprite
+	{
+		public var clipRect:Rectangle = new Rectangle(0, 0, 100, 100);
+		public var frame:uint = 0;
+		
+		private var storage:BitmapData;
+		public var buffer:BitmapData;
+		private var frameDelay:uint = 0;
+		private var animation:Anim;
+		private var animations:Vector.<Anim> = new Vector.<Anim>;
+		private var frameWidth:Number;
+		private var frameHeight:Number;
+		private var _playing:String;
+		
+		/**
+		 * 
+		 * @param	DATA 	The embedded image to assign to the animation
+		 * @param	width	Width of one animation frame
+		 * @param	height	Height of one animation frame
+		 */
+		public function Animation(DATA:*, width:Number, height:Number) 
+		{
+			super();
+			
+			//	Since the DATA parameter is untyped, we need to check what type it is before we can make use of it
+			//	We can do this with the 'is' keyword
+			if (DATA is Class)
+			{
+				this.storage = (new DATA).bitmapData;
+			}
+			else if (DATA is Sprite || DATA is MovieClip)
+			{
+				this.storage = new BitmapData(DATA.width, DATA.height, true, 0);
+				this.storage.draw(DATA);
+			}
+			else if (DATA is Bitmap)
+			{
+				this.storage = DATA.bitmapData;
+			}
+			else if (DATA is BitmapData)
+			{
+				this.storage = DATA;
+			}
+			else
+			{
+				//	The type of DATA is incompatible, so end the program and throw an error
+				throw new Error("Invalid image source! Valid types are Class, Bitmap, BitmapData, Sprite and MovieClip.");
+			}
+			
+			//	Make both bitmapData objects the same
+			this.buffer = this.storage.clone();
+			
+			//	Assign the default frame to the object
+			this.graphics.beginBitmapFill(storage, null, false, true);
+			this.graphics.drawRect(0, 0, width, height);
+			this.graphics.endFill();
+			
+			//	Set the buffer to update every frame
+			this.addEventListener(Event.ENTER_FRAME, runUpdate);
+			
+			this.frameWidth = width;
+			this.frameHeight = height;
+		}
+		
+		/**
+		 * Add a new animation
+		 * @param	name		The animation's identifier
+		 * @param	array		An arbitrary array denoting frames, i.e. [0, 2, 3]
+		 * @param	framerate	How many times per second the animation should update
+		 * @param	loop		Whether the animation should restart when it reaches the end
+		 */
+		public function add(name:String, array:Array, framerate:uint, loop:Boolean):void
+		{
+			//	Animation names must be unique, so throw an error if an animation is added again
+			for each (var item:Anim in this.animations) 
+			{
+				if (item.name == name)
+				{
+					throw new Error("An animation with the name '" + name  +"' already exists.");
+					return;
+				}
+			}
+			
+			this.animations.push(new Anim(name, framerate, array, loop) );
+		}
+		
+		/**
+		 * Return the currently playing animation's name
+		 */
+		public function get playing():String
+		{
+			if (this.animation)	return this.animation.name;
+			return "No animation is currently playing.";
+		}
+		
+		/**
+		 * Start an animation by name
+		 * @param	name	The name of the animation to play
+		 * @param	restart	Whether the named animation should restart from the beginning
+		 */
+		public function play(name:String, restart:Boolean = false):void
+		{
+			for each (var item:Anim in this.animations) 
+			{
+				if (item.name == name)
+				{
+					if (item == this.animation)
+					{
+						if (restart) this.frame = 0;
+						break;
+					}
+					else
+					{
+						this.animation = item;
+						this.frame = 0;
+						break;
+					}
+					
+					return;
+				}
+			}
+		}
+		
+		/**
+		 * Internal enterFrame function
+		 */
+		private function runUpdate(e:Event):void 
+		{			
+			if (animation)
+			{
+				if (frameDelay >=  30 / animation.framerate)
+				{
+					if (this.frame == animation.frames.length - 1)
+					{
+						if (this.animation.loop)  this.frame = 0;
+						else this.animation = null;
+						return;
+					}
+					else
+					{
+						frame++;
+					}
+					
+					frameDelay = 0;
+					trace("Frame index", frame);
+					trace("Frame", this.animation.frames[frame]);
+				}
+				
+				setRect();
+				frameDelay++;
+			}			
+		}
+	
+		/**
+		 * Internal function to update the buffer
+		 */
+		private function setRect():void 
+		{
+			this.buffer.copyPixels(storage, new Rectangle(this.animation.frames[this.frame] * this.frameWidth, 0, this.frameWidth, this.frameHeight), new Point(0, 0));
+			this.graphics.clear();
+			
+			this.graphics.beginBitmapFill(buffer, null, false, false);
+			this.graphics.drawRect(0, 0, this.frameWidth, this.frameHeight);
+			this.graphics.endFill();
+		}
+		
+	}
+}

File src/ifrit/Enemy.as

File contents unchanged.

File src/ifrit/Mob.as

 	import flash.geom.Point;
 	import flash.utils.Timer;
 	
+	import com.jacobalbano.Animation;
+	
 	/**
 	 * @author Chris Logsdon
 	 * @author Jake Albano
 	public class Mob extends Sprite
 	{
 		//	Graphical representation
-		public var bitmap:Bitmap;
-		protected var container:Sprite = new Sprite;
+		public var graphic:Animation;
+		protected var container:Sprite;
 		
 		private var shootTimer:Timer = new Timer(0, 20);
 		
 		public var hitpoints:int;
 		public var maxHealth:uint;
 		
-		public function Mob(x:Number, y:Number, bitmap:Bitmap) 
+		public function Mob(x:Number, y:Number, bitmap:Bitmap, frameWidth:Number, frameHeight:Number) 
 		{
-			this.bitmap = bitmap;
+			//super(bitmap, frameWidth, frameHeight);
 			
+			//this.bitmap = bitmap;
+			
+			this.container = new Sprite;
 			addChild(container);
+			graphic = new Animation(bitmap, frameWidth, frameHeight);
+			graphic.add("stand", [1], 0, true);
+			graphic.add("walk", [0, 1, 2, 3], 6, true);
+			graphic.add("attack", [6, 7, 8, 9], 12, false);
+			graphic.play("stand");
 			
-			container.x = bitmap.x - (bitmap.width / 2); // Set registration point to center
-			container.y = bitmap.y - (bitmap.height / 2);
-			bitmap.smoothing = true;
-			container.addChild(bitmap);
+			container.x = -frameWidth / 2; // Set registration point to center
+			container.y = -frameHeight / 2;
+			container.addChild(graphic);
 			
 			this.x = x;
 			this.y = y;
 			
-			this.halfSize = new Point(this.width / 2, this.height / 2);
+			this.halfSize = new Point(frameWidth / 2, frameHeight/ 2);
 			
 			speedLimit = new Point(7, 20);
 			

File src/ifrit/Player.as

 	{		
 		public function Player(x:Number, y:Number) 
 		{
-			super( x, y, Library.IMG("rogue.png") );
+			super( x, y, Library.IMG("mageWalk.png"), 18, 25 );
 			this.friendly = true;
 		}