0

I am slowly going insane, I am trying to make objects of player class.

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

}

An I am using the a method KeyInput as a keyListener in my main game mechanism

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

        }
    }
}

When I hold the key the object player passes right through other objects whereas when I already colide and press the key it won't let me move. it's like the key listener doesnt take into consideration the return statement when the there is a colision with the rectangle infront of the player and the other object

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Consider using the [key bindings API](https://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) over `KeyListener`, it will solve one of the headaches you might suffer from. Instead of trying to update the state within the key event, set a flag indicating the current key state and use a separate "game loop" to update the state, this will solve the issue of the staggered key events – MadProgrammer May 29 '15 at 11:58
  • Are you checking collisions every time you update the Player's position or just when you press a key? Try drawing your collision boxes and see if where they are intersecting. Also, try debugging and setting some breakpoints in the keyPressed method to see what it's doing. – Mouscellaneous May 29 '15 at 12:01
  • See [this](http://stackoverflow.com/questions/22741215/how-to-use-key-bindings-instead-of-key-listeners/). – user1803551 May 29 '15 at 13:23
  • @Mouscellaneous yes I tried to draw them out in the player render method as you see, and yeah that is what I tried to achieve, the update check every time not just when I press the key. So far so good but I still didn't achieve it :P@MadProgrammer thank you for suggesting I am going to try immidiatelly! Thank you all for mentioning this article! I figured it out somehow with the help of you. it is still laggy but I implemented another thread with its own loop in KeyInput class, though it's just a little bit laggy it work almost impecably – Andrej Jaššo May 30 '15 at 08:42
  • It's a good idea to just set the "dir" and "velx"/"vely" attributes when a key is pressed or released. In the main game loop, you should then check for collision and move the player accordingly. You shouldn't need to check for collision when the key is pressed. – Mouscellaneous Jun 02 '15 at 08:28

0 Answers0