In the attached script (part of a framework), custom objects of type scratchBlock are created. They should have a function 'render' assigned, and they do. But aScratchBlock.render() always seems to trigger the render function on the last created object of type scratchBlock.
Any ideas?
HTML Code:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>WebScratch Sandbox</title> <link href="style.css" rel="stylesheet" type="text/css"> <script type="text/javascript" src="editor.js"></script> </head> <body onLoad="app_init()" onMouseMove="app_mouseMove(event)"> <div id="scriptEditor"> </div> </body> </html>Code:function app_init() { scratchBlocks = new Array(); scratchBlockId = 0; scratchBlocks[0] = new scratchBlock(); scratchBlocks[1] = new scratchBlock(); scratchBlocks[2] = new scratchBlock(); scratchBlock_dragging = -1; testvar = 0 } function app_mouseMove(event) { mousex = event.clientX; mousey = event.clientY; testvar ++; window.status = typeof scratchBlock_dragging; if (scratchBlock_dragging != -1) { /*xpos = scratchBlock_dragging.style.left; xpos = xpos.substr(0, xpos.length-2); ypos = scratchBlock_dragging.style.top; ypos = ypos.substr(0, ypos.length-2);*/ xpos = scratchBlock_dragging.x ypos = scratchBlock_dragging.y //alert(typeof scratchBlock_dragging + ' ' + xpos + ' ' + ypos); if (scratchBlock_dragStarted == false) { //avoid drag on regular click or very slight mouse moves scratchBlock_dragStarted = (Math.abs(xpos-xdiff-mousex)>2 || (Math.abs(ypos-ydiff-mousey)>2)) } else { //document.getElementById(scratchBlock_dragging).style.top = (mousey + ydiff) + 'px'; //document.getElementById(scratchBlock_dragging).style.left = (mousex + xdiff) + 'px'; scratchBlock_dragging.x = mousex + xdiff; scratchBlock_dragging.y = mousey + ydiff; scratchBlock_dragging.render(); } } } //scratchBlock Object Definition //actual object definition function scratchBlock() { //Object Properties this.bid = scratchBlockId; this.x = 100 this.y = this.bid*50+50; this.blockType = 'command'; this.color = 'blue'; this.label = 'go to x: [' + Math.round(Math.random()*100) + '] y: [' + Math.round(Math.random()*100) + ']'; //Object Methods //this.render = scratchBlock_render; this.render = scratchBlock_render; this.render(); scratchBlockId ++; } //methods function scratchBlock_render() { oldHTML = this.HTML this.HTML = '<div class="block ' + this.color + '" id="b'+scratchBlockId+'">' + this.label + '</div>'; if (this.DOMobj == undefined) { document.getElementById('scriptEditor').innerHTML += this.HTML; this.DOMobj = document.getElementById('b'+this.bid); //event handling this.DOMobj.setAttribute('onMouseDown','scratchBlock_initDrag('+this.bid+')'); this.DOMobj.setAttribute('onMouseUp','scratchBlock_dragging = -1 '); this.DOMobj.setAttribute('onSelectStart','return false'); //avoid text selection on browser other then Mozilla } if ((this.HTML != undefined)&&(this.HTML != oldHTML)) { document.getElementById('scriptEditor').innerHTML.replace(oldHTML, this.HTML); } this.DOMobj.style.top = this.y + 'px'; this.DOMobj.style.left = this.x + 'px'; } function scratchBlock_initDrag(blockId) { //scratchBlock_dragging contains object scratchBlock scratchBlock_dragging = scratchBlocks[blockId]; //alert(blockId + ' ' + typeof scratchBlock_dragging) scratchBlock_dragStarted = false; xdiff = scratchBlock_dragging.x - mousex; ydiff = scratchBlock_dragging.y - mousey; }


Reply With Quote

Bookmarks