Permalink
Browse files

dpad.js added

  • Loading branch information...
1 parent b94c209 commit 5785dfeaf3ef9e1d1f5389dffe7fc0ac9784582b @luizgustavolino luizgustavolino committed Nov 18, 2012
Showing with 144 additions and 5 deletions.
  1. +5 −0 Classes/AppDelegate.js
  2. +5 −5 Classes/GameScene.js
  3. +133 −0 Classes/dpad.js
  4. +1 −0 cocos2d.js
View
@@ -10,11 +10,16 @@ cc.AppDelegate = cc.Application.extend({
},
applicationDidFinishLaunching:function () {
+
var director = cc.Director.sharedDirector();
director.setDisplayFPS(false);
director.setAnimationInterval(1.0 / 60);
+
var scene = GameScene.scene();
director.runWithScene(scene);
+
+ dpad.setup();
+
return true;
},
View
@@ -14,18 +14,18 @@ var GameScene = cc.Layer.extend({
this.addChild(this.helloLabel);
this.schedule(this.update);
-
return true;
},
update:function(dt){
+ dpad.update();
+
+ if(dpad.keys.up.state == dpad.stateKeyDown){
+ console.log("Up Down!");
+ }
- },
-
- onKeyDown:function(e){
- console.log("key event")
}
});
View
@@ -0,0 +1,133 @@
+dpad = {};
+
+dpad.stateIdle = 0;
+dpad.stateKeyDown = 1;
+dpad.stateKeyPressed = 2;
+dpad.stateKeyUp = 3;
+
+dpad.keys = {
+ actionUp:{
+ state: 0,
+ next: 0,
+ dirty: false,
+ bindingCode: 73,
+ },
+ actionDown:{
+ state: 0,
+ next: 0,
+ dirty: false,
+ bindingCode: 75,
+ },
+ actionRight:{
+ state: 0,
+ next: 0,
+ dirty: false,
+ bindingCode: 76,
+ },
+ actionLeft:{
+ state: 0,
+ next: 0,
+ dirty: false,
+ bindingCode: 74,
+ },
+ up:{
+ state: 0,
+ next: 0,
+ dirty: false,
+ bindingCode: 87
+ },
+ down:{
+ state: 0,
+ next: 0,
+ dirty: false,
+ bindingCode: 83
+ },
+ left:{
+ state: 0,
+ next: 0,
+ dirty: false,
+ bindingCode: 65
+ },
+ right:{
+ state: 0,
+ next: 0,
+ dirty: false,
+ bindingCode: 68
+ }
+
+}
+
+dpad.update = function () {
+
+ for(var aKeyTag in dpad.keys){
+
+ var aKey = dpad.keys[aKeyTag];
+
+ if(aKey.state == dpad.stateKeyUp && !aKey.next){
+ aKey.state = dpad.stateIdle;
+ }else if(aKey.state == dpad.stateKeyDown && !aKey.next){
+ aKey.state = dpad.stateKeyPressed;
+ }else{
+ if(aKey.next){
+ aKey.state = aKey.next;
+ }
+ }
+
+ aKey.next = null;
+ aKey.dirty = false;
+
+ }
+
+}
+
+dpad.setup = function(){
+ document.onkeydown = dpad.keyDown;
+ document.onkeyup = dpad.keyUp;
+}
+
+dpad.keyDown = function (e) {
+ dpad.processKeyEvent("down",e);
+}
+
+dpad.keyUp = function (e) {
+ dpad.processKeyEvent("up", e);
+}
+
+dpad.processKeyEvent = function (type, evt){
+
+ var x = null;
+ if(window.event) x = event.keyCode;
+ else x = evt.charCode ? evt.charCode : evt.keyCode;
+
+ for(var aKeyTag in dpad.keys){
+
+ var aKey = dpad.keys[aKeyTag];
+ if(aKey.bindingCode == x){
+ if(!aKey.dirty){
+ aKey.dirty = true;
+
+ if(aKey.state == dpad.stateIdle){
+ if(type == "down"){
+ aKey.next = dpad.stateKeyDown;
+ }
+ }else if (aKey.state == dpad.stateKeyDown) {
+ if(type == "down"){
+ aKey.next = dpad.stateKeyPressed;
+ }else if(type == "up"){
+ aKey.next = dpad.stateKeyUp;
+ }
+ }else if (aKey.state == dpad.stateKeyPressed) {
+ if(type == "up"){
+ aKey.next = dpad.stateKeyUp;
+ }
+ }
+ }else{
+ if(type == "up"){
+ aKey.next = dpad.stateKeyUp;
+ }
+ }
+ }
+ }
+
+}
+
View
@@ -49,5 +49,6 @@ cc.loadjs = function (filename) {
cc.loadjs('Lib/Cocos2d-html5-canvasmenu-min.js');
cc.loadjs('Classes/AppDelegate.js');
+cc.loadjs('Classes/dpad.js');
cc.loadjs('Classes/GameScene.js');
cc.loadjs('Classes/PirateMutiny.js');

0 comments on commit 5785dfe

Please sign in to comment.