简体   繁体   English

碰撞检测在吃豆人游戏中不起作用

[英]Collision Detection Not working in Game of Pac-Man

I am making my own game of pac-man and am having difficulty getting the collision detection between my surrounding div and the "pac-man". 我正在制作自己的pac-man游戏,很难在周围的div和“ pac-man”之间进行碰撞检测。

Here is the css for my the character (which is a square for now) and the border surrounding the entire game. 这是角色的CSS(目前为正方形)和整个游戏的边框。

div#pac{
    background-color: white;
    width: 35px;
    height: 35px;
    position: fixed;
    left: 90px;
    top: 135px;
    margin: 2px;
}
div#shell{
    border: 1px solid white;
    width: 80%;
    height: 75%;
    top: 85px;
    left: 130px;
    text-align: center;
    position: fixed;
    background-color: #DEB887;
}

next I thought I should show you the html although it is pretty basic 接下来,我认为我应该向您展示html,尽管它非常基本

<div id="shell">
<div id="pac"></div>
</div>

finally the javascript. 最后是javascript。 which is where the problem starts, and it can be quite complicated for some to follow.Also I use my own functions that I made called getStyle and setStyle which do exactly what they sound like they do. 这是问题开始的地方,有些人可能会很复杂。我还使用了我自己创建的名为getStyle和setStyle的函数,它们的功能完全像听起来一样。

//move function for pac-man.
function move() {
    var upDown = parseInt(getStyle("pac", "top"));
    var leftRight = parseInt(getStyle("pac","left"));
    var width = parseInt(getStyle("shell", "width")) - parseInt(getStyle("pac", "width"));
    var height = parseInt(getStyle("shell", "height")) - parseInt(getStyle("pac", "height"));

    //arrow keys move down
    if (arrowKey == 40) {
        upDown = upDown + 2;
    }
    //arrow keys move up
    else if (arrowKey == 38) {
        upDown = upDown - 2;
    }    
    else if (arrowKey == 37) { //arrow keys move left
        leftRight = leftRight - 2;
    }
    else if (arrowKey == 39) { //arrow keys move right
        leftRight = leftRight + 2;
    }
    if (contains("pac", "shell")) {
        //arrow keys bounce from frame down
        if (upDown <= 85) {
            upDown += 2;
        }
        else if (upDown >= height + 35) {
            upDown += -2;
        }
        else if (leftRight <= 130){
            leftRight += 2;
        }
        else if (leftRight >= width + 35){
            leftRight += -2;
        }
    }
    //update to new location
    setStyle("pac", "top", upDown + "px");
    setStyle("pac", "left", leftRight + "px");
}

in the function above I start by defining my variables. 在上面的函数中,我首先定义了变量。

next I create the actual movement function to tell it to move in a certain direction when a certain key is pressed. 接下来,我创建实际的移动功能,以告诉它在按下某个键时向某个方向移动。

then I do my collision testing which is where the problem occurs. 然后我进行碰撞测试,这就是问题所在。

this problem is very erratic and random. 这个问题很不稳定而且很随机。 when I run the function the collision detection between the top and the left works just fine. 当我运行该功能时,顶部和左侧之间的碰撞检测效果很好。 but when I try to move to the right or bottom sides of the surrounding div it cuts me off about 50-80 pixels short. 但是,当我尝试移至周围div的右侧或底部时,它使我缩短了约50-80像素。

and you would think just add like 50-80 more pixels to the detection but NOPE that's when it gets even weirder because if I add any more pixels to it, the collision detection just disappears and the pac-man is able to fly right off the screen like there is no div there at all 并且您会认为只是向检测中添加了50-80个像素,但NOPE变得更奇怪了,因为如果我向其添加更多像素,碰撞检测就会消失,而pac-man能够立即飞出屏幕就像根本没有div

please, anyone, I have been trying to solve this for 2 days and I can't find a solution. 拜托了,任何人,我已经尝试解决了2天,但找不到解决方案。 Am I missing something obvious? 我是否缺少明显的东西? I tried changing some of the css to stuff like position relative and the left and top attributes but nothing helped. 我尝试将某些CSS更改为诸如position relative和left和top属性之类的东西,但没有任何帮助。

the full source code and the included library(which has the getStyle and setStyle function is below) https://github.com/nicholaskorte/Javascript-pac-man-game/tree/master 完整的源代码和包含的库(下面具有getStyle和setStyle函数) https://github.com/nicholaskorte/Javascript-pac-man-game/tree/master

Found my own answer. 找到了我自己的答案。

The issue resided within the containing function within my library. 该问题驻留在我的库中的包含函数中。

i changed this... 我改变了这个...

function contains(id1, id2) {
    var left1 = parseInt(getStyle(id1, "left"));
    var top1 = parseInt(getStyle(id1, "top"));
    var width1 = parseInt(getStyle(id1, "width"));
    var width2 = parseInt(getStyle(id2, "width"));
    var height1 = parseInt(getStyle(id1, "height"));
    var height2 = parseInt(getStyle(id2, "height"));

    if (left1 <= 5) {
        return false;
    }
    else if (left1 > width2 - width1) {
        return false;
    }
    else if (top1 < 0) {
        return false;
    }
    else if (top1 > height2 - height1) {
        return false;
    }
    else {
        return true;
    }
}

to this... 为此...

function collisionFrame(id1, id2) {
    var left1 = parseInt(getStyle(id1, "left"));
    var right1 = left1 + parseInt(getStyle(id1, "width"));
    var top1 = parseInt(getStyle(id1, "top"));
    var bottom1 = top1 + parseInt(getStyle(id1, "height"));

    var left2 = parseInt(getStyle(id2, "left"));
    var right2 = left2 + parseInt(getStyle(id2, "width"));
    var top2 = parseInt(getStyle(id2, "top"));
    var bottom2 = top2 + parseInt(getStyle(id2, "height"));

    if (left1 < left2 + 4) {//from left
        return false;
    }
    else if (right1 > right2 - 4) {//from right
        return false;
    }
    if (top1 < top2 + 4) {//from top
        return false;
    }
    if (bottom1 > bottom2 - 4) {//from bottom
        return false;
    }
    else {
        return true;
    }
}

...and it worked ...而且有效

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

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