简体   繁体   English

使用canvas + javascript进行2D碰撞检测

[英]2D Collision detection with canvas + javascript

Hey i'm trying to implement simple game in HTML5 canvas and Javascript. 嘿,我正在尝试在HTML5画布和Javascript中实现简单的游戏。 My problem is that player jumps between platforms. 我的问题是玩家在平台之间跳跃。 Whenever player jumps on platform(i have array with platform positions) i turn off the gravitation so player doesn't fall on ground.Then in my update method(the one which refreshes all the time) I call another method which checks with the for loop whether player jumped on the platform position. 每当玩家在平台上跳跃(我有阵列平台位置)时我会关闭引力,因此玩家不会落在地上。然后在我的更新方法(一直刷新的方法)中我调用另一个方法来检查循环是否玩家跳到平台位置。 The problem is it only works with one platform because when i add more platforms, method loops through platform positions and player falls down. 问题是它只适用于一个平台,因为当我添加更多平台时,方法循环通过平台位置和播放器倒下。 Is there a way I could constantly check all the platform positions not just one at the time? 有没有办法我可以不断检查所有平台位置,而不仅仅是一个? My code which checks whether player is on platform 我的代码检查玩家是否在平台上

 for(i=0;i<platforms.length;i++) {
              if ((player.y <= platforms[i].y) && (player.y >= platforms[i].y - 5) && (player.x >= platforms[i].x) && (player.x <= (platforms[i].x + platforms[i].width + 10)) && (player.jumping == true)) {
                  ground = true;
                  player.jumping=false;
              }
              else if((player.y >= platforms[i].y) && (player.y <= platforms[i].y - 5) && (player.x <= platforms[i].x) && (player.x >= (platforms[i].x + platforms[i].width + 10)) && (player.jumping == false)){
                  ground=false;
              }
              if ((player.x >= platforms[i].x - 10) && (player.x <= (platforms[i].x + platforms[i].width + 10)) && (player.y <= platforms[i].y + platforms[i].height + 10) && (player.y >= platforms[i].y-5)) {
                  player.velY = 0;

              }
              if ((player.y <= platforms[i].y) && (player.x >= platforms[i].x - 10) && (player.x <= (platforms[i].x + platforms[i].width + 10)) && (ground == true)) {
                  player.y = platforms[i].y;
                  player.jumping = false;
              }
          }

Use a flag while you're testing all the platforms. 在测试所有平台时使用标志。 Here's pseudo-code: 这是伪代码:

var isOnPlatform=false;

for(i=0;i<platforms.length;i++) {

    if( testPlayerIsOnThisPlatform ){ isOnPlatform=true; }

}

if(isOnPlatform){ turn off gravity };

You are setting various variables for the current platform (eg you set ground = true, etc) but then the for loop goes onto the next platform and immediately clobbers all the things you set for the previous platform. 您正在为当前平台设置各种变量(例如,您设置ground = true等),然后for循环进入下一个平台并立即破坏您为之前平台设置的所有内容。 You don't seem to do anything to preserve the settings when you find the platform the player was on (such as breaking out of the loop, setting a flag, or something). 当您找到播放器所在的平台时(例如,突破循环,设置标记或其他内容),您似乎没有做任何事情来保留设置。

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

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