Pages

Thursday, August 4, 2011

AS3: Basic navigation class

Here is basic code for making a navigation class. The navigation here in is based on a counter. By taking the reference of a counter the class can be extended to achieve any type of navigation.

CODE
 package   
 {  
  /**  
   * @file CNavigator.as  
   * @author Abhishek Kumar  
   */  
  public class CNavigator   
  {  
   public var _Proceed:Function;  
   private var counter:int;  
   private var minRange:int;  
   private var maxRange:int;  
   public function CNavigator(min:int, max:int)  
   {  
    trace('CNavigator -> constructor');  
    minRange = min;  
    maxRange = max;  
    counter = 0;  
   }  
   public function navLeft():void  
   {  
    trace('CNavigator -> navLeft');  
    if (counter > minRange)  
    {  
     counter--;  
     _Proceed(counter);  
    }  
   }  
   public function navRight():void  
   {  
    trace('CNavigator -> navRight');  
    if (counter < maxRange)  
    {   
     counter++;  
     _Proceed(counter);  
    }   
   }   
  }   
 }   

USAGE
 import CNavigator;

 var oNav:CNavigator = new CNavigator(0, 5);
 oNav._Proceed = trace;

 oNav.navLeft();
 oNav.navLeft();

 oNav.navRight();
 oNav.navRight();
 oNav.navRight();
 oNav.navRight();

 oNav.navLeft();
 oNav.navLeft();
 oNav.navLeft();

No comments:

Post a Comment