{"author_name":"Dream Of Sleeping","cat":"LD #27","comments":[{"author_name":"JPatrick","time":"August 21, 2013 10:53 am","epoch":1377100380,"text":"Please use pastebin or gist next time.","spam":"N"},{"author_name":"Dream Of Sleeping","time":"August 21, 2013 11:22 am","epoch":1377102120,"text":"Ok sorry, I will do in future.","spam":"N"}],"epoch":1377093720,"likes":2,"metadata":{"p_key":"41728","p_author":"Dream Of Sleeping","p_authorkey":"25172","p_urlkey":"77351","p_title":"Just declaring my rubbish base code","p_cat":"LD #27","p_event":"LD27","p_time":"1377093720","p_likes":"2","p_comments":"2","p_status":"UPD5","us_key":"25172","us_name":"Dream Of Sleeping","us_username":"dream-of-sleeping","event_start":"1377216000","event_key":"18","event_name":"LD27"},"text":"<p>This will be my first Ludum Dare. I have entered three 48 hour game jams recently (the weekly Fight Magic Run competition), and won one of them,\u00a0 so I&#8217;m pretty wamed up. I&#8217;ve been using createjs. The only base code that I will use that I wrote myself will probably be my key input class. I wrote it some time ago and it&#8217;s\u00a0 pretty messy but it&#8217;s worked pretty well. So I thought I had better declare it. Also my loading bar class which was written last minute and rubbish, but again it does the job.\u00a0 I don&#8217;t have a github, so I&#8217;m pasting the code below. You can use it if you like, but I doubt you will want to! haha<br \/>\n <span id=\"more-268790\"><\/span><br \/>\n njc.KeyInput = function() {<br \/>\n this.keyActions = [];<br \/>\n this.keyEvents = [];<br \/>\n this.loggingInput = false;<\/p>\n <p>&nbsp;<\/p>\n <p>njc.KeyInput.prototype.register = function(elementId) {<br \/>\n var element = document.getElementById(elementId);<br \/>\n var referenceName = this;<br \/>\n element.addEventListener(&#8220;keydown&#8221;, function(event) {referenceName.onEvent(event)}, false);<br \/>\n element.addEventListener(&#8220;keyup&#8221;, function(event) {referenceName.onEvent(event)}, false);<br \/>\n element.addEventListener(&#8220;keypress&#8221;, function(event) { referenceName.iHateOpera(event)}, false);<br \/>\n };<\/p>\n <p>\/\/ opera doesn&#8217;t support pevent default on keydown! edit: or it didn&#8217;t I have no idea anymore what this is for<br \/>\n njc.KeyInput.prototype.iHateOpera = function(event) {<\/p>\n <p>if (!this.loggingInput) {<br \/>\n return;<br \/>\n }<\/p>\n <p>if (this.getActionByCode(event.keyCode)) {<br \/>\n event.preventDefault();<br \/>\n }<br \/>\n };<\/p>\n <p>njc.KeyInput.prototype.onEvent = function(event) {<\/p>\n <p>if (!this.loggingInput) {<br \/>\n return;<\/p>\n <p>}<br \/>\n var keyAction = this.getActionByCode(event.keyCode);<\/p>\n <p>if (keyAction) {<br \/>\n event.preventDefault();<br \/>\n }<br \/>\n this.keyEvents.push(event);<br \/>\n };<\/p>\n <p>njc.KeyInput.prototype.poll = function() {<br \/>\n this.resetPress();<\/p>\n <p>for (var i = 0; i &lt; this.keyEvents.length; i++) {<br \/>\n this.processEvent(this.keyEvents[i]);<br \/>\n }<\/p>\n <p>this.keyEvents.length = 0;<br \/>\n };<\/p>\n <p>njc.KeyInput.prototype.processEvent = function(e) {<br \/>\n keyAction = this.getActionByCode(e.keyCode);<\/p>\n <p>if (keyAction != undefined) {<\/p>\n <p>if (e.type == &#8220;keydown&#8221;) {<br \/>\n keyAction.press();<br \/>\n } else if (e.type == &#8220;keyup&#8221;) {<br \/>\n keyAction.release();<br \/>\n }<br \/>\n };<br \/>\n };<\/p>\n <p>njc.KeyInput.prototype.addKeyAction = function(name, code, rapidFireTime, initialDelay) {<br \/>\n var keyAction = new njc.KeyAction(name, code);<\/p>\n <p>if (rapidFireTime) {<br \/>\n keyAction.rapidFire = true;<br \/>\n keyAction.rapidFireTime = rapidFireTime;<br \/>\n }<\/p>\n <p>if (initialDelay) {<br \/>\n keyAction.initialDelay = initialDelay;<\/p>\n <p>}<\/p>\n <p>this.keyActions.push(keyAction);<\/p>\n <p>return this;<br \/>\n };<\/p>\n <p>njc.KeyInput.prototype.getActionByCode = function(keyCode) {<\/p>\n <p>for (var i = 0; i &lt; this.keyActions.length; i++) {<br \/>\n if (this.keyActions[i].code == keyCode) {<br \/>\n return this.keyActions[i];<br \/>\n }<br \/>\n };<br \/>\n };<\/p>\n <p>njc.KeyInput.prototype.key = function(keyName) {<\/p>\n <p>for (var i = 0; i &lt; this.keyActions.length; i++) {<br \/>\n if (this.keyActions[i].name == keyName) {<br \/>\n return this.keyActions[i];<br \/>\n }<br \/>\n };<br \/>\n return undefined;<br \/>\n };<\/p>\n <p>njc.KeyInput.prototype.resetPress = function() {<br \/>\n for (var i = 0; i &lt; this.keyActions.length; i++) {<br \/>\n var keyAction = this.keyActions[i];<br \/>\n keyAction.pressed = false;<br \/>\n keyAction.released = false;<br \/>\n }<br \/>\n };<\/p>\n <p>njc.KeyInput.prototype.logInput = function(log) {<br \/>\n this.loggingInput = log;<br \/>\n this.keyEvents.length = 0;<br \/>\n \/\/ reset all keys when log input is enabled or disabled to make sure<br \/>\n \/\/ game has no unwanted actions.<br \/>\n for (var i = 0; i &lt; this.keyActions.length; i++) {<br \/>\n this.keyActions[i].fullReset();<br \/>\n }<br \/>\n };<br \/>\n };<\/p>\n <p>njc.KeyAction = function(name, code) {<br \/>\n this.name = name;<br \/>\n this.code = code;<\/p>\n <p>this.rapidFire = false;<br \/>\n this.rapidFireTime;<br \/>\n this.initialDelay = 0;<br \/>\n this.firstPress = false;<br \/>\n this.timeStamp;<\/p>\n <p>this.isDown = false;<br \/>\n this.pressed = false;<br \/>\n this.released = false;<\/p>\n <p>njc.KeyAction.prototype.fullReset = function() {<br \/>\n this.isDown = false;<br \/>\n this.pressed = false;<br \/>\n this.released = false;<br \/>\n };<\/p>\n <p>njc.KeyAction.prototype.press = function() {<\/p>\n <p>if (!this.isDown) {<br \/>\n this.isDown = true;<br \/>\n this.pressed = true;<br \/>\n this.firstPress = true;<\/p>\n <p>if (this.rapidFire) {<br \/>\n this.timeStamp = new Date().getTime();<br \/>\n }<br \/>\n }<br \/>\n };<\/p>\n <p>njc.KeyAction.prototype.release = function() {<br \/>\n this.isDown = false;<br \/>\n this.released = true;<br \/>\n };<\/p>\n <p>njc.KeyAction.prototype.wasReleased = function() {<br \/>\n return this.released;<br \/>\n };<\/p>\n <p>njc.KeyAction.prototype.wasPressed = function() {<\/p>\n <p>if (this.rapidFire &amp;&amp; this.isDown &amp;&amp; this.enoughTimePassed()) {<br \/>\n this.timeStamp = new Date().getTime();<br \/>\n this.firstPress = false;<br \/>\n this.pressed = true;<br \/>\n }<\/p>\n <p>return this.pressed;<br \/>\n };<\/p>\n <p>njc.KeyAction.prototype.enoughTimePassed = function() {<br \/>\n var time = this.rapidFireTime;<\/p>\n <p>if (this.firstPress &amp;&amp; this.initialDelay != 0) {<br \/>\n time = this.initialDelay;<br \/>\n }<\/p>\n <p>if (new Date().getTime() &#8211; this.timeStamp &gt;= time) {<br \/>\n return true;<br \/>\n }<\/p>\n <p>return false;<br \/>\n };<br \/>\n };<\/p>\n <p>&nbsp;<\/p>\n <p>&nbsp;<\/p>\n <p>&nbsp;<\/p>\n <p>\/\/ loading scene<br \/>\n njc.LoadingScene = function(stage, numberToLoad) {<br \/>\n this.barColor = &#8220;white&#8221;;<br \/>\n this.fontColor = &#8220;white&#8221;;<br \/>\n this.fontStyle = &#8220;30px arial, sans-serif&#8221;;<br \/>\n this.barWidth = 400;<br \/>\n this.barHeight = 40;<br \/>\n this.text = &#8220;Loading&#8230;&#8221;;<br \/>\n this.backgroundColor = &#8220;rgb(30, 30, 30)&#8221;;<\/p>\n <p>this.stage = stage;<br \/>\n this.numberToLoad = numberToLoad;<br \/>\n this.assetsLoaded = 0;<\/p>\n <p>this.background = new createjs.Shape();<\/p>\n <p>this.background.graphics.beginFill(this.backgroundColor).drawRect(0, 0, this.stage.canvas.width, this.stage.canvas.height);<\/p>\n <p>this.barText = new createjs.Text(this.text, this.fontStyle, this.fontColor)<br \/>\n this.barText.x = (this.stage.canvas.width &#8211; this.barText.getMeasuredWidth() ) \/ 2;<br \/>\n this.barText.y = 330;<\/p>\n <p>this.bar = new createjs.Shape();<br \/>\n this.bar.x = (this.stage.canvas.width &#8211; this.barWidth) \/ 2;<br \/>\n this.bar.y = (this.stage.canvas.height &#8211; this.barHeight) \/ 2;<\/p>\n <p>this.barOutLine = new createjs.Shape();<br \/>\n this.barOutLine.x = this.bar.x;<br \/>\n this.barOutLine.y = this.bar.y;<br \/>\n this.barOutLine.graphics.beginStroke(this.barColor).drawRect(0, 0, this.barWidth, this.barHeight);<\/p>\n <p>this.stage.addChild(this.background);<br \/>\n this.stage.addChild(this.bar);<br \/>\n this.stage.addChild(this.barOutLine);<br \/>\n this.stage.addChild(this.barText);<br \/>\n this.stage.update();<br \/>\n };<\/p>\n <p>njc.LoadingScene.prototype.onComplete = function() {<br \/>\n this.stage.removeChild(this.background);<br \/>\n this.stage.removeChild(this.bar);<br \/>\n this.stage.removeChild(this.barOutLine);<br \/>\n this.stage.removeChild(this.barText);<br \/>\n };<\/p>\n <p>njc.LoadingScene.prototype.render = function() {<br \/>\n this.bar.graphics.clear();<\/p>\n <p>var percentageLoaded =\u00a0 this.assetsLoaded \/ this.numberToLoad * 100;<br \/>\n var width = (percentageLoaded \/ 100) * this.barWidth;<br \/>\n this.bar.graphics.beginFill(this.barColor).drawRect(0, 0, width, this.barHeight);<br \/>\n this.stage.update();<br \/>\n };<\/p>\n <p>njc.LoadingScene.prototype.onAssetLoaded = function() {<\/p>\n <p>this.assetsLoaded++;<\/p>\n <p>if (this.assetsLoaded &gt; this.numberToLoad) {<br \/>\n console.log(&#8220;Error&#8221;);<br \/>\n return;<br \/>\n }<\/p>\n <p>this.render();<br \/>\n };<\/p>","time":"August 21st, 2013 9:02 am","title":"Just declaring my rubbish base code"}