【发布时间】:2017-09-11 04:19:18
【问题描述】:
我正在处理一个 javascript 文件,它可以从四面墙上弹起一个球。我想在球撞到墙上时改变它的颜色...为此我尝试了两个连续的 if 但第二个 if 没有被执行我不知道为什么
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var ballRadius = 10;
var x = canvas.width/2;
var y = canvas.height-30;
var dx = 2;
var dy = -2;
if(dx>0){
alert("dx is greater than 0");
}
if(dx<0){
alert("dx is less than 0");
}
function drawBall() {
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI*2);
ctx.fillStyle = "#0095DD";
ctx.fill();
ctx.closePath();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) {
dx = -dx;
}
if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) {
dy = -dy;
}
x += dx;
y += dy;
}
setInterval(draw, 10);
【问题讨论】:
-
您确定不能使用
else代替 -
我已经尝试过其他...但它不起作用
标签: javascript canvas