Java-2d平台游戏:碰撞不起作用(奇怪的bug)

时间:2015-09-07 06:39:02

标签: java swing user-interface jframe jpanel

我有一个非常奇怪的问题。 我正在使用Java制作一个简单的2d平台游戏。 与播放器和平台的碰撞检测不起作用。 但奇怪的是,当我在屏幕上打印一些内容以查看是否执行了碰撞if语句时,碰撞会起作用o_O 也许这有点令人困惑,请看我的代码。

主要课程(我认为这很好):

import javax.swing.*;

public class Main extends JFrame{
    private static final long serialVersionUID = 1L;
    GameClass gc = new GameClass();

    public Main(){
        setSize(gc.WIDTH,gc.HEIGHT);
        setResizable(false);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Flying GoatZ!");
        add(new GameClass());
        setLocationRelativeTo(null);
        setVisible(true);
    }
    public static void main(String[] args) {
        new Main();
    }
}

GameClass类:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class GameClass extends JPanel implements ActionListener, KeyListener, MouseListener{

//OBJECTS
Text text = new Text();
openImages open_img = new openImages();
Random ran = new Random();

//VARIABLES
static final long serialVersionUID = 1L;
final int WIDTH = 800;
final int HEIGHT = 600;
int goatx = WIDTH/2;
int goaty = 350;
int goatspeed = 0;
int fallspeed = 15;
int maxy = 150;
boolean up = false;
boolean flying = true;
ArrayList<Integer> xes = new ArrayList<Integer>();
ArrayList<Integer> yes = new ArrayList<Integer>();

//FPS SETTER AND KEYLISTENERS
public GameClass(){
    Timer time = new Timer(15, this);
    time.start();
    this.addKeyListener(this);
    this.setFocusable(true);
    open_img.openImage();
}

public void update(){
    Collision();
    goatx += goatspeed;
    if(up){
        if(goaty > maxy){
            goaty -= 5; 
        }else{
            up = false;
        }
    }else
        if(goaty < 350)
            goaty += fallspeed; 
}

public void print(String msg){
    System.out.println(msg);
}

public void platformDrawing(Graphics g,int x,int y,int x1,int y1, int x2, int y2){
    g.setColor(Color.RED);
    g.drawImage(open_img.block,x, y, null);
    g.drawImage(open_img.block,x1, y1, null);
    g.drawImage(open_img.block,x2, y2, null);
    xes.addAll(Arrays.asList(x,x1,x2));
    yes.addAll(Arrays.asList(y,y1,y2));
}

//HERE IS THE COLLISION METHOD(I NEED THE PLAYER TO STAND STILL WHEN IT IS ON THE PLATFORM.
public void Collision(){
    for(int x : xes){
        for(int y : yes){
            if( ( (goatx > x-20) && (goatx < (x + 150)) ) && ( (goaty+open_img.goat.getHeight(null)) <= y ) ){
                //print("TEST");
                fallspeed = 0;  
            }else{
                fallspeed = 10;
            }
        }
    }
}

//ALL TEH DRAWING
public void paintComponent(Graphics g){
    //MAP
    g.setColor(Color.CYAN);
    g.fillRect(0,0,WIDTH,HEIGHT);
    g.setColor(Color.ORANGE);
    g.fillRect(0, HEIGHT-100, WIDTH, 100);
    g.setColor(Color.GREEN);
    g.fillRect(0, HEIGHT-125, WIDTH, 25);
    //PLAYER & PLATFORMS
    platformDrawing(g,50,350,300,350,600,350);
    g.drawImage(open_img.goat, goatx, goaty, null);
    g.dispose();
}

//THIS IS EXECUTED EVERYTIME
public void actionPerformed(ActionEvent e){
    update();
    repaint();
}

//KEY DETECTION
public void keyPressed(KeyEvent e){
    if(e.getKeyCode() == KeyEvent.VK_LEFT){
        goatspeed = -5;
    }

    if(e.getKeyCode() == KeyEvent.VK_RIGHT){
        goatspeed = 5;
    }

    if(e.getKeyCode() == KeyEvent.VK_SPACE){
        if(flying){
            flying = false;
            up = true;
        }
    }
}

public void keyReleased(KeyEvent e){
    if(e.getKeyCode() == KeyEvent.VK_LEFT){
        goatspeed = 0;
    }

    if(e.getKeyCode() == KeyEvent.VK_RIGHT){
        goatspeed = 0;
    }

    if(e.getKeyCode() == KeyEvent.VK_SPACE){
        flying = true;
    }
}

//SOME STUFF THAT YOU HAVE TO IGNORE LEL   
public void keyTyped(KeyEvent e){}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}

}

我不明白为什么一份印刷声明会有所作为...... 任何帮助都是适用的,谢谢! 哦,抱歉英语不好或问题不清楚。

0 个答案:

没有答案