Can I stop the entire Openfl?

I want it to not do the frame event refresh of the task, because I want to be able to pause and resume when Event.ACTIVATE and Event.DEACTIVATE occur.

How did you deal with the game suspension and recovery?

I found that returning to the desktop on android, the progress of the game continues without pause. I hope to be able to pause, is there any good way?

Hi rainy.

I’d say that heavily depends on the kind of application.
Generally you need to keep track what it’s doing and use the listener
function to handle it.
Here’s an example:

package;

import openfl.display.Sprite;
import openfl.Lib;
import openfl.events.Event;

class Main extends Sprite 
{
    private var currentState:String;
    public function new() 
    {
        super();
        
        addEventListener(Event.ENTER_FRAME, mainLoop);
        currentState = "mainLoop";
        
        addEventListener(Event.DEACTIVATE, deactivated);
    }
    
    private function mainLoop(e:Event):Void
    {
        trace("loop");
    }
    
    private function deactivated(e:Event):Void
    {
        trace("deactivated");
        removeEventListener(Event.DEACTIVATE, deactivated);
        switch(currentState)
        {
        case "mainLoop":
            removeEventListener(Event.ENTER_FRAME, mainLoop);
        }
        
        addEventListener(Event.ACTIVATE, activated);
    }
    
    private function activated(e:Event):Void
    {
        trace("activated");
        removeEventListener(Event.ACTIVATE, activated);
        switch(currentState)
        {
        case "mainLoop":
            addEventListener(Event.ENTER_FRAME, mainLoop);
        }
        
        addEventListener(Event.DEACTIVATE, deactivated);
    }    
}

Another option is simply setting the frameRate to zero but I cannot guarantee
that this will work in every case.

package;

import openfl.display.Sprite;
import openfl.Lib;
import openfl.events.Event;

class Main extends Sprite 
{
    var frameRate:Float;
    public function new() 
    {
        super();
        frameRate = stage.frameRate;
        addEventListener(Event.ENTER_FRAME, mainLoop);
        addEventListener(Event.DEACTIVATE, deactivated);
    }
    
    private function mainLoop(e:Event):Void
    {
        trace("loop");
    }
    
    private function deactivated(e:Event):Void
    {
        trace("deactivated");
        removeEventListener(Event.DEACTIVATE, deactivated);
        addEventListener(Event.ACTIVATE, activated);
	stage.frameRate = 0;
    }
    
    private function activated(e:Event):Void
    {
        trace("activated");
        removeEventListener(Event.ACTIVATE, activated);
        addEventListener(Event.DEACTIVATE, deactivated);
	stage.frameRate = frameRate;
    }    
}
1 Like

Ok, I understand it, but without a unified pause and recovery, it is still somewhat inconvenient. Thank you for your demo!