Created
August 26, 2012 10:16
-
-
Save tonistiigi/3476859 to your computer and use it in GitHub Desktop.
Moving Lime shapes with keyboard
This file contains 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
//set main namespace | |
goog.provide('keycontrol'); | |
/** | |
* Sample for using keyboard to naturally move around lime objects. | |
*/ | |
//get requirements | |
goog.require('lime.Director'); | |
goog.require('lime.Scene'); | |
goog.require('lime.Circle'); | |
goog.require('goog.events.KeyCodes'); | |
// entrypoint | |
keycontrol.start = function(){ | |
var director = new lime.Director(document.body,1024,768), | |
scene = new lime.Scene(), | |
circle = new lime.Circle().setSize(75, 75).setFill(255,150,0) | |
var SPEED = 0.05, FRICTION = 0.95, MAXSPEED = 10; | |
var ax = 0, ay = 0, vx = 0, vy = 0; | |
goog.events.listen(goog.global, ['keydown', 'keyup'], function (e) { | |
var isdown = e.type == 'keydown'; | |
switch (e.keyCode) { | |
case goog.events.KeyCodes.LEFT: | |
ax = isdown ? -SPEED : 0; | |
break; | |
case goog.events.KeyCodes.UP: | |
ay = isdown ? -SPEED : 0; | |
break; | |
case goog.events.KeyCodes.RIGHT: | |
ax = isdown ? SPEED : 0; | |
break; | |
case goog.events.KeyCodes.DOWN: | |
ay = isdown ? SPEED : 0; | |
break; | |
} | |
}); | |
lime.scheduleManager.schedule(function(dt) { | |
vx += ax * dt; | |
vx *= FRICTION; | |
vy += ay * dt; | |
vy *= FRICTION; | |
var pos = circle.getPosition(); | |
circle.setPosition(pos.x + vx, pos.y + vy); | |
}); | |
scene.appendChild(circle); | |
director.makeMobileWebAppCapable(); | |
// set current scene active | |
director.replaceScene(scene); | |
} | |
//this is required for outside access after code is compiled in ADVANCED_COMPILATIONS mode | |
goog.exportSymbol('keycontrol.start', keycontrol.start); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment