简体   繁体   English

在html5画布内进行边界检测时翻转坐标

[英]fliping coordinate when border detection inside html5 canvas

this is my jsfiddle : http://jsfiddle.net/whZ44/1/ 这是我的jsfiddle: http : //jsfiddle.net/whZ44/1/

 std1.x += std1.sp;
 if ((std1.x + std1.w) > cw) {
     std1.x *= -std1.sp; 
 }

when the block hit the right border it should flip it x and start moving to the left and if the block hit the left border it should flip it x and move to the right. 当块碰到右边界时,应将其翻转x并开始向左移动;如果块碰到左边界,则应将其翻转x并向右移动。 I'm having a problem with the code and I would appreciate some help 我的代码有问题,请多多帮助

if ((std1.x + std1.w) > cw || std1.x < 0) {
    // Invert the speed when bat reaches boundaries
    std1.sp *= -1; 
}

Updated fiddle 更新小提琴

This works now: http://jsfiddle.net/whZ44/5/ 现在可以使用: 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM