【发布时间】:2020-05-27 13:10:41
【问题描述】:
我是编码新手,我需要一些帮助。我有一个开始按钮<button id="startBtn" onclick="draw()">START</button> 我想要这个开始按钮来调用我的绘图函数
function draw() {
drawWalther();
rain();
if (rightPressed && (waltherX + waltherWidth) < 865) {
waltherX += waltherDx;
} else if (leftPressed && waltherX > -54){
waltherX -= waltherDx;
}
if (y + dy > c.height - 100 || y + dy < 0) {
dy = 0;
}
if (rightPressed && (waltherheadX + waltherheadWidth) < 900) {
waltherheadX += waltherheadDx;
} else if (leftPressed && waltherheadX > 50){
waltherheadX -= waltherheadDx;
}
x += dx;
y += dy;
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
当然,当我加载游戏时,它会加载绘图功能,而无需我单击开始按钮 那么如何在单击开始按钮而不是在重新加载游戏时调用该函数
【问题讨论】:
-
请考虑使用stack snippet 或其他可用工具(如jsfiddle.net 或codepen.io)为我们创建当前问题的Minimal, Reproducible Example。
-
删除
onClick属性然后添加这个:document.querySelector("#startBtn").addEventListener("click", draw})最后删除最后一行requestAnimationFrame(draw);
标签: javascript function button