keyboard
12-17-2013, 12:43 PM
G'day all.
I have the following code (I've removed parts and only left in the relevant sections)(sorry if there are any syntax errors but there aren't in the full version):
$.fn.game = function(args) {
for(var key in args.objects) {
if(typeof args.objects[key].exec.mousedown == "function") {
$("#" + key).mousedown(function() {
args.objects[key].exec.mousedown();
});
}
}
}
$("#gameParent").game({
objects : {
main : {
hooks : {
mousedown : function() {
console.log("test");
}
}
},
enemy : {
hooks : {
mousedown : function() {
}
}
}
}
});
The idea is to the ability to create objects (for a game) then hook events on them.
When clicking on either of the objects, the hooks.mousedown() is called twice for the enemy object and not at all for the main object.
This is because args.objects[key].exec.mousedown(); runs onmousedown, which is triggered after both the objects have been looped over. This means that key will only ever point to the last object on the list.
I have no clue how to work around/restructure to fix this.
Any suggestions? :)
I have the following code (I've removed parts and only left in the relevant sections)(sorry if there are any syntax errors but there aren't in the full version):
$.fn.game = function(args) {
for(var key in args.objects) {
if(typeof args.objects[key].exec.mousedown == "function") {
$("#" + key).mousedown(function() {
args.objects[key].exec.mousedown();
});
}
}
}
$("#gameParent").game({
objects : {
main : {
hooks : {
mousedown : function() {
console.log("test");
}
}
},
enemy : {
hooks : {
mousedown : function() {
}
}
}
}
});
The idea is to the ability to create objects (for a game) then hook events on them.
When clicking on either of the objects, the hooks.mousedown() is called twice for the enemy object and not at all for the main object.
This is because args.objects[key].exec.mousedown(); runs onmousedown, which is triggered after both the objects have been looped over. This means that key will only ever point to the last object on the list.
I have no clue how to work around/restructure to fix this.
Any suggestions? :)