Javascript 在html5画布内进行边界检测时翻转坐标
这是我的JSFIDLE: 当块碰到右边框时,它应该将其翻转Javascript 在html5画布内进行边界检测时翻转坐标,javascript,jquery,html,canvas,Javascript,Jquery,Html,Canvas,这是我的JSFIDLE: 当块碰到右边框时,它应该将其翻转x并开始向左移动,如果块碰到左边框,它应该将其翻转x并向右移动。我对代码有问题,如果((std1.x+std1.w)>cw | std1.xcw | | newX
x
并开始向左移动,如果块碰到左边框,它应该将其翻转x
并向右移动。我对代码有问题,如果((std1.x+std1.w)>cw | std1.x<0),我将非常感谢您的帮助{
if ((std1.x + std1.w) > cw || std1.x < 0) {
// Invert the speed when bat reaches boundaries
std1.sp *= -1;
}
//蝙蝠到达边界时反转速度
std1.sp*=-1;
}
现在可以使用:
var leftBorderX=0;//设置为左边框的x值
var newX=std1.x+std1.w;
//如果新的x值超过边界(左或右),则反转速度
如果(newX>cw | | newX
if ((std1.x + std1.w) > cw || std1.x < 0) {
// Invert the speed when bat reaches boundaries
std1.sp *= -1;
}
var leftBorderX = 0; // set to the x value of your left border
var newX = std1.x + std1.w;
// if the new x value exceeds the borders (left or right) then invert the speed
if(newX > cw || newX < leftBorderX) std1.sp *= -1;