【问题标题】:Java Game: Checking Intersections and JumpingJava 游戏:检查交叉点和跳跃
【发布时间】:2013-06-09 08:22:35
【问题描述】:

我需要帮助检查我正在创建的游戏中的交叉点:当角色与屏幕上的单个障碍物发生碰撞时,一切都会按预期进行,但当屏幕上添加多个障碍物时,角色只能跳下正在检查碰撞的最后一个障碍物。角色仍然可以正确地与其他障碍物发生碰撞(不会跌落,无法通过),但无法跳下它们。 Obs 是障碍物的数组列表。 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;
        }

    }
} 

以及如何在游戏组件上检查碰撞:

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);
        }

【问题讨论】:

  • 欢迎堆栈溢出。请您编辑您的帖子以澄清您正在寻求帮助的问题。您希望您的代码会发生什么以及正在发生什么?如有必要,您可能会考虑为您担心的案例添加一些示例数据。祝你好运!
  • 1) 您已经描述了一个问题以及如何解决问题,但到目前为止还没有提出任何问题(更不用说具体的、可回答的问题了)。您的问题是什么? 2)查看Shape based collision detection。 3) 为了尽快获得更好的帮助,请发帖SSCCE

标签: java game-physics


【解决方案1】:

您的错误最可能的原因是随后对checkIntersect() 的调用覆盖了ground 的值。这导致ground 的值仅反映对checkIntersect() 的最后一次调用。要修复此错误,您应该在调用checkIntersect() 之前将ground 设置为默认值truefalse。然后,修改checkIntersect() 方法,使其只能将ground 设置为非默认值。当ground为默认值时,不需要设置。

做这样的事情。我使用默认值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;
        }

    }
} 

当您检查碰撞时:

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;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 2014-11-12
    相关资源
    最近更新 更多