Friday, April 3, 2015

KeyPressed KeyReleased Issues




Hello Guys. So When I as Adding Methods To Make My Player Move. It Didn't Work And Im Not Quite Sure Why. I've debugged the program and 've been searching for the error for the last few dails but to no prevail. So I was hoping that you guys could help me spot the issue. I have a seperate keyInput class that is handling the movements. Now let me just say that the class is not the issue, meaning that it is something wrong with the connection between my sextVelX and setVelY variable. I set it up so that i can press escape to exit the game in that same class and it works just fine. I can't seem to find the issue although I know where the error is coming from as I said previously. The reason I haven't been able to find it yet is because I am fairly new to java gaming. So I'm going to show you three classes now that I think the error might be coming from.




Game.java






Java Code:






package com.Tobysmith10.neon.window;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;

import com.Tobysmith10.neon.framework.KeyInput;
import com.Tobysmith10.neon.framework.ObjectId;
import com.Tobysmith10.neon.objects.Player;

public class Game extends Canvas implements Runnable{

private static final long serialVersionUID = -5748136944288826691L;

Random rand = new Random();
private boolean running = false;
private Thread thread;

public static int WIDTH, HEIGHT;

//Object
Handler handler;

private void init(){
WIDTH = getWidth();
HEIGHT = getHeight();


handler = new Handler();

handler.addObject(new Player(100, 100, handler, ObjectId.Player));

handler.createLevel();

this.addKeyListener(new KeyInput(handler));

}

public synchronized void start()
{

if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}




public void run()
{
init();
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
updates++;
delta--;
}
render();
frames++;

if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("FPS: " + frames + " TICKS: " + updates);
frames = 0;
updates = 0;
}
}

System.out.println("Thread Has Begun");


}

private void tick()
{

handler.tick();

}

private void render()
{

BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
this.createBufferStrategy(3);
return;
}

Graphics g = bs.getDrawGraphics();


g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());

handler.render(g);



g.dispose();
bs.show();


}



public static void main(String args[]){

new Window(800, 600, "Neon Platformer Game Prototype", new Game());


}


}




Player.java





Java Code:






package com.Tobysmith10.neon.objects;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.LinkedList;

import com.Tobysmith10.neon.framework.GameObject;
import com.Tobysmith10.neon.framework.ObjectId;
import com.Tobysmith10.neon.window.Handler;

public class Player extends GameObject {

private float width = 48, height = 96;
private float gravity = 0.5f;
private final float MAX_SPEED = 10;
private Handler handler;

public Player(float x, float y, Handler handler, ObjectId id) {
super(x, y, id);
this.handler = handler;

}

public void tick(LinkedList<GameObject> object) {
x += velX;
y += velY;

if(falling || jumping){

velY += gravity;

if(velY > MAX_SPEED)
velY = MAX_SPEED;

}

Collision(object);
}



private void Collision(LinkedList<GameObject> object){
for(int i =0; i < handler.object.size(); i++){

GameObject tempObject = handler.object.get(i);

if(tempObject.getId() == ObjectId.Block){

if(getBounds().intersects(tempObject.getBounds())){

y = tempObject.getY() - height;
velY = 0;
falling = false;
jumping = false;
}

}

}

}

public void render(Graphics g) {

g.setColor(Color.BLUE);
g.fillRect((int)x, (int)y, (int)width, (int)height);

Graphics g2d = (Graphics2D) g;
g.setColor(Color.RED);
((Graphics2D) g2d).draw(getBounds());
((Graphics2D) g2d).draw(getBoundsRight());
((Graphics2D) g2d).draw(getBoundsLeft());
((Graphics2D) g2d).draw(getBoundsTop());
}

public Rectangle getBounds() {
return new Rectangle((int) ((int)x+ (width/2) -((width/2)/2)), (int) ((int)y+height/2), (int)width/2, (int)height/2);
}

public Rectangle getBoundsTop() {
return new Rectangle((int) ((int)x+ (width/2) -((width/2)/2)), (int)y, (int)width/2, (int)height/2);
}

public Rectangle getBoundsRight() {
return new Rectangle((int) ((int)x+width - 5), (int)y+5, (int)5, (int)height-10);
}

public Rectangle getBoundsLeft() {
return new Rectangle((int)x, (int)y+5, (int)5, (int)height-10);
}

}




KeyInput.java



Java Code:






package com.Tobysmith10.neon.framework;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import com.Tobysmith10.neon.window.Handler;

public class KeyInput extends KeyAdapter
{

Handler handler;

public KeyInput(Handler handler){
this.handler = handler;

}

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

for(int i = 0; i < handler.object.size(); i++){
if(handler.object.get(i).getId() == ObjectId.Player){

if(key == KeyEvent.VK_D) handler.object.get(i).setVelX(5);
if(key == KeyEvent.VK_A) handler.object.get(i).setVelX(-5);
}

}

if(key == KeyEvent.VK_ESCAPE){
System.exit(1);
}
}

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

for(int i = 0; i < handler.object.size(); i++){
if(handler.object.get(i).getId() == ObjectId.Player){

if(key == KeyEvent.VK_D) handler.object.get(i).setVelX(0);
if(key == KeyEvent.VK_A) handler.object.get(i).setVelX(0);
}

}
}

}




Now I can't seem to locate where exactly the error is coming from, but I can locate the variable it is coming from, which is setVelX and setVelY. Thanks For The Help




No comments:

Post a Comment