这是我的jsfiddle:http://jsfiddle.net/whZ44/1/
std1.x += std1.sp;
if ((std1.x + std1.w) > cw) {
std1.x *= -std1.sp;
}
当块碰到右边框时,它应该将其翻转x
并开始向左移动,如果块碰到左边框,则应将其翻转x
并向右移动。我遇到了代码问题,我希望得到一些帮助
答案 0 :(得分:1)
if ((std1.x + std1.w) > cw || std1.x < 0) {
// Invert the speed when bat reaches boundaries
std1.sp *= -1;
}
答案 1 :(得分:1)
现在可以使用:http://jsfiddle.net/whZ44/5/
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;