Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: help i cant get my code working

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help i cant get my code working

    I am very new to java and i have only been using it for a few days however after watching a tutorial on youtube i am having problems getting my circle (my player) to move using the arrow keys,when I run the game there aren't any errors but my player wont move.

    This is my code:



    game.java (what I run the game on)
    import java.awt.Graphics;
     
     
    public class game extends gameloop {
     
    	public void init() {
    		setSize(854,480);
    		Thread th = new Thread(this);
    		th.start();
    		addKeyListener(this);
    	}
     
    	public void paint(Graphics g){
    		g.fillRect(x, y, 20, 20);
    	}
    }


    gameloop.java
    import java.applet.Applet;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
     
    public class gameloop extends Applet implements Runnable, KeyListener {
     
    	public int x, y;
    	public boolean up, down, left, right;
     
    	public void run() {
     
    		x = 400;
    		y = 220;
     
    		while(true){
     
    			if (left) {
    				x-=2;
    				left = false;
    			}
    			if (up) {
    				y-=2;
    				up = false;
    			}
    			if (right) {
    				x+=2;
    				right = false;
    			}
    			if (down) {
    				y+=2;
    				down = false;
    			}
    			repaint();
    			try {
    				Thread.sleep(20);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
     
    	}
     
    	public void keyPressed(KeyEvent e) {
    		if (e.getKeyCode() == 37) {
    			left = true; 
    		}  
    		if (e.getKeyCode() == 38) {
    			up = false;
    		}
    		if (e.getKeyCode() == 39) {
    			right = false;
    		}
    		if (e.getKeyCode() == 40) {
    			down = false;
    		}
     
    	}
     
    	public void keyReleased(KeyEvent e) {}
     
     
     
    	public void keyTyped(KeyEvent e) {}
     
    }


    can someone please tell me what i have done wrong, i would really appreciate it.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help i cant get my code working

    What you did wrong (or could do to improve your code and this post):

    1. Post your code in code tags.
    2. Post any errors you're getting when compiling or running your code. If none, say so.
    3. Follow Java naming conventions: Class names are capitalized, methods and variables begin with lower-case and are camel-cased thereafter.
    4. Indent your code correctly (may have been lost due to lack of code tags).
    5. Style: if ( left == true ) is unnecessary. Simply if ( left ) will do.
    6. Finally (for now), empty the keyReleased() method and change your run() method to something like:
        public void run()
        {
            x = 40;
            y = 40;
     
            while(true)
            {
                if (left)   
                {
                    x -= 2;
                    repaint();
                    left = false;
                }
     
                // etc. . . . . .

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help i cant get my code working

    I have updated my code the way you have said and edited it into my first post but it still doesn't work.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: help i cant get my code working

    Quote Originally Posted by alexrox27 View Post
    there aren't any errors but my player wont move
    Use printlns to verify what the code is actually doing.
    print the key code in the key pressed event...
    Inside the while(true) loop, print out the values of up, down, left, and right to see if they are updated in the loop.

    Finding out what the code is doing will put you on the path to making it do what you want it to do

    You should be using KeyEvent.VK_LEFT instead of 37... and the others

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help i cant get my code working

    i have been testing what is and isn't working, my boolean variables have been working and have been moving my player when i manually set them how ever i have found that it is the println code to check it, i will try using the KeyEvent.VK_LEFT code and see if that works.

    --- Update ---

    I have just tested the game using this code

    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
    			left = true; 
    }

    and it still doesn't work, thanks for the suggestion though.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help i cant get my code working

    Post your updated code. I made the changes I suggested to your code, and "it" works fine.

    We can't help with "it isn't working" if we don't know what "it" is and what "working" means. Make an effort and I'll post my code.

  7. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help i cant get my code working

    my updated code is in my first post, can you please send me your version of the code because mine isnt working still

  8. #8
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: help i cant get my code working

    Hey man whats up. I took a second to go through your code and discovered several minor problems you might have overlooked.

    First off when reading input from KeyEvent you should always use

    if (e.getKeyCode() == KeyEvent.VK_#) {
    //whatever you wanna do
    }

    Where the '#' would be the name of the key you are looking for input from.
    Next, if you look here in your keyPressed method, as soon as the input is detected it is setting most your boolean values to false...
    public void keyPressed(KeyEvent e) {
    		if (e.getKeyCode() == 37) {
    			left = true; 
    		}  
    		if (e.getKeyCode() == 38) {
    			up = false;
    		}
    		if (e.getKeyCode() == 39) {
    			right = false;
    		}
    		if (e.getKeyCode() == 40) {
    			down = false;
    		}
     
    	}

    When you have specified to only move the player when these values are true, like below. So you must change them all to true.
    while(true){
     
    			if (left) {
    				x-=2;
    				left = false;
    			}
    			if (up) {
    				y-=2;
    				up = false;
    			}
    			if (right) {
    				x+=2;
    				right = false;
    			}
    			if (down) {
    				y+=2;
    				down = false;
    			}

    Instead of setting up, down, left, and right to false when they are found true, you should set them false when the same key you pressed to move the player, is released. You can do this easily by copying and pasting your keyPressed method, changing "keyPressed" to "keyReleased", and replacing all the true values with false, like so:

    	public void keyReleased(KeyEvent e) {
    		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
    			left = false; 
    		}  
    		if (e.getKeyCode() == KeyEvent.VK_UP) {
    			up = false;
    		}
    		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
    			right = false;
    		}
    		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    			down = false;
    		}
    	}

    This will make it so your player is moving only when the key used to move the player is held down.

    What I have done in my code is the following:
    • Changed the keyPressed method to read input by using "KeyEvent.VK_#"
    • Set my boolean values to false in the keyReleased method
    • Removed setting my boolean values to false, when they are found true in the while loop


    GameLoop.java
    import java.applet.Applet;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
     
     
    public class GameLoop extends Applet implements Runnable, KeyListener {
     
    	public int x, y;
    	public boolean up, down, left, right;
     
    	public void run() {
     
    		x = 50;
    		y = 50;
     
    		while(true) {
    			if(left) {
    				x -= 2;
    			}
    			if(up) {
    				y -= 2;
    			}
    			if(right) {
    				x += 2;
    			}
    			if(down) {
    				y += 2;
    			}
    			repaint();
    			try {
    				Thread.sleep(20);
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
     
    	}
     
    	public void keyPressed(KeyEvent e) {
    		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
    			left = true; 
    		}  
    		if (e.getKeyCode() == KeyEvent.VK_UP) {
    			up = true;
    		}
    		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
    			right = true;
    		}
    		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    			down = true;
    		}
    	}
     
    	public void keyReleased(KeyEvent e) {
    		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
    			left = false; 
    		}  
    		if (e.getKeyCode() == KeyEvent.VK_UP) {
    			up = false;
    		}
    		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
    			right = false;
    		}
    		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
    			down = false;
    		}
    	}
     
    	public void keyTyped(KeyEvent e) {};
     
    }

    Game.java
    import java.awt.Graphics;
     
     
    public class Game extends GameLoop {
     
    	public void init(){
    		setSize(300, 300);
    		Thread th = new Thread(this);
    		th.start();
    		addKeyListener(this);
    	}
     
    	public void paint(Graphics g){
    		g.fillRect(x, y, 20, 20);
    	}
    }


    You should be able to use this information to make the player do most things you want with basic keyboard input Let me know if you have any questions or need further explanation.

Similar Threads

  1. Code not working
    By Petrejonn in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 23rd, 2013, 02:12 PM
  2. Please why is this code not working
    By Petrejonn in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 23rd, 2013, 10:18 AM
  3. How to Translate working code into code with a Tester Class
    By bankoscarpa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 15th, 2012, 02:13 PM
  4. Code working only on ide
    By xeyos in forum Java Networking
    Replies: 0
    Last Post: November 17th, 2011, 05:40 PM
  5. My code is not working
    By mike2452 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 9th, 2011, 06:17 AM