角色与墙壁碰撞

时间:2015-09-07 23:44:34

标签: java slick2d

我的问题是,当我的角色移动到地图的一侧时,它会慢慢地移动到墙上(即使但是当角色死掉时,这个停止。你能告诉我有什么改变我的代码所以这不会发生吗?

package Slick.jar;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Play extends BasicGameState{

    //initializing variables
    Animation bucky, movingUp, movingDown, movingLeft, movingRight;
    Image worldMap;
    boolean quit = false;
    int[] duration = {200,200};
    float buckyPositionX = 0;
    float buckyPositionY = 0;
    float shiftX = buckyPositionX + 320;
    float shiftY = buckyPositionY + 160;
    boolean notPaused = true;
    boolean running = false;
    float speedL = 0;
    float speedR = 0;
    float speedU = 0;
    float speedD = 0;
    boolean ableToMoveL = true;
    boolean ableToMoveR = true;
    boolean ableToMoveU = true;
    boolean ableToMoveD = true;
    int health = 1000;
    boolean dead = false;
    float bounceL = speedL/2;  
    float bounceR = speedR/2;
    float bounceU = speedU/2;
    float bounceD = speedD/2;


    public Play(int state){

    }

    public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
        //initializing images
        worldMap = new Image("res/world.png");
        Image[] walkUp = {new Image("res/buckysBack.png"), new Image("res/buckysBack.png")};
        Image[] walkDown = {new Image("res/buckysFront.png"), new Image("res/buckysFront.png")};
        Image[] walkLeft = {new Image("res/buckysLeft.png"), new Image("res/buckysLeft.png")};
        Image[] walkRight = {new Image("res/buckysRight.png"), new Image("res/buckysRight.png")};
        //creating animations
        movingUp = new Animation(walkUp, duration, false);
        movingDown = new Animation (walkDown, duration, false);
        movingLeft = new Animation (walkLeft, duration, false);
        movingRight = new Animation (walkRight, duration, false);
        bucky = movingDown;
    }   
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
        //drawing images onto canvas
        worldMap.draw(buckyPositionX, buckyPositionY);
        bucky.draw(shiftX, shiftY);
        g.drawString("Buckys X: "+buckyPositionX+"\n Y: "+buckyPositionY, 400, 200);
        g.drawString("Health: " + health, 400, 250);
        g.drawString("speedL: " + speedL , 10, 50);
        g.drawString("speedR: " + speedR , 10, 75);
        g.drawString("speedU: " + speedU , 10, 100);
        g.drawString("speedD: " + speedD , 10, 125);
        //seeing if player wants to quit
        if(quit==true){
            g.drawString("Resume (R)", 250, 100);
            g.drawString("Main Menu (M)", 250, 150);
            g.drawString("Quit Game (Q)", 250, 200);
            if(quit==false){
                g.clear();
            }   
        }
        if(health == 0)
            dead = true;
        if(dead){
            g.drawString("You are dead", 250, 100);
            ableToMoveR = false;
            ableToMoveL = false;
            ableToMoveU = false;
            ableToMoveD = false;
            speedU = 0;
            speedD = 0;
            speedL = 0;
            speedR = 0;
            g.drawString("Press (R) to respawn", 250, 125);
            if(!dead)
                g.clear();
        }
    }   

    public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
        Input input = gc.getInput();
        float bounceL = speedL/2;  
        float bounceR = speedR/2;
        float bounceU = speedU/2;
        float bounceD = speedD/2;
        if(health == 0){
            ableToMoveR = false;
            ableToMoveL = false;
            ableToMoveU = false;
            ableToMoveD = false;
            dead = true;
        }
        //creating velocity movement
        if(ableToMoveR){
            buckyPositionX += speedR;
            if(!ableToMoveR)
                speedR = 0.1f;
        }
        if(ableToMoveL){
            buckyPositionX += speedL;
            if(!ableToMoveL)
                speedL = 0.1f;
        }
        if(ableToMoveU){
            buckyPositionY += speedU;
            if(!ableToMoveU)
                speedU = 0.1f;
        }
        if(ableToMoveD){
            buckyPositionY += speedD;
            if(!ableToMoveD)
                speedD = 0.1f;
        }
        //creating "slow-down" system
        if(!input.isKeyDown(input.KEY_W)){
            if(speedU >= 0)
                speedU -= .01f;
            if(speedU > 0 && speedU < .01f)
                speedU = 0;
        } 
        if(!input.isKeyDown(input.KEY_S)){
            if(speedD <= 0)
                speedD += .01f;
            if(speedD > 0 && speedD < .01f)
                speedD = 0;
        }
        if(!input.isKeyDown(input.KEY_A)){
            if(speedL >= 0)
                speedL -= .01f;
            if(speedL > 0 && speedL < .01f)
                speedL = 0;
        }
        if(!input.isKeyDown(input.KEY_D)){
            if(speedR <= 0)
                speedR += .01f;
            if(speedR < 0 && speedR > -.01f)
                speedR = 0;
        }
        //creating crash detection
        if(buckyPositionX<-840){
            speedR = 0.01f;
            if(health > 0)
                health--;
            ableToMoveR = false;
        } else
            ableToMoveR = true;
        if(buckyPositionY<-600){
            speedD = 0.01f;
            if(health > 0)
                health--;
            ableToMoveD = false;
        } else
            ableToMoveD = true;
        if(buckyPositionY>162){
            speedU = -0.01f;
            if(health > 0)
                health--;
            ableToMoveU = false;
        } else
            ableToMoveU = true;
        if(buckyPositionX>320){
            speedL = -0.01f;
            if(health > 0)
                health--;
            ableToMoveL = false;
        } else
            ableToMoveL = true;

            //seeing if player wants to run
        if(input.isKeyDown(input.KEY_LSHIFT))
            running = true;
        else
            running = false;
        //seeing if paused
        if(notPaused == true){
            if(ableToMoveU){ 
                //letting you move/run up
                if(input.isKeyDown(input.KEY_W)&&running==false ){
                        bucky = movingUp;
                        if(speedU < 5)
                            speedU += .005f;
                    }
                if(input.isKeyDown(input.KEY_W)&&running==true ){
                    bucky = movingUp;
                    if(speedU < 5)
                        speedU += .01f;
                }
            }
            //letting you move/run down
            if(ableToMoveD){
                if(input.isKeyDown(input.KEY_S)&&running==false){
                    bucky = movingDown;
                    if(speedD > -5)
                        speedD -= .005f;
                    }
                if(input.isKeyDown(input.KEY_S)&&running==true){
                    bucky = movingDown;
                    if(speedD > -5)
                    speedD -= .01f;
                }
            }
            //letting you move/run left
            if(ableToMoveL){
                if(input.isKeyDown(input.KEY_A)&&running==false){
                    bucky = movingLeft;
                    if(speedL < 5)
                        speedL += .005f;
                    }
                if(input.isKeyDown(input.KEY_A)&&running==true){
                    bucky = movingLeft;
                    if(speedL < 5)
                        speedL += .01f;
                }
            }
            //letting you move/run right
            if(ableToMoveR){
                if(input.isKeyDown(input.KEY_D)&&running==false){
                    bucky = movingRight;
                    if(speedR > -5)
                        speedR -= .01f;
                    } 
                if(input.isKeyDown(input.KEY_D)&&running==true){
                    bucky = movingRight;
                    if(speedR > -5)
                    speedR -= .02f;
                        }
                    }
                }
            //escape        
            if(input.isKeyDown(input.KEY_ESCAPE)){
                quit = true;
                notPaused = false;
            }

            if(dead){
                if(input.isKeyDown(input.KEY_R)){
                    dead = false;
                    health = 1000;
                    buckyPositionX = 0;
                    buckyPositionY = 0;
                }
            }
            //menu is up
            if(quit==true){
                if(input.isKeyDown(input.KEY_R)){
                    quit = false;
                    notPaused = true;
                }
                if(input.isKeyDown(input.KEY_M)){
                    sbg.enterState(0);
                }
                if(input.isKeyDown(input.KEY_Q)){
                    System.exit(0);
                }
            }
        if(input.isKeyDown(input.KEY_X))
                health = 0;
    }

    public int getID(){
        return 1;
    }
}

P.S。还有另外两个课程,但他们在这个问题上没有发挥作用,但如果你需要它们,那就问问。

P.P.S。如果您需要更多信息,请询问。

1 个答案:

答案 0 :(得分:0)

我不确定因为缩进不完美但我认为这些行中存在问题

if(!input.isKeyDown(input.KEY_W)){
    if(speedU >= 0)
        speedU -= .01f;
    if(speedU > 0 && speedU < .01f)
        speedU = 0;
} 

如果未按下键,则speedU == 0 THEN speedU - = .01f;所以最后的速度是U == - .01f。它可以解释为什么它仍然会移动。

为您提供更多建议

  • 始终将括号放在if和else
  • 周围
  • 发布时始终正确缩进,这样可以为您提供更多回答的机会

最后这些线路并不好,因为你测试的是与你输入if

的相反
if(ableToMoveL){ // TRUE
    buckyPositionX += speedL;
    if(!ableToMoveL) //  FALSE
        speedL = 0.1f;
}