Created
October 22, 2018 12:47
-
-
Save twilson90/f96d599d77e68357dc56d144399c4605 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//When it comes to implementing user interfaces (buttons, detecting key presses, etc.), depending on the language & framework it is different. | |
//I was brought up with the concept of events and event listeners. An event is something that happens, a listener is something that will allow you to respond to certain events. | |
//Every game engine/framework will implement a system like this, to allow the app and the user to communicate back and forth. | |
//Some examples of an event would be 'Click' or 'KeyPress'. these would allow you (the coder) to detect what part of the screen the user is clicking or what keys are being pressed. | |
//There are also system events that you would depend on for making the game run at all, like 'EnterFrame' (fired at the beginning of each frame) or 'OnExit' if you want to perform an action (like saving the user's progress) before the app is terminated. | |
//By listening for these events, it's possible to create a simple game loop. | |
//An example of a mouse click listener: | |
function OnClick(e:MouseEvent) { | |
//e will contain all the information you need to know about the mouse event, it is full of useful variables. | |
//e.g. isDown (bool), xPosition (float), yPosition (float), etc. | |
if (e.isDown && buttonRect.Contains(e.xPosition, e.yPosition)) { | |
//do something! | |
} | |
} | |
//An example of a very simple game: | |
class Player extends Sprite { | |
var moveSpeed:Float = 5.0; | |
var speedX:Float = 0.0; | |
var speedY:Float = 0.0; | |
var movingUp:Bool; | |
var movingDown:Bool; | |
var movingLeft:Bool; | |
var movingRight:Bool; | |
// constructor | |
function Player() { | |
// draws a black square, 40px width/height from the center. | |
graphics.beginFill(0x000000); | |
graphics drawRect(-20,-20, 20 20); | |
// setup some event listeners | |
addEventListener(Event.KEY_DOWN, OnKeyDown); | |
addEventListener(Event.KEY_UP, OnKeyUp); | |
} | |
function OnKeyDown(e:KeyEvent) { | |
movingUp = e.keyCode == 87; // W | |
movingDown = e.keyCode == 83; // S | |
movingLeft = e.keyCode == 65; // A | |
movingRight = e.keyCode == 68; // D | |
} | |
function OnKeyUp(e:KeyEvent) { | |
movingUp = e.keyCode != 87; // W | |
movingDown = e.keyCode != 83; // S | |
movingLeft = e.keyCode != 65; // A | |
movingRight = e.keyCode != 68; // D | |
} | |
function Update() { | |
speedX = 0; | |
speedY = 0; | |
if (movingUp) | |
speedY = moveSpeed; | |
if (movingDown) | |
speedY = -moveSpeed; | |
if (movingLeft) | |
speedX = -moveSpeed; | |
if (movingRight) | |
speedX = moveSpeed; | |
this.x += speedX; | |
this.y += speedY; | |
} | |
} | |
var player = new Player(); | |
addEventListener(Event.ENTER_FRAME, OnEnterFrame); | |
// the main game loop, called 60 times a second | |
function OnEnterFrame(e:Event) { | |
player.Update(); | |
} | |
//With a bit of work, you can easily expand the code to a 2 player game, which uses the arrow keys for the 2nd player. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment