javascript - Multiple event listeners on HTML5 canvas -


i've been trying create game in strictly html5 , javascript , have run issue can't seem wrap head around. in attempt try , avoid using third party classes/libraries, i'm creating own class handling custom buttons within html5 canvas element. got them work , had re-write of script after realizing event listeners kept adding on top of each other every time canvas redrew (i using anonymous function in mousedown event listener before, have since switched different method).

first of all, event listeners use function holds reference whichever button i'm trying use. prototype's mousedownfunc called, checks boundary of button instance's dimensions, , calls referenced onpress() (which overridden method every button uses, each button has custom set of instructions when pressed).

so, if you're still following along (i know, it's bit confusing without seeing full script), problem because event listeners using same function, they're overwriting previous event listener, last button added functions correctly. sum up, how can add multiple event listeners canvas element, use same function without erasing previous event listeners. note i'm trying without use of jquery or other third-party extensions.

if more information needed in regards code it's easier understand, let me know. in advance type of feedback.


edit: perhaps might help. note isn't complete code, contains main points:

adding button:

this.test_button = new createbutton(this, 'test_button'); this.test_button.onpress = function() {     alert('blue button works!'); }; this.test_button.create(200, 50, 30, 200, 'text'); 

when using create() on button, variables checked , stored, array holds onto current buttons (so can referenced @ point). run: this.that.custom_canvas.addeventlistener('mousedown', this.create.prototype.mousedownfunc, false);

when mousedownfunc called, takes place:

createbutton.prototype.create.prototype.mousedownfunc = function(e) {     var mouse_x = e.clientx-this.offsetleft;     var mouse_y = e.clienty-this.offsettop;      // check if mini-button button clicked     if(mouse_x >= test2.x && mouse_y >= test2.y && mouse_x <= (test2.x + test2.width) && mouse_y <= (test2.y + test2.height)){         alert('clicked , working!');         test2.onpress(); // reference saved function     } }; 

currently, test2 reference given object -- it's global var, after these other issues fixed, i'll deal getting rid of global var (i know, know - temporary quick-fix).

maybe instead of event listener each , every possible button, , checking box size within function, create single event calls routing function check on element event occurred, , delegate function


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -