简体   繁体   English

Java游戏:检查交叉口和跳跃

[英]Java Game: Checking Intersections and Jumping

I need help with checking for intersections in a game I'm creating: When a character collides with a single obstacle on the screen all works as intended, but when multiple obstacles are added to the screen the character can only jump off of the last obstacle that is being checked for collision. 我需要检查正在创建的游戏中的交叉点的帮助:当角色与屏幕上的单个障碍物碰撞时,所有对象均按预期工作,但是当向屏幕中添加多个障碍物时,角色只能跳出最后一个障碍物正在检查碰撞。 The character still collides properly with the other obstacles (doesn't fall through, cant walk through) but can't jump off of them. 角色仍会与其他障碍物正确碰撞(不会掉入,无法穿过),但不能从其他障碍物上跳下来。 Obs is an arraylist of obstacles. Obs是一个障碍列表。 Ground is the boolean that determines if the character is allowed to jump. Ground是确定是否允许角色跳跃的布尔值。

public void checkIntersect(ArrayList<Obstacle> obs){
    for(Obstacle a: obs){


        if(a.getLeft().intersects(this.getRight())){
            if(getDx()>0){
                sidecollision=true;
                setDx(0);
                this.x-=1.5f;
            }
        } if (a.getRight().intersects(this.getLeft())){
                sidecollision = true;
                setDx(0);
                this.x+=1.5f;
        } if((a.getTop()).intersects(this.getBottom())){
            ground=true;
            setDy(0);
            this.y-=.10f;
        } else if(!(a.getTop()).intersects(this.getBottom())){ 
                ground = false;

                //return ground;        
        } if(a.getBottom().intersects(this.getTop())){
            ground=false;
            setDy(0);

            this.y+=.1f;
        }

    }
} 

and how collision is being checked on the game component: 以及如何在游戏组件上检查碰撞:

bg.update(quote);
        win.fill(clear);
        bg.drawBG(win);
        for(Obstacle o: obs){
            o.draw(win);
            o.update(bg);
        }
        if(quote.isVisible()){

                quote.movedrawProtag(win, keys);
                quote.checkIntersect(obs);
        }

The most likely cause of your bug is subsequent calls to checkIntersect() overriding the value of ground . 造成错误的最可能原因是随后对checkIntersect()调用覆盖了ground的值。 This results in the value of ground only reflecting the last call to checkIntersect() . 这将导致ground的值仅反映对checkIntersect()的最后一次调用。 To fix this bug, you should set ground to a default value of either true or false before making any calls to checkIntersect() . 要修复此错误,应在调用checkIntersect()之前将ground的默认值设置为truefalse Then, modify the checkIntersect() method so that it can only set ground to the non-default value. 然后,修改checkIntersect()方法,使其只能将ground设置为非默认值。 When ground should be at its default value, it does not need to be set. ground应为其默认值时,无需设置。

Do something like this. 做这样的事情。 I'm using a default value of false . 我使用的默认值为false

public void checkIntersect(ArrayList<Obstacle> obs){
    for(Obstacle a: obs){


        if(a.getLeft().intersects(this.getRight())){
            if(getDx()>0){
                sidecollision=true;
                setDx(0);
                this.x-=1.5f;
            }
        } if (a.getRight().intersects(this.getLeft())){
                sidecollision = true;
                setDx(0);
                this.x+=1.5f;
        } if((a.getTop()).intersects(this.getBottom())){
            ground=true;
            setDy(0);
            this.y-=.10f;
        } else if(!(a.getTop()).intersects(this.getBottom())){ 
                // ground = false;

                //return ground;        
        } if(a.getBottom().intersects(this.getTop())){
            // ground=false;
            // setDy(0);

            // this.y+=.1f;
        }

    }
} 

When you check for collisions: 检查冲突时:

bg.update(quote);
        win.fill(clear);
        bg.drawBG(win);
        for(Obstacle o: obs){
            o.draw(win);
            o.update(bg);
        }

        // Set ground to its default value
        quote.ground = false;  

        if(quote.isVisible()){

                quote.movedrawProtag(win, keys);
                quote.checkIntersect(obs);
        }

        // If ground has not been set to true, do the default action
        if (quote.ground == false) {
            setDy(0);
            quote.y += .1f;
        }

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

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