程序没有检查第二个条件

时间:2014-12-27 00:12:38

标签: java swing if-statement

当我运行我的游戏时,它在对象碰撞的底部工作正常但是,它不会分散那个和另一个。它不是在阅读第二个。我拍了一张照片,你可以看到(x,y)坐标,它符合条件,但我仍然可以移动。

IMG:http://i.stack.imgur.com/PjcHB.png

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Player{

int x = 1; // Location of player
int y = 314; // location of player
int xa = 0; // Representation of where the player goes
int ya = 0; // Representation of where the player goes
int playerWeight = 115;
private int speed = 2;
int[] playerPos = {x, y};
private static final int WIDTH = 30;
private static final int HEIGHT = 30;

private Game game;

public Player(Game game){
    this.game=game;
}

public void move(){
    System.out.println(x + ", " + y);
    x = x + xa;
    y = y + ya;

    if(x + xa < 0) // Left Bounds
        xa = 0;
    if (x + xa > game.getWidth() - WIDTH) // Right Bounds
        xa = 0;
    if (y + ya < 0) // Top Bounds
        ya = 0;
    if(y + ya > game.getHeight() - WIDTH)
        ya = 0;
    if (collision()) // Tile bounds
        y = y - 4;
    if (collision2()){

        if(x > 370 && x < 531 && y < 286){ // Check for 3 values 
            y = y + 4; 
            System.out.println("-x");
            /*
             * Use the gravity() method to determine player fall rate
             */
        }

        else if(x > 370 && x < 531 && y > 223){ // Check for 3 values 
            ya = 0;
            System.out.println("+x");
            /*
             * Use the gravity() method to determine player fall rate
             */
        }

    }
}

// Method to find where player is located
public int[] Playerposition(){
    x = 1;
    y = 300;
    return playerPos;
}

public void Gravity(){


}

public void paint(Graphics2D g2d){
    //Draws player to screen
    g2d.drawImage(getPlayerImg(), x, y, null);
}

public Image getPlayerImg(){
    ImageIcon ic = new ImageIcon("C:/Users/AncientPandas/Desktop/KingsQuest/Misc/Images/Sprites/player.png");
    return ic.getImage();
}

public void keyReleased(KeyEvent e){
    xa = 0;
    ya = 0;
}

public void keyPressed(KeyEvent e){
    if (e.getKeyCode() == KeyEvent.VK_S)
        xa = -speed;
    if (e.getKeyCode() == KeyEvent.VK_F)
        xa = speed;
    if (e.getKeyCode() == KeyEvent.VK_E)
        ya = -speed;
    if (e.getKeyCode() == KeyEvent.VK_D)
        ya = speed;
}

public Rectangle getBoundsPlayer(){
    return new Rectangle(x, y, WIDTH, HEIGHT);
}

private boolean collision(){
    return game.maplayout.getBoundsBlock().intersects(getBoundsPlayer());
}

private boolean collision2(){
    return game.maplayout.getBoundsBlock2().intersects(getBoundsPlayer());
}


}

1 个答案:

答案 0 :(得分:0)

变化:

    else if(x > 370 && x < 531 && y > 223){ // Check for 3 values 
        ya = 0;
        System.out.println("+x");
        /*
         * Use the gravity() method to determine player fall rate
         */
    }

为:

    if(x > 370 && x < 531 && y < 286){ // Check for 3 values 
        y = y + 4; 
        System.out.println("-x");
        /*
         * Use the gravity() method to determine player fall rate
         */
    }

    if(x > 370 && x < 531 && y > 223){ // Check for 3 values 
        ya = 0;
        System.out.println("+x");
        /*
         * Use the gravity() method to determine player fall rate
         */
    }

原因是关键字&#34; else&#34;只有在上面的&#34; if&#34;声明的条件失败/要求未得到满足。例如:

boolean anInteger = 9;
if (anInteger == 1999) {
    // ...
} else {
    // If 'anInteger' is not equal to 9, then execute the following code (within this block)
    // ...
}