keyPressed与墙对象

时间:2015-05-29 11:51:50

标签: java awt bounds

我正在慢慢疯狂,我正在尝试制作玩家类的对象。

package hra.objects;

import hra.Handler;
import hra.abstracts.GameObject;
import hra.enums.Direction;
import hra.enums.ID;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;


public class Player extends GameObject{
    private final Handler handler;
    public boolean keyDown;

    private int HEALTH=100;private int MANA=100; private int STAMINA=100;

    public Player(int x, int y, ID id, Direction dir,Handler handler) {
        super(x, y, id , dir);
        this.handler=handler;
    }

    @Override
    public void tick() {
        x+=velX;
        y+=velY;
        Collision();
    }
    public void Collision(){
        for (GameObject tempObject : handler.object) {
            if(tempObject.getId()==ID.Enemy){
                if(getBounds().intersects(tempObject.getBounds())){
                    setHEALTH(getHEALTH() - 2);
                }   
            }
            if(this.wallCheck(dir)==true){
                keyDown=false;
            }
        }


    }

    @Override
    public void render(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(x, y, 32, 32);
        g.setColor(Color.red);
        g.drawRect(x, y-5, 32, 5);
        g.drawRect(x, y+32, 32, 5);
        g.drawRect(x-5, y, 5, 32);
        g.drawRect(x+32,y, 5, 32);
        g.drawRect(x, y, 31, 31);
    }

    @Override
    public Rectangle getBounds() {
        return new Rectangle(x,y,32,32);
    }
    @Override
    public boolean wallCheck(Direction dir) {
        for (GameObject tempObject : handler.object) {
            if(tempObject.getId()!=this.id){
                if (dir==Direction.UP){
                    return new Rectangle(x,y-5,32,5).intersects(tempObject.getBounds());
                }
                else if (dir==Direction.DOWN){
                    return new Rectangle(x,y+32,32,5).intersects(tempObject.getBounds());
                }
                else if (dir==Direction.LEFT){
                    return new Rectangle(x-5,32,5,32).intersects(tempObject.getBounds());
                }
                else if(dir==Direction.RIGHT){
                    return new Rectangle(x+32,32,5,32).intersects(tempObject.getBounds());
                }
            }
        }
        return false;
    }
    public int getHEALTH() {
        return HEALTH;
    }

    public void setHEALTH(int HEALTH) {
        this.HEALTH = HEALTH;
    }

    public int getMANA() {
        return MANA;
    }

    public void setMANA(int MANA) {
        this.MANA = MANA;
    }

    public int getSTAMINA() {
        return STAMINA;
    }

    public void setSTAMINA(int STAMINA) {
        this.STAMINA = STAMINA;
    }

}

我在主游戏机制中使用方法KeyInput作为keyListener

package hra;

import hra.abstracts.GameObject;
import hra.enums.Direction;
import hra.enums.ID;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter{
    private final Handler handler;
    private final boolean[] keyDown =new boolean[8];
    public KeyInput(Handler handler){
        this.handler=handler;


    }



    @Override
    public void keyPressed(KeyEvent e){
        int key= e.getKeyCode();

        for (GameObject tempObject : handler.object) {

            if(tempObject.getId()==ID.Player){

                if(key==KeyEvent.VK_UP){
                    tempObject.setDir(Direction.UP);
                    if(tempObject.wallCheck(Direction.UP)==true) return;
                    else tempObject.setVelY(-1);
                }
                if(key==KeyEvent.VK_DOWN){
                    tempObject.setDir(Direction.DOWN);
                    if(tempObject.wallCheck(Direction.DOWN)==true) return;
                    else tempObject.setVelY(1);
                }
                if(key==KeyEvent.VK_LEFT){
                    tempObject.setDir(Direction.LEFT);
                    if(tempObject.wallCheck(Direction.LEFT)==true) return;
                    else tempObject.setVelX(-1);
                }
                if(key==KeyEvent.VK_RIGHT){
                    tempObject.setDir(Direction.RIGHT);
                    if(tempObject.wallCheck(Direction.RIGHT)==true) return;
                    else tempObject.setVelX(1);       
                }
                keyReleased(e);
            }
    }

    @Override
    public void keyReleased(KeyEvent e){
        int key=e.getKeyCode();

        for (GameObject tempObject : handler.object) {
            if(tempObject.getId()==ID.Player){
                if(key==KeyEvent.VK_UP)   tempObject.setVelY(0);
                if(key==KeyEvent.VK_DOWN) tempObject.setVelY(0);
                if(key==KeyEvent.VK_LEFT) tempObject.setVelX(0);
                if(key==KeyEvent.VK_RIGHT)tempObject.setVelX(0);
            }

        }
    }
}

当我按住键时,对象播放器直接穿过其他对象 而当我已经按下并按下键时,它不会让我动起来。 它就像关键的监听器没有考虑到返回语句,当与播放器前面的矩形和另一个对象存在切口时

0 个答案:

没有答案