The keyboard event isn't working -- Windows/Neko -- SOLVED

Basically, I am trying to make a platform game, and have done before in other engines, but I can’t seem to get keyboard input in OpenFL. I have 4 objects, a main class, player class, game object class, and engine class. The engine organizes all the game objects, which all must be a child of GameObject. This is run by the main class.

Here is my code for all four files (I apologize for the code dump.):

The Engine

package;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.events.KeyboardEvent; 
// The engine class itself

class Engine extends Sprite
{
	// This is the array that holds all the game objects
	var gameObjects:Array<GameObject>;

	// An array holding all of the keyboard presses
	public var keyDown:Array<Bool>;

	// Create the class
	public function new()
	{
		super();

		// Create an empty array
		gameObjects = new Array<GameObject>();
	}

	// All init does is add the listeners
	public function init()
	{
		// Create the empty keys map
		keyDown = [];

		// This adds the game loop
		this.addEventListener(Event.ENTER_FRAME, gameLoop);

		// Add the key events to the game
		stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
		stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
	}

	// Add an object to the game
	public function addObject(objectToAdd:GameObject)
	{
		// Insert said object at the end of the array
		gameObjects.insert(gameObjects.length, objectToAdd);
	}

	// The function that runs all the object's frame Event
	private function gameLoop(e:Event):Void
	{
		for (i in gameObjects)
		{
			i.frameEvent(this);
		}
	}

	// Set the keys for the game
	private function onKeyDown(evt:KeyboardEvent):Void
	{
    	keyDown[evt.keyCode] = true;
		trace("Thing ran");
	}

	private function onKeyUp(evt:KeyboardEvent):Void
	{
	    keyDown[evt.keyCode] = false;
        trace("Thing ran");
	}
}

The main file

package;
import openfl.display.Sprite;

class Main extends Sprite
{
	var gameEngine:Engine;

	public function new()
	{
		super();

		// Make the engine used everywhere
		gameEngine = new Engine();

		// Add the engine to the display
		addChild(gameEngine);

		// Create a player at 14,14
		var player = new ObjPlayer(14, 14, gameEngine);
	}
}

The base class

package;
import openfl.display.Sprite;

class GameObject extends Sprite
{
	// The class needs to start the Sprite
	public function new(newX:Int, newY:Int, gameEngine:Engine)
	{
		super();
		objectCreate(newX, newY, gameEngine);

		x = newX;
		y = newY;

		// Add the object to the game
		gameEngine.addObject(this);
	}

	// A kind of startup function that is called by new
	public function objectCreate(newX:Int, newY:Int, gameEngine:Engine)
	{
		// This is really mostly done by the child object
	}

	// Some things that will be used by the engine
	public function frameEvent(gameEngine:Engine)
	{
		// This function inheritly does nothing
	}

	// It's important to note that objects must draw themselves. This is not done for them.
	public function drawEvent(gameEngine:Engine)
	{
		// Same with this one
	}
}

And the player

package;
import openfl.display.Bitmap;
import openfl.Assets;

class ObjPlayer extends GameObject
{
	// The player's image is a, well, player...
	var bPlayer:Bitmap;

	// In the init, just get the image and add it to the openfl display
	public override function objectCreate(newX:Int, newY:Int, gameEngine:Engine)
	{
		// Load the player's bitmap
		var tempImage = Assets.getBitmapData("assets/player.png");
		var bPlayer = new Bitmap(tempImage);
		gameEngine.addChild(bPlayer);
	}

	public override function frameEvent(gameEngine:Engine)
	{
		//Move around
		if (gameEngine.keyDown[39])
		{
			x += 5;
		}
		else if (gameEngine.keyDown[37])
		{
			x -= 5;
		}
		if (gameEngine.keyDown[40])
		{
			y += 5;
		}
		else if (gameEngine.keyDown[38])
		{
			y -= 5;
		}
	}
}

I apologize for all that code, I suspect the problem is just with the Engine and Main class, but I know from stack overflow to just give all associated code. I put traces all over to see what was happening, and the values in the keyDown array are all just null. The event is never triggering anyway. I put a trace in the keydown function, and it never prints.

Any help is greatly appreciated.

EDIT: Fixed it myself, I never called the init or waited for it to start…

Makes sene, you might also consider using Map<Int> for the keyDown type, might perform better than an array. Also, please feel free to post if you have any other questions or problems :slight_smile:

I don’t know about Haxe, but I know from other languages maps are almost never faster than arrays. Arrays are linear in memory, but maps have to be looped through to find the value.

I could be wrong because I’m no Haxe master, but I’ll test them both out. Besides, a map definitely makes more sense.

I think it is about how you will access the data.

Map is quick and efficient for key-based access. You will randomly (in no certain order) access or store values, based on a key type. Iterating through all known keys is slower, but if you have a key already, access is quick.

Array is quick and efficient for ordered access. If you will iterate through all of the values, or specific values within an int range of values, Array is ideal, but it is not as efficient if you use more random keys. Setting array[10000] may make the Array 10000 items in size, even though you only have one value. It is also more difficult to tell if a value has been set or is just a default unset value (since you lack the map.exists API).