EXIT_FRAME event missing

MIssing events! Oh noes?

Class<openfl.events.Event> has no field EXIT_FRAME

I’m not sure if I’m simply missing out on a patch somewhere, but it appears Event.ENTER_FRAME was implemented, but not Event.EXIT_FRAME. I’ve seen a few posts in the archive, over a year old now. It appears the standard Haxe API supports the event, http://api.haxe.org/flash/events/Event.html - will it be added to the OpenFL API?

I hope so, I think the caution is trying to be sure that we don’t make things run too slowly by iterating over the display list too many times. What are the key reasons for EXIT_FRAME over ENTER_FRAME?

If you want to run code after all of the other ENTER_FRAME listeners, try using a priority of -1.

@player_03, that won’t do on some cases…
For example, in any Sprite:

addEventListener(Event.ENTER_FRAME, enterFrameSprite, false, 0);
stage.addEventListener(Event.ENTER_FRAME, enterFrameStage, false, -1);

The enterFrameStage will always be called first, doesn’t matter the listener order or priority.
A real-word example is a Input class which needs to clean the justPressed/justReleased keys after every frame.

package sourbit.utils;

import openfl.Lib;
import openfl.events.Event;
import openfl.events.KeyboardEvent;
import sourbit.utils.KeyboardInput;

class KeyboardInput
{
	static var input:KeyboardInput;

	var pressedKeys:Array<Bool>;
	var justPressedKeys:Array<Bool>;
	var justReleasedKeys:Array<Bool>;

	static public function register()
	{
		if (input == null)
		{
			input = new KeyboardInput();
		}
	}

	static public function justPressed(key:Int):Bool
	{
		return input.justPressedKeys[key];
	}

	static public function pressed(key:Int):Bool
	{
		return input.pressedKeys[key];
	}

	static public function justReleased(key:Int):Bool
	{
		return input.justReleasedKeys[key];
	}

	static public function released(key:Int):Bool
	{
		return !input.pressedKeys[key];
	}

	function new()
	{
		// the Event.ENTER_FRAME should be Event.EXIT_FRAME
		Lib.current.stage.addEventListener(Event.ENTER_FRAME, onStageEnterFrame);
		Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
		Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onStageKeyUp);
		
		pressedKeys = new Array<Bool>();
		justPressedKeys = new Array<Bool>();
		justReleasedKeys = new Array<Bool>();
	}

	function onStageKeyDown(event:KeyboardEvent)
	{
		if (!pressedKeys[event.keyCode])
		{
			justPressedKeys[event.keyCode] = true;
		}
		pressedKeys[event.keyCode] = true;
	}

	function onStageKeyUp(event:KeyboardEvent)
	{
		pressedKeys[event.keyCode] = false;
		justReleasedKeys[event.keyCode] = true;
	}

	function onStageEnterFrame(event:Event)
	{
		for (i in 0...justPressedKeys.length)
		{
			justPressedKeys[i] = false;
			justReleasedKeys[i] = false;
		}
	}
}

EXIT_FRAME should be added now, we are not handling FRAME_CONSTRUCTED yet, but that can come later on

Thanks :slight_smile:

Thanks @singmajesty!