Results 1 to 4 of 4
  1. #1
    Jasonpaul2001 is offline Member
    Join Date
    Apr 2015
    Posts
    2
    Rep Power
    0

    Question 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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: KeyPressed KeyReleased Issues

    That's archaic AWT code. I recommend you make the move to Swing, which has been around for about 15 years: Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)

    Also, I'm moving this thread from New to Java to AWT/Swing.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Jasonpaul2001 is offline Member
    Join Date
    Apr 2015
    Posts
    2
    Rep Power
    0

    Default Re: KeyPressed KeyReleased Issues

    Can anyone help me ?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: KeyPressed KeyReleased Issues

    Can you explain what the problem is?

    There are a lot of missing classes preventing the code from being compiled and tested.

    Can you make a smaller, simpler program that compiles and executes for testing that shows the problem?
    Last edited by Norm; 04-04-2015 at 08:43 PM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. KeyPressed doesn't end when key is released
    By Misryma in forum NetBeans
    Replies: 3
    Last Post: 10-26-2013, 03:04 AM
  2. Change Image when Keypressed
    By neilq5 in forum AWT / Swing
    Replies: 0
    Last Post: 10-13-2012, 08:42 PM
  3. KeyPressed trouble with KeyListeners
    By CuddlyKittens11 in forum Advanced Java
    Replies: 8
    Last Post: 05-28-2011, 09:31 PM
  4. Problem with KeyPressed any Help is appreciated
    By gryd00 in forum New To Java
    Replies: 3
    Last Post: 05-17-2011, 05:15 PM
  5. Random keyReleased() in KeyListener
    By briman0094 in forum AWT / Swing
    Replies: 3
    Last Post: 03-21-2011, 10:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •