简体   繁体   English

Javascript:碰撞检测帮助(HTML5 Canvas)

[英]Javascript: Collision Detection help (HTML5 Canvas)

I am trying to throw together a platformer in HTML5 using the new canvas feature. 我正在尝试使用新的canvas功能将HTML5中的平台程序组合在一起。 I was implementing a somewhat simple way to do collision with rectangles, and it worked until I added multiple rectangles. 我实现了一种与矩形碰撞的简单方法,直到我添加了多个矩形后,该方法才起作用。 Basically I have a function which adds a new object to an array with all the atributes needed to draw the rectangle. 基本上,我有一个函数,该函数将一个新对象添加到具有绘制矩形所需的所有属性的数组中。 The collision detection loops through the array and checks and corrects collisions. 冲突检测遍历数组,并检查并纠正冲突。 When I added another rectangle on top, it freaks out when you are standing on the bottom one, and then you jump and hit the bottom of the top one. 当我在顶部添加另一个矩形时,当您站在底部的矩形上时,它会变怪,然后跳到顶部的底部。

Here is the main javascript file , and here is a demo that I threw up on my site. 这是主要的javascript文件 ,这是我在自己的网站上发布的一个演示

Your collision detection code is OK, the only problem is that it is not working the way you want. 您的冲突检测代码还可以,唯一的问题是它无法按照您想要的方式工作。 A simple fix could be only check the collision with platforms when the player is falling down. 一个简单的解决方法是仅在玩家摔倒时检查与平台的碰撞。 (Line 188) (第188行)

if ((player.yvel > 0) && (player.y + player.height > rect[i].y) && (player.y < rect[i].y + rect[i].height))

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

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