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

TimerManger for Actopn Script 3.0 – 100 Useful classes

January 6th, 2012 by aabhaanjan

dremsus.com
Lets handle showing Timer in your game:
[xml]

//

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(); } } } [/xml]

ShakingEffect Manager for Games

January 2nd, 2012 by aabhaanjan

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

KeyBoardEventManager for Games

January 2nd, 2012 by aabhaanjan

images
[xml]
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;

}
}

}
[/xml]

PaginationManager For photo Gallery creation

January 2nd, 2012 by aabhaanjan

images
[xml]

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(); } } } } [/xml]

Get Random Number From A Range

December 23rd, 2011 by aabhaanjan

images
[xml]
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;
}
}

}
[/xml]

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

December 23rd, 2011 by aabhaanjan

images

[xml]
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);
}

}

}
[/xml]

Remove All Children – Important Classes for games in AS 3.0

December 22nd, 2011 by aabhaanjan

images

[xml]
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 );
}
}
}

}

}
[/xml]