Archive for the ‘100 Useful Classes in AS 3.0 For Game Programming’ Category

Getting Center of an object , MovieClip or Shape:

February 17th, 2012 by admin | No Comments | Filed in 100 Useful Classes in AS 3.0 For Game Programming, Flash AS 3.0, Games Programming
package com
{
	import flash.display.MovieClip;
	import flash.geom.Point;

	/**
	 * ...
	 * @author Aava Rani
	 */
	public class getMidPoint extends MovieClip
	{
		/*
		 * This function will return center point of the
		 *
		 */
		public function getMidPoint(__mc:MovieClip):Point
		{
			var centerPoint : Point = new Point();
			centerPoint.x = __mc.x + (__mc.width / 2);
			centerPoint.y = __mc.y + (__mc.height / 2);
			return centerPoint ;
		}

	}

}

Circular motion using Action Script 3.0

February 16th, 2012 by admin | No Comments | Filed in 100 Useful Classes in AS 3.0 For Game Programming, Adobe Certifications, Architectures, Flash AS 3.0, Games Programming

Lets start a cicular motion for Sun and Moon using AS 3.0.

/***
 *
 * Lets learn how to create a circular motion using Action Script 3.0
 *
 *
 */ 

package com
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.DisplayObject;
    import flash.display.Graphics;
    import flash.display.Shape;

	/**
	 * ...
	 * @author Aava Rani
	 */
	[Frame(factoryClass="com.Preloader")]
	public class Main extends Sprite
	{
       private var sun:Shape ;
	   private var moon:Shape ;
	   private var centerX:Number ;
	   private var centerY:Number ;
	   private var radius:Number ;
	   private var speed:Number ;
	   private var angle:Number;
	   private var bgColor:uint      = 0xFFCC00;
	   private var borderColor:uint  = 0x666666;
	   private var borderSize:uint   = 0;  

		public function Main():void
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			initValues();
			createPlanets();

		}

		private function initValues():void {
		      centerX = stage.stageWidth / 2;
			 centerY = stage.stageHeight / 2;
			 speed = 0.2;
			 angle = 0;
		}

		private function createPlanets():void {
			sun = drawCircle(100);
			moon = drawCircle(40);
			addChild(sun);
			addChild(moon);
			sun.x = centerX;
			sun.y = centerY;
			radius = sun.width /2 + 100;
			moon.addEventListener(Event.ENTER_FRAME, rotate);
		}

		private function rotate(evt:Event):void{
			evt.currentTarget.x = centerX + Math.cos(angle) * radius ;
			evt.currentTarget.y = centerY + Math.sin(angle) * radius ;
			angle += speed;
		}

		 private function drawCircle(__redius:int):Shape {
            var circle:Shape = new Shape();
            var halfSize:uint = Math.round(__redius / 2);
            circle.graphics.beginFill(bgColor);
            circle.graphics.lineStyle(borderSize, borderColor);
            circle.graphics.drawCircle(halfSize, halfSize, halfSize);
            circle.graphics.endFill();
            return circle;
        }

	}

}

TimerManger for Actopn Script 3.0 – 100 Useful classes

January 6th, 2012 by admin | 1 Comment | Filed in 100 Useful Classes in AS 3.0 For Game Programming, Flash AS 3.0, Games Programming

dremsus.com
Lets handle showing Timer in your game:


//

