Created
March 22, 2014 00:16
-
-
Save eternalruler/9699201 to your computer and use it in GitHub Desktop.
zz.util.keyboard
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
// create zz namespace | |
var zz = zz = zz || {}; | |
// create zz.util namespace | |
zz.util = zz.util = zz.util || {}; | |
// create zz.util.keyboard prototype | |
zz.util.keyboard = cc.Class.extend( /** @lends zz.util.keyboard# */ { | |
_keys: {}, | |
/** | |
* Constructor | |
*/ | |
ctor: function() { | |
this._initKeyboard(); | |
_keys = {}; | |
}, | |
_initKeyboard: function() { | |
log('keyboard initialize'); | |
}, | |
// set the key to be down | |
// key is a number or constant | |
keyDown: function(key) { | |
if (this._keys[key] == undefined) { | |
this._keys[key] = []; | |
} | |
this._keys[key]['strokeFromHold'] = this._keys[key]['isDown']; | |
this._keys[key]['isDown'] = true; | |
this._keys[key]['eventJustHappened'] = true; | |
}, | |
// set the key to be up | |
// key is a number or constant | |
keyUp: function(key) { | |
this._keys[key]['isDown'] = false; | |
this._keys[key]['strokeFromHold'] = false; | |
this._keys[key]['eventJustHappened'] = true; | |
}, | |
// get if the key is down | |
// key is a number or constant | |
keyIsDown: function(key) { | |
if (this._keys[key] != undefined) { | |
return this._keys[key]['isDown']; | |
} | |
}, | |
// get if the key was pressed just before this tick | |
// key is a number or constant | |
keyJustPressed: function(key, ignoreHeldStrokes) { | |
if (this._keys[key] != undefined) { | |
if (ignoreHeldStrokes) { | |
return this._keys[key]['isDown'] && this._keys[key]['eventJustHappened'] && !(this._keys[key]['strokeFromHold']); | |
} else { | |
return this._keys[key]['isDown'] && this._keys[key]['eventJustHappened']; | |
} | |
} | |
}, | |
// get if the key was released just before this tick | |
// key is a number or constant | |
keyJustReleased: function(key) { | |
if (this._keys[key] != undefined) { | |
return !this._keys[key]['isDown'] && this._keys[key]['eventJustHappened']; | |
} | |
}, | |
// update the keyboard object at the end of the tick (because 'just pressed' events will be set to 'not just pressed') | |
update: function(dt) { | |
for (k in this._keys) { | |
//log(k +' = '+ this._keys[k][0] + " " + this._keys[k][1]); | |
this._keys[k]['eventJustHappened'] = false; | |
if (!this._keys[k]['isDown']) { | |
this._keys[k] = null; | |
delete this._keys[k]; | |
} | |
} | |
}, | |
// debug: get list of currently tracked keys | |
getListOfPressedKeys: function() { | |
var keyList = ''; | |
for (x in this._keys) { | |
for (y in cc.KEY) { | |
var keyCode = cc.KEY[y]; | |
if (x == keyCode) { | |
keyList += y + ' '; | |
break; | |
} | |
} | |
} | |
return keyList; | |
}, | |
// description of object | |
description: function() { | |
return 'zz.util.keyboard' + JSON.stringify(this) + ';'; | |
} | |
}); | |
debug('loaded', 'zz.util.keyboard'); | |
/* | |
This is a class I wrote for a game which uses Cocos2d-html5. | |
You can see that the class uses the cocos2d function cc.Class.extend(). | |
The code allows the creation of a keyboard object which can store the states of keypresses. | |
The keyboard object can determine if a key is down, up, just pressed, just released, or | |
if the key is being held down and re-firing. | |
I like how useful it is and the functionality it provides to a game developer. | |
I dislike how it is dependent upon using the cocos2d base class prototype. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment