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…