package
{
	/*
	 * @Comment: This class is for showing timer in your game or application.
	 *
	 */
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.utils.Timer;
	import flash.events.TimerEvent;

	public class TimerManager extends MovieClip
	{
		private var _timer:Timer;
		private var _interval:int;
		private var _currentTime:String;
		private var _currentCount;
		private var _intialTime:int=0;
		private var _targetTime:int;
		private var _textField:TextField;
	/*
	 * @Params:
		 * @__type:There are two types of timer you can use one is in increasing order while other is in decreasing order
		 * @Pass 'DECREASE' for decreasing order and leave blank for increasing order.
		 * @__timerInterval: This is the interval between the increment.
		 * @__targetTime : The complete time where the thmer has to reach.
		 * @__textField: The text field where you want to show the Time.
	 *
	 */
		public function TimerManager(__type:String,__timerInterval:int,__targetTime:int,__textField:TextField){
			_interval = __timerInterval;
			_targetTime = __targetTime;
			_textField = __textField;
			addTimer(__type);
		}

		private function addTimer(__type:String = ""):void {
			_timer = new Timer(_interval);
			_timer.start();
			if(__type == 'DECREASE'){
			 _timer.addEventListener(TimerEvent.TIMER, decreaseTimer);
			}else {
			 _timer.addEventListener(TimerEvent.TIMER, increaseTimer);
			}
		}

		private function decreaseTimer(evt:Event):void{
			_currentCount = evt.target.currentCount;
			_intialTime = _targetTime - evt.target.currentCount;
			_textField.text = calculateTimeInDecre();
		}

	   private function calculateTimeInDecre():String {
		   var __sec = _intialTime;
			var __min = Math.floor(__sec/60);
			__sec = String(__sec % 60);
			if(__sec.length < 2){
				__sec = "0" + __sec;
			}
			__min = String(__min % 60);
			if(__min.length < 2){
				__min = "0" + __min;
			}
			 _currentTime = __min + ":" + __sec;
			if(_currentTime == "00:00"){
				_timer.stop();
			}	

			return _currentTime;
	   }

		private function increaseTimer(evt:Event):void{
			_intialTime +=  1
			_textField.text = calculateTimeInIncre();
		}

		private function calculateTimeInIncre():String {
			var __sec = _intialTime;
			var __min = Math.floor(__sec/60);
			__sec = String(__sec % 60);
			if(__sec.length < 2){
				__sec = "0" + __sec;
			}
			__min = String(__min % 60);
			if(__min.length < 2){
				__min = "0" + __min;
			}
			 _currentTime = __min + ":" + __sec;
			 return _currentTime;
		}

		/*
		 * @Comment: If you want to stop the timer in between call this function.
		 *
		 */ 

		public function stopTimer():void {
			_timer.stop();
		}

	}

}

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , ,

ShakingEffect Manager for Games

January 2nd, 2012 by admin | 1 Comment | Filed in 100 Useful Classes in AS 3.0 For Game Programming, Flash AS 3.0, Games Programming

images

[xml]
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;

public class ShakingEffect extends MovieClip
{
private var _posX:Number;
private var _posY:Number;
private var _refObject:Object;
private var _timer:Timer;
private var _isTimerStop:Boolean = false;
private var _interval:int ;
private var _strength:int;

public function ShakingEffect()
{
super();
}
/*
* @params :
* @__obj:pass the object on which you want to put the effect.
* @__interval:Pass the interval of shaking effect.
* @__strength:Passing this you can control the speed of shaking effect.
*/
public function startShake(__obj:Object,__interval:int,__strength:int):void {
_refObject = __obj;
_posX = _refObject.x;
_posY = _refObject.y;
_interval = __interval;
_strength = __strength;
startTimer();
}
/*
* @Comments: Here we are starting the timer. The Shaking effect will continue untill you stop the timer.
*/
private function startTimer():void{
_timer = new Timer(__interval);
_timer.addEventListener(TimerEvent.TIMER, shakeObject);
_timer.start ()
}

private function shakeObject(evt:Event):void {
if(!_isTimerStop){
_refObject.x = _posX+ getMinusOrPlusValues()*(Math.random()*_strength);
_refObject.y = _posY+ getMinusOrPlusValues()*(Math.random()*_strength);
}
}

/*
* @Comments: By Calling this function you can stop the shaking effect.
*/
public function stopShake():void {
stopTimer();
resetObjectPosition();
}

private function resetObjectPosition():void
{
_refObject.x = _posX;
_refObject.y = _posY;
}

private function stopTimer():void
{
_timer.stop();
_isTimerStop = true;
_timer.removeEventListener(TimerEvent.TIMER, shakeObject);

}

private function getMinusOrPlusValues():int{
var rand : Number = Math.random()*4;
if (rand<1) return -1
else return 1;
}

}

}

[]
/xml

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

KeyBoardEventManager for Games

January 2nd, 2012 by admin | 7 Comments | Filed in 100 Useful Classes in AS 3.0 For Game Programming, Flash AS 3.0, Games Programming

images

