onPause / onResume for states

Discuss suggestions to improve the Axel library.

onPause / onResume for states

Postby Rhuno » Mon Aug 13, 2012 11:54 am

I've been working on a mobile game and when I push the menu state onto the stack, I've found it helpful to have state level onPause and onResume functions. This is very useful functionality that can easily be added to Axel. I just added public onPause and onResume functions in the AxState class and then called those methods in the pushState and popState functions in the main Ax class.

AxState

Code: Select all
public function onPause():void {
    // Override as needed
}
      
public function onResume():void {
    // Override as needed
}


Ax - pushState
Code: Select all
public static function pushState(state:AxState):AxState {

    if(states.length > 0)
        states[states.length - 1].onPause();

    camera.reset();
    states.push(state);
    state.create();
    return state;
}


Ax - popState
Code: Select all
public static function popState():void {
    camera.reset();
    destroyedStates.push(states.pop());
    state.onResume();
}


Anyway, just a suggestion; keep up the good work! :)
Rhuno
Private
 
Posts: 4
Joined: Mon Aug 06, 2012 5:35 pm

Re: onPause / onResume for states

Postby GigaClon » Fri Nov 02, 2012 8:33 pm

Nice bit of code. I have upgraded it to send the class of the incoming/outgoing state to the functions

In pushState
Code: Select all
   if(states.length > 0) states[states.length - 1].onPause(getQualifiedClassName(state));   


in popState
Code: Select all
var s:String = getQualifiedClassName(states[states.length-1]);
then when calling onResume pass s.

This for people that want to do stuff based which state they are pausing/resume. Qualified names are package::ClassName
GigaClon
Sergeant Major
 
Posts: 45
Joined: Sat Oct 27, 2012 6:52 pm

Re: onPause / onResume for states

Postby yahoo92 » Sun Nov 18, 2012 10:26 pm

This is very useful functionality that can easily be added to Axel???
yahoo92
Private
 
Posts: 1
Joined: Sun Nov 18, 2012 10:18 pm

Re: onPause / onResume for states

Postby GigaClon » Mon Nov 19, 2012 9:26 am

Arkeus intends on adding it. You can add it yourself by following the instructions.
GigaClon
Sergeant Major
 
Posts: 45
Joined: Sat Oct 27, 2012 6:52 pm

Re: onPause / onResume for states

Postby Arkeus » Thu Nov 22, 2012 2:46 am

Just a heads up, I've implemented it with your addition GigaClon, but the signature is a class rather than a string:

Code: Select all
      /**
       * This function is called whenever this state loses focus due to another state being pushed
       * on top of it. Override it to provide specific logic that fires just before the state is
       * switched away from.
       *
       * @param sourceState The class of the state that was pushed on the stack that now has focus.
       */
      public function onPause(sourceState:Class):void {
         // Override as needed
      }
      
      /**
       * This function is called whenever focus returns this to state due to another state being popped
       * off the stack. Override it to provide specific logic that fires immediately when this state
       * is switched to.
       *
       * @param sourceState The class of the state that was popped off to reveal this state.
       */
      public function onResume(sourceState:Class):void {
         // Override as needed
      }


The main reason is that when comparing it's a compile time error if you do something wrong, rather than an issue with you mispelling the package name because it's a string.

Should be in the 0.9.3 branch "soon".
Image
User avatar
Arkeus
Site Admin
 
Posts: 363
Joined: Mon Mar 26, 2012 12:43 am


Return to Suggestions

Who is online

Users browsing this forum: No registered users and 1 guest

cron