在浏览器中查看网页时,以下功能出现此控制台错误。
function drawStartButton(){
image.onload = function(){
context.drawImage(image, 260.5, 60);
image.on("mousdown", function(){
drawLevelOneElements();
}
});
image.src = "StartButton.png";
/** Now I need to add an event listener to the area of the canvas on
on which the button image is displayed, in order to 'listen' for
a click on the button */
var boundingBox = myGameCanvas.getBoundingClientRect();
//var mouseX = (mouse_event.clientX-boundingBox.left) * (myGameCanvas.width/boundingBox.width);
//var mouseY = (mouse_event.clientY-boundingBox.top) * (myGameCanvas.height/boundingBox.height);
boundingBox.onmousemove = function(e){
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
var pixels = context.getImageData(mouseX, mouseY, 1, 1);
}
/** There maybe more than one pixel at this location so use a loop
to test whether any of the pixels have an alpha value greater than
0. With pixel data, 3 is alpha, so check data[3] and every fourth
element in data after that. */
//for (var i=3; i<pixels.data.length; i+=4;){
/** If a non- zero alpha is found, stop and return "true"- the click
was on a part of the canvas that has colour on it. */
// if(pixels.data[i]!=0) return true;
//}
/** If the function gets here, then the mouse click wasn't on a painted
part of the canvas. */
//return false;
/**myGameCanvas.getElementById("StartButton").onClick = function(e){
drawLevelOneElements();
} */
}
错误发生在从第二行开始的函数结尾处。最初,我只是以
};
开头这一行,但在收到此错误后,将其更改为});
但是,当我重新加载页面时,画布仍然是空白的,即使我在指示的位置插入了
)
,控制台仍在抱怨相同的错误。我查看了关于此错误的一些建议主题,但似乎都没有表明我需要做些什么才能使函数在画布上显示它们的含义。有人有什么建议吗?
最佳答案
image.onload = function(){
context.drawImage(image, 260.5, 60);
image.on("mousdown", function(){
drawLevelOneElements();
}
});
需要是:
image.onload = function(){
context.drawImage(image, 260.5, 60);
image.on("mousdown", function(){
drawLevelOneElements();
});
};
注意最后一个
)
关于javascript - 参数列表后缺少),即使我在指示的位置添加了),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10283741/