Skip to content
This repository has been archived by the owner on Apr 10, 2020. It is now read-only.

Commit

Permalink
Focus on Input, pipe arrows to game
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Nadeau committed Jun 29, 2014
1 parent e20c90f commit 529644f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
47 changes: 47 additions & 0 deletions app/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,28 @@ $(document).ready(function(){
input_editor.renderer.setShowGutter(false);
input_editor.focus();

input_editor.on("blur",function(){
input_editor.focus();
});

window.input_editor = input_editor;

input_editor.container.addEventListener("keydown", function(e){
if (e.keyCode == 13) {
var value = input_editor.getSession().getValue();
input_editor.setValue("");
postToServer(value)
} else if (e.keyCode > 36 && e.keyCode < 40) {
e.preventDefault();
e.stopImmediatePropagation();

if (e.keyCode == 37) {
cursors.left.processKeyDown(pressKey(37));
} else if (e.keyCode == 38) {
cursors.up.processKeyDown(pressKey(38));
} else if (e.keyCode == 39) {
cursors.right.processKeyDown(pressKey(39));
}
}
}, true);

Expand Down Expand Up @@ -67,3 +82,35 @@ function nextDialogue(characterName, dialogueArray, deleteDialogue) {
}
}
}

function pressKey(code) {
var keyboardEvent = document.createEvent("KeyboardEvent");
Object.defineProperty(keyboardEvent, 'keyCode', {
get : function() {
return this.keyCodeVal;
}
});

Object.defineProperty(keyboardEvent, 'which', {
get : function() {
return this.keyCodeVal;
}
});

var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";

keyboardEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
code, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
keyboardEvent.keyCodeVal = code;
return keyboardEvent;
}
5 changes: 2 additions & 3 deletions app/js/rubysadventure.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ function setupSprites() {
addSprite(game, 'sun', 0.5, 0.65);

for (var i = 0; i < 3; i++) {
addSprite(game, 'cloud1', -0.05 + i, randomHeightPercent(0.0, 0.3));
addSprite(game, 'cloud2', 0.6 + i, randomHeightPercent(0.5, 0.7));
addSprite(game, 'cloud1', -0.05 + i, randomHeightPercent(0.0, 0.4));
addSprite(game, 'cloud2', 0.6 + i, randomHeightPercent(0.5, 0.6));
addSprite(game, 'birds', 0.48 + i, Math.random());
}

Expand Down Expand Up @@ -172,7 +172,6 @@ function update() {
facing = 'idle';
}
}

}

function randomHeightPercent(lower, higher) {
Expand Down

0 comments on commit 529644f

Please sign in to comment.