Results 1 to 20 of 20
  1. #1
    askumi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default Rpg game player wont move help please

    I'm trying to make a RPG game using java and I cannot get my character to move. I have the keyboard commands in a seperate class file and I cannot figure out how to get them to interact with other classes when I hit a key heres my code. also, what does private static final long serialVersionUID do eclipse just told me to put it there automatically

    all 4 of my files for my game are in here...

    Java Code:
    Main.java
    
    import java.awt.Dimension;
    import javax.swing.JFrame;
    
    public class Main {
    	static int width;
    	static int height;
    	public static void main(String[] args){
    		JFrame f = new JFrame("RPG game");
    		width=640;
    		height=640;
    		f.getContentPane().setPreferredSize(new Dimension(width,height));
    		f.setResizable(false);
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Gui gui = new Gui();
    		f.add(gui);
    		f.pack();
    		f.setVisible(true);
    	}
    	public static int getWidth() {
    		return width;
    	}
    	public static int getHeight() {
    		return height;
    	}
    }
    
    Player.java
    import javax.swing.JPanel;
    
    public class Player extends JPanel{
    	int Xloc,Yloc;
    	private static final long serialVersionUID = 4113419485118457382L;
    	public Player (int x,int y){
    		x*=32;
    		y*=32;
    		Xloc=x;
    		Yloc=y;
    	}
    	public int getX(){
    		return Xloc;
    	}
    	public int getY(){
    		return Yloc;
    	}
    	public void Move(String direction, int amount){
    		if (direction == "down"){
    			Yloc+=amount*32;
    		}
    	}
    }
    
    Gui.java
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    
    public class Gui extends JPanel{
    	private static final long serialVersionUID = -1049958103353244632L;
    	int row,column;
    	BufferedImage TilesetGrass;
    	Player user = new Player (0,0);
    	int[][] map = {
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//0 grass
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
    			{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
    	public Gui(){
    		try {
    			TilesetGrass = ImageIO.read(new File("tiles/grass.png"));
    		}
    		catch (IOException e) {
    		}
    	}
    	public void updateGui(){
    		repaint();
    	}
    	public void paintComponent(Graphics g){
    		for (row=0; row<map.length; row++){
    			for (column=0; column < map[row].length; column++){
    				g.drawImage(TilesetGrass, column*32, row*32, column*32+32, row*32+32, map[row][column]*32, 0, map[row][column]*32+32, 32, null);
    			}
    		}
    		g.setColor (Color.blue);
    		g.fillOval(user.getX(),user.getY(),32,32);
    	}
    	public static void delay(int ms){
    		try{
    				Thread.sleep(ms);
    		}
    		catch(InterruptedException e){
    		}
    	}
    }
    
    Keyboard.java
    import java.awt.Event;
    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;
    
    public class Keyboard extends Gui implements KeyListener{
    	private static final long serialVersionUID = -6570742606515125465L;
    	public void keyTyped(KeyEvent e){}
    	public void keyPressed(KeyEvent e){
    		char c = e.getKeyChar();
    		if (c==Event.DOWN){
    			user.Move("down",1);
    		}
    	}
    	public void keyReleased(KeyEvent e){}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    There are several things that bother me about this code, including but not exclusive:

    • KeyListener that is never added to anything
    • Probably shouldn't be even using a KeyListener to begin with but rather key binding (the tutorials can explain how to do this)
    • Comparing Strings with == rather than the equals method
    • Ignored exceptions with empty catch blocks
    • No animation code. I worry about the Thread.sleep in there that you may put the main Swing thread, the EDT, to sleep. Better to use a Swing Timer in this situation. Again, the tutorials will show you how.
    Last edited by Fubarable; 12-21-2010 at 06:20 AM.

  3. #3
    askumi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    I tried reading up on key bindings and i know taht they use Input maps and Action maps but when i dont fullly understand how to use them This is what I try

    Java Code:
    Player.getInputMap().put(KeyStroke.getKeyStroke("Event.DOWN"),"movedown");
    Player.getActionMap().put("movedown",downAction);
    
    .........
    
    Action downAction = new DownAction();
    class DownAction extends AbstractAction
    {
    	public DownAction(){
    		Yloc+=amount*32;
    	}
    }
    Code is in my player.java file, I get this error when i mouse over getInputMap()
    Syntax error on token "getInputMap", Identifier expected after this token

    what am i doing wrong..

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    First test out key binding in a very small program that does nothing more than show a JPanel that has key binding to say the down arrow, and that will System.out.println("down") when the down arrow is pressed. Once you get that to work, you can use the same concepts in your program above. If it fails to work, then post your small program with full error messages and we can help you debug it.

    Also, I've not seen key strokes obtained this way: KeyStroke.getKeyStroke("Event.DOWN"), and I'm not sure, but this doesn't look right to me.
    Last edited by Fubarable; 12-21-2010 at 07:11 PM.

  5. #5
    askumi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    Ok i made a program but i dont know if i did it right anyway it doesnt work

    Java Code:
    main.java
    
    import java.awt.Dimension;
    import javax.swing.JFrame;
    
    public class Main {
    	public static void main(String[] args){
    		JFrame f = new JFrame("Test");
    		f.getContentPane().setPreferredSize(new Dimension(256,256));
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.pack();
    		f.setVisible(true);
    	}
    }
    
    Test.java
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    
    public class Test {
    	Test.getInputMap().put(KeyStroke.getKeyStroke("Event.DOWN"),"movedown");
    	Test.getActionMap().put("movedown",downAction);
    
    	Action downAction = new DownAction();
    	class DownAction extends AbstractAction
    	{
    		public DownAction(){
    			System.out.println("down");
    		}
    		public void actionPerformed(ActionEvent e) {
    		}
    	}
    }
    }
    eclipse underlines the getinputmap() and says
    Syntax error on token "getInputMap", Identifier expected after this token

    edit: event.down worked for me before i found it on the internet somewhere but ill try using sumthing else to be sure..

  6. #6
    SmilingKey is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    It shows that error cuz Test class doesn't have these methods defined.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Yep, you can only call that method on a JComponent such as a JPanel. As I suggested, add the binding to a JPanel that you display, not to some random class "Test" that you don't display (nor could it ever be displayed). The tutorials have functioning code examples on key binding. Also this forum has plenty of examples if you search on the term. Luck.

  8. #8
    askumi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    ive looked aroud site and experimented and i still cannot figure out keybindings.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't see where you've made the corrections in your test program above based on our comments, such as having Test extend JPanel and adding a Test object to your JFrame's contentPane. Why not try that first, and let's work from there.

  10. #10
    askumi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    Okai i made Test extend JPanel and put it in the Jframe but its still not working correct

    Java Code:
    main.java
    import java.awt.Dimension;
    import javax.swing.JFrame;
    
    public class Main {
    	public static void main(String[] args){
    		JFrame f = new JFrame("Test");
    		f.getContentPane().setPreferredSize(new Dimension(256,256));
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Test one = new Test();
    		f.add(one);
    		f.pack();
    		f.setVisible(true);
    	}
    }
    
    test.java
    
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JPanel;
    
    public class Test extends JPanel{
    	Test[COLOR="Red"].getInputMap[/COLOR]()[COLOR="Red"].[/COLOR]put(KeyStroke.getKeyStroke("Event.DOWN"),"movedown");
            [COLOR="Red"]//Whats highlighted in red gives me "Syntax error on token "getInputMap", Identifier expected after this token"[/COLOR]
    	Test.getActionMap().put("movedown",downAction);
    
    	Action downAction = new DownAction();
    	class DownAction extends AbstractAction
    	{
    		public DownAction(){
    			System.out.println("down");
    		}
    		public void actionPerformed(ActionEvent e) {
    		}
    	}
    }
    }
    Last edited by askumi; 12-22-2010 at 12:27 AM.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your errors have nothing to do with key binding and all to do with basic Java.

    • In your Test class the errors are first of all occurring because you're trying to make method calls outside of a method, constructor, or static block. Don't do this, but instead make these calls inside of your constructor.
    • You're trying to make these method calls on the Test class as if they were static methods. As you'll see in the examples, they're not and need to be called on JComponent instances.


    If you don't understand my comments above, then ask for clarification.

  12. #12
    askumi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    can i have clerification please

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by askumi View Post
    can i have clerification please
    Please tell us what confuses you in particular. Do you understand that you can't call method and such outside of a method? In other words this won't work:

    Java Code:
    class Foo {
       System.out.println("foo");
    }

    But this does:

    Java Code:
    class Foo {
       public Foo() {
         System.out.println("foo");
       }
    }


    and so does this:


    Java Code:
    class Foo {
       public void myMethod() {
         System.out.println("foo");
       }
    }

    Honestly, if you don't understand these basics yet, you should probably step back from trying to code Swing and do the introductory tutorials first. Trying to learn basic Java and Swing at the same time is too steep of a learning curve for mere mortals, and believe me, I speak from experience having failed to scale that cliff myself in the early going and having had to take a step back.

  14. #14
    askumi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    I understand the problem I just dont understand how to fix it or what you mean by it need sto be called on JComponent instances.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by askumi View Post
    I understand the problem I just dont understand how to fix it or what you mean by it need sto be called on JComponent instances.
    If you understand about not calling methods outside of methods or constructors why did you do it? Seriously, you need to put in effort if you are going to ask us to do the same.

    Anyway, the instance is Test itself, or "this". So in Test's constructor call getActionMap and getInputMap. You don't need to call them "on" any object since they will be called implicitly on the this. Keep trying.

  16. #16
    askumi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    main.java
    import java.awt.Dimension;
    import javax.swing.JFrame;
    
    public class Main {
    	public static void main(String[] args){
    		JFrame f = new JFrame("Test");
    		f.getContentPane().setPreferredSize(new Dimension(256,256));
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		Test one = new Test();
    		f.add(one);
    		f.pack();
    		f.setVisible(true);
    	}
    }
    
    test.java
    
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    
    public class Test extends JPanel{
    	public Test(){
    		Action downAction = new DownAction();
    		getInputMap().put(KeyStroke.getKeyStroke("Event.DOWN"),"movedown");
    		getActionMap().put("movedown",downAction);
    	}
    	class DownAction extends AbstractAction
    	{
    		public DownAction(){
    			System.out.println("down");
    		}
    		public void actionPerformed(ActionEvent e) {
    		}
    	}
    }
    I know im getting close, its running now. only thing is its saying "down" before i hit the down arrow key i tried changing they key to like F2 or something but it still doing that thanks for all the help you gave so far thoguh :>
    Last edited by askumi; 12-22-2010 at 02:50 AM.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    OK, you're much closer now. Suggestions:

    1) Put your code that you want to occur on key press in the actionPerformed method, not in the constructor.
    2) You're not getting the keystroke correctly. Check out the API. One way I get the KeyStroke is shown below.
    3) There are 3 different InputMaps available for each component, and you may have to experiment with the condition int parameter to get the right one. Check out the tutorial and API for the differences.

    e.g.,

    Java Code:
    class KeyBindingTest extends JPanel {
       public KeyBindingTest(){
          Action downAction = new DownAction();
          //!!getInputMap().put(KeyStroke.getKeyStroke("Event.DOWN"),"movedown");
          int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
          getInputMap(condition).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "movedown");
          getActionMap().put("movedown",downAction);
      }
      class DownAction extends AbstractAction
      {
          public DownAction(){
              //!! System.out.println("down");
          }
          public void actionPerformed(ActionEvent e) {
             System.out.println("down");
          }
      }
    }

  18. #18
    askumi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

    Default

    If you put
    Java Code:
    getInputMap(2).put...
    would it be the same as
    Java Code:
    int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
    getInputMap(condition).put...
    also what does this mean eclipse whats me to put it in after my classes

    Java Code:
    private static final long serialVersionUID
    Thanks for all the help though :>

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by askumi View Post
    If you put
    Java Code:
    getInputMap(2).put...
    would it be the same as
    Java Code:
    int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
    getInputMap(condition).put...
    I don't know if JComponent.WHEN_IN_FOCUSED_WINDOW equals 2, and I don't care since coding this way would be using a "magic number", which is something to avoid as it can allow pernicious errors to easily and quietly be introduced into your code and makes for debugging nightmares. Better to use constants that have names that make sense, which fortunately Swing provides us with.


    also what does this mean eclipse whats me to put it in after my classes

    Java Code:
    private static final long serialVersionUID
    Thanks for all the help though :>
    Eclipse doesn't want you to do this, the Java compiler is recommending this though because your class extends a Serializable class, and all well behaved Serializable classes should declare this constant. This is not an error message though but a warning, and feel free to either ignore it or add the annotation:

    @SuppressWarnings("serial")

    above your class declaration.

    You're welcome.

  20. #20
    askumi is offline Member
    Join Date
    Dec 2010
    Posts
    10
    Rep Power
    0

Similar Threads

  1. How to add online two player to a Java game?
    By michealsmith in forum Java Gaming
    Replies: 1
    Last Post: 11-30-2010, 08:43 PM
  2. how to log into a game with my player.java class
    By basketball8533 in forum New To Java
    Replies: 11
    Last Post: 10-13-2010, 01:40 AM
  3. sound player
    By jperson in forum New To Java
    Replies: 1
    Last Post: 05-02-2010, 06:09 AM
  4. MP3 Player idea
    By vinoth in forum New To Java
    Replies: 3
    Last Post: 08-18-2009, 02:10 AM
  5. mp3 player applet
    By willemjav in forum Java Applets
    Replies: 0
    Last Post: 05-20-2008, 02:16 AM

Tags for this Thread

Posting Permissions

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