Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
added onbuttondown and onbuttonup
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomax committed Nov 11, 2012
1 parent 9d80e82 commit 0454d19
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions js/game.js
Expand Up @@ -513,6 +513,60 @@ function fromBox2DValue(v) { return BOX2D_PIXELS_PER_METER*v; }
}
}
},
// fixme: REFACTOR
onbuttondown: function() {
var element = this.el;
if(element.hasAttribute("onbuttondown")) {
var bounceCode = element.getAttribute("onbuttondown");
// form the list of function calls
var split = bounceCode.split(")"),
code = [];
split.forEach(function(s) {
if(!s.trim()) return;
code.push("OnBounceAPI." + s.trim() + ");");
});
// create a new function that runs through the calls,
// and is triggered as an onbounce (called by box2d contact listener)
try {
var fn = new Function(code.join("\n"));
try { fn(); }
catch(runtimeError) {
// runtime error in code: throw it up
throw(runtimeError);
}
} catch(syntaxRrror) {
// syntax error in Function - don't run it.
console.log("syntax error");
}
}
},
// fixme: REFACTOR
onbuttonup: function() {
var element = this.el;
if(element.hasAttribute("onbuttonup")) {
var bounceCode = element.getAttribute("onbuttonup");
// form the list of function calls
var split = bounceCode.split(")"),
code = [];
split.forEach(function(s) {
if(!s.trim()) return;
code.push("OnBounceAPI." + s.trim() + ");");
});
// create a new function that runs through the calls,
// and is triggered as an onbounce (called by box2d contact listener)
try {
var fn = new Function(code.join("\n"));
try { fn(); }
catch(runtimeError) {
// runtime error in code: throw it up
throw(runtimeError);
}
} catch(syntaxRrror) {
// syntax error in Function - don't run it.
console.log("syntax error");
}
}
},
keyHandlers: {},
handleKey: function(key) {
var fn = this.keyHandlers[key];
Expand Down Expand Up @@ -760,6 +814,16 @@ function fromBox2DValue(v) { return BOX2D_PIXELS_PER_METER*v; }
}
}());

worldParent.onmousedown = function() {
bars.forEach(function(b) { b.onbuttondown(); });
balls.forEach(function(b) { b.onbuttondown(); });
};

worldParent.onmouseup = function() {
bars.forEach(function(b) { b.onbuttonup(); });
balls.forEach(function(b) { b.onbuttonup(); });
};

worldParent.onchange = (function(element) {
return function() {
// handle gravity
Expand Down

0 comments on commit 0454d19

Please sign in to comment.