如何在单击组件时调用函数
How to call a function on a click on a component?
function component(width, height, source, x, y) {
this.image = new Image();
this.image.src = source;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.update = function() {
ctx = myGameArea.context;
ctx.drawImage(this.image,
this.x,
this.y,
this.width, this.height);
}
this.newPos = function(a, b, image, h) {
this.x = a;
this.y = b;
this.image.src = image;
this.height = h;
}
}
单击组件后如何调用函数?还是我应该只记录单击的坐标并将它们与组件的坐标进行比较?
在使用HTML5 Canvas时,不幸的是,没有单独的节点可以注册点击事件。您唯一真正的选择是在<canvas>
元素本身上注册一个单击事件,然后使用 X 和 Y 坐标来确定单击的组件。
我不知道
您使用的是香草JS还是特殊引擎,但是如果您可以使用JQuery,也许您必须查看此 https://api.jquery.com/click/,它允许您每次单击组件时执行一个函数下面的代码只是一个关于如何使用该函数的示例,请对其进行测试以确保它适合您
function component(width, height, source, x, y) {
this.image = new Image();
this.image.src = source;
this.width = width;
this.height = height;
this.x = x;
this.y = y;
this.update = function() {
ctx = myGameArea.context;
ctx.drawImage(this.image,
this.x,
this.y,
this.width, this.height);
}
this.newPos = function(a, b, image, h) {
this.x = a;
this.y = b;
this.image.src = image;
this.height = h;
}
$(this).click(function(){
//Code here
});
}
相关文章:
- 如何从模块链中调用函数.导出到节点中
- 调用函数内部的函数
- 在javascript中调用c函数
- DropDownListFor赢得't在更改时调用函数
- Javascript页面调用函数
- 在动画结束之前调用函数
- 允许父窗口在其不同域的子iframe上调用函数
- 运行Infinite Scroll后调用函数时出现问题
- JavaScript:在调用函数的文本输入上按enter键
- 使用大括号和不使用bracs调用函数的区别
- javascript在事件上调用函数
- 从index.html调用函数,该函数无限循环
- 从带参数的字符串变量调用函数中的函数
- 为什么 JS 不在滚动时调用函数
- 是否可以在不更改上下文的情况下调用函数.apply
- 如何在ES6类中使用参数调用函数
- AngularJS:调用函数时编号不更新
- JavaScript中的立即调用函数表达式(IIFE)-传递jQuery
- 在JavaScript中调用函数时自定义此选项
- 调用函数中声明的变量