package
{

	import flash.display.MovieClip;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	public class KeyBoardEventManager extends MovieClip
	{
		private var _parent:Object;
		private var _isRightPressed:Boolean = false;
		private var _isLeftPressed:Boolean = false;
		private var _isUPpressed:Boolean = false;
		private var _isDOWNpressed:Boolean = false;
		/*
		 * @Comments : During the development of a game a number of times we need Key Board
		 * controls in that case we can use this class.
		 *
		 *
		 */ 

		public function KeyBoardEventManager()
		{
			super();

		}

		public function addKeyListners(__parent:Object):void {
			_parent = __parent
			_parent.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			_parent.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
		}

		private function keyDownHandler(evt:KeyboardEvent):void {
				if(evt.keyCode == Keyboard.RIGHT){
					_isRightPressed = true
				}
				else if (evt.keyCode == Keyboard.LEFT ) {
					  _isLeftPressed = true;
				}
				else if (evt.keyCode == Keyboard.UP) {
					_isUPpressed = true;
				}
				else if (evt.keyCode == Keyboard.DOWN) {
				  _isDOWNpressed = true;
				}

		}

		private function keyUpHandler(evt:KeyboardEvent):void {
		     if (evt.keyCode == Keyboard.RIGHT){
				_isRightPressed = false;
			 }
			else if (evt.keyCode == Keyboard.UP){
					_isUPpressed = false;
			}
			else if (evt.keyCode == Keyboard.DOWN){
				  _isDOWNpressed = false;
			}
			else if (evt.keyCode == Keyboard.LEFT){
				  _isLeftPressed = false;
			}
		}

		public function getLEFTkeyStatus():Boolean {
			return  _isLeftPressed;
		}

		public function getRIGHTkeyStatus():Boolean {

			return  _isRightPressed;
		}
		public function getUPkeyStatus():Boolean {
			return  _isUPpressed;
		}
		public function getDownkeyStatus():Boolean {
			return  _isDOWNpressed
		}

		public function resetValuse():void {
			_parent.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			_parent.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
			_isRightPressed = false;
		    _isLeftPressed = false;
	        _isUPpressed = false;
		    _isDOWNpressed = false;	  	   

		}
	}

}

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , ,

PaginationManager For photo Gallery creation

January 2nd, 2012 by admin | 4 Comments | Filed in 100 Useful Classes in AS 3.0 For Game Programming, Flash AS 3.0, Games Programming

images


package
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;

	public class PaginationManager extends MovieClip
	{
		/*
		 * @Comments : Before using this class you need to create a container MovieClip
		 * in your FLA with linkage ID '_mcContainer'. Inside the _mcContainer there should be 'btnNext' & 'btnPrev'
		 *
		 * */

		private var _pageCounter:int = 1;
		private var _NoOfObjectAtOnce:int;
		private var _objCounter:int = 0;
		private var _totalPages:int
		private var _objectArray:Array;
		private var _PosX:int;
		private var _PosY:int;

		public function PaginationManager()
		{
			super();
		}
		/*
		 * @Params :
			 * @__array : Pass the array of those object which you want to show in the pagination.
			 * @__NoOfObjectAtOnce : This is the parameter for how many objects you want to show in one page.
			 * @__PosX : Starting X position.
			 * @__PosY : Starting Y position.
		 *
		 * */
		public function init(__array:Array,__NoOfObjectAtOnce:int,__PosX:int,__PosY:int):void{
			_objectArray = __array;
			_noOfObjectAtOnce = __noOfObjectAtOnce;
			_PosX = __PosX;
			_PosY = __PosY;
		    _totalPages = Math.ceil(_objectArray.length/_noOfObjectAtOnce);
			btnPrev.addEventListener(MouseEvent.CLICK, prevPage);
			btnNext.addEventListener(MouseEvent.CLICK, nextPage);
			generateNewPage();
		}
		/*
		 * @Params :Null
		 * @Comments : This function will genarate the pages/new thumbnails.
		 * */
		private function generateNewPage():void{
				for(var __i=(_pageCounter-1)*_noOfObjectAtOnce;__i<_pageCounter*_noOfObjectAtOnce;__i++){
				if(__i < _objectArray.length){
					addObjects();
				}
			}
			_objCounter=0;
		}

		private function addObjects():void {
			var _newMCcontainer:_mcContainer = new _mcContainer();
			_newMCcontainer.x = _PosX;
			_newMCcontainer.y = _PosY + _newMCcontainer.height *_objCounter;
			_newMCcontainer.name = 'container'+_objCounter;
			addChild(_newMCcontainer);
			_objCounter ++;
		}

		private function removeObjectArray():void{
			for(i=0;i<_noOfObjectAtOnce;i++){
				removeChild(getChildByName('container'+i));
			}
		}
		/*
		 * @Params :event
		 * @Comments : On Clicking previous button this function will be called.
		 * */
		private function prevPage(event:MouseEvent):void{
			if(_pageCounter > 1){
				removePage();
				_pageCounter --;
				generateNewPage();
			}
		}
		/*
		 * @Params :event
		 * @Comments : On Clicking next button this function will be called.
		 * */
		private function nextPage(event:MouseEvent):void{
			if(_pageCounter < _totalPages){
				removePage();
				_pageCounter ++;
				generateNewPage();
			}
		}
	}
}

Tags: , , , , , , , , , , , , , , , , , , , , , , , ,

Get Random Number From A Range

December 23rd, 2011 by admin | 1 Comment | Filed in 100 Useful Classes in AS 3.0 For Game Programming, Flash AS 3.0, Games Programming

images

package
{
	import flash.display.MovieClip;

	/**
	 * ...
	 * @Description : This class can be used when we need a Random number from a specific range.
	 * For example if we need a Random number between 10-20. We can use this class.
	 * @author Aava
	 */
	public class GetRandomNumberFromARange extends MovieClip
	{

		public function GetRandomNumberFromARange()
		{
			super();
		}

		/*
		 * @Description : Pass the maximum value and minimum value as paramere.
		 * @Return : It will return the random value from the given range.
		 * @Parameters : __MAX represents Maximum value of range.
		 * @Parameters : __MIN represents Minimum value of range.
		 */
		public function getRandomNumber(__MAX:Number, __MIN:Number):Number {
			var __range:Number = __MAX - __MIN
			var __someNum:Number = int(Math.random() * __range) + __MIN
			return __someNum;
		}
	}

}

Tags: , , , , , , , , , , , , , , , , , , , , , , ,

Set Highest depth of an object – Important Classes for games in AS 3.0

December 23rd, 2011 by admin | No Comments | Filed in 100 Useful Classes in AS 3.0 For Game Programming, Flash AS 3.0, Games Programming

images

package
{
	import flash.display.MovieClip;

	/**
	 * ...
	 * @Description : In development of game we somtime need to put a particular object in highest Depth, means
	 *                above of all other objects of same parent. In that case we can use this class.
	 * @author Aava
	 */
	public class KeepInTopDepthClass extends MovieClip
	{

		private var _parent:Object
		private var _ObjRef:MovieClip
		/*
		 * @Description : Here we are passing the parent object as the parameter of constructor.
		 */

		public function KeepInTopDepthClass(__parent:Object) {
			_parent = __parent;
		}
		/*
		 * @Description : By Calling this function with the parameter of that object which you want to be in top , you
		 * can make that object on above of all others.
		 */
		public function setTheObjectAtTopDepth(__Obj:MovieClip):void {
			_ObjRef = __Obj;
			var topPosition:uint = _parent.numChildren - 1;
		   _parent.setChildIndex(_ObjRef, topPosition-1);
		}

	}

}

Tags: , , , , , , , , , , , , , , , , , , ,

Remove All Children – Important Classes for games in AS 3.0

December 22nd, 2011 by admin | No Comments | Filed in 100 Useful Classes in AS 3.0 For Game Programming, Flash AS 3.0, Flex (RIA), Games Programming

images

package
{

	import flash.display.MovieClip;
	/**
	 * ...
	 * @description : This Class can be used when you need to remove all the childern of a parent movieClip.
	 * @author Aava Rani
	 */
	public class RemoveAllChildClass extends MovieClip
	{

		public function RemoveAllChildClass():void
		{
			trace(' Constructor called ');
		}

		public function removeAllChild(__parent:MovieClip):void {
			if(__parent.numChildren!=0){
				var __i:int = __parent.numChildren;
				while(__i -- )
				{
					__parent.removeChildAt( __i );
				}
			}
		}

	}

}

Tags: , , , , , , , , , , , , , , , , ,

Remove movieclips from Array- Important Classes for games in AS 3.0

December 22nd, 2011 by admin | No Comments | Filed in 100 Useful Classes in AS 3.0 For Game Programming, Flash AS 3.0, Flex (RIA), Games Programming

images

package
{

	import flash.display.MovieClip;
	/**
	 * ...
	 * @description : This Class can be used when you need to remove all movieclips from its stored array.
	 * @author Aava Rani
	 */
	public class RemoveMovieClipsFromArrayClass extends MovieClip
	{

		public function RemoveMovieClipsFromArrayClass():void
		{
			trace(' Constructor called ');
		}

		public function removeAllMovieClipsFromArray(__clipArray:Array):void {
			for (var __i:int = __clipArray.length - 1; __i >= 0 ; __i-- ) {
				 removeChild(__clipArray[__i])
				__clipArray.splice(__i, 1);
			}
		}

	}

}

Tags: , , , , , , , , , , , , , , ,