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 5 of 5

Thread: Platform game player doesn't respond to keyboard

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Platform game player doesn't respond to keyboard

    I started learning java, and tried to make a game, i almost finished it but then i realized that it doesn't work, so i copied the project and started dumping code from my game to find what did i do wrong, but i just cant figure it out. Here is the main class:
    import javax.swing.JFrame;
     
    public class Main extends JFrame {
     
    	//	KONSTRUKTOR
    	public Main() {
    		super("Plavko glupavko");
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		setSize(800, 600);
    		setResizable(false);
    		setVisible(true);
     
    		add(new Igra());
    	}
     
    	//	MAIN METOD
    	public static void main(String[] args) {
    		new Main();
    	}
    }

    here is igra class:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
     
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
     
    public class Igra extends JPanel implements ActionListener {
     
    	private Timer tajmer;
    	private Tabla tabla;
    	private Igrac igrac;
     
    	private int igracV;
    	private int igracDx;
     
    	//	KONSTRUKTOR
    	public Igra() {
    		setSize(800, 600);
    		setBackground(Color.WHITE);
    		addKeyListener(new Slusalac());
    		setFocusable(true);
     
    		tabla = new Tabla(1);
    		igrac = tabla.getIgrac();
     
    		igracV = 2;
    		igracDx = 0;
     
    		tajmer = new Timer(50, this);
    		tajmer.start();
    	}
     
    	//	ISCRTAVANJE
    	public void paint(Graphics g) {
    		super.paint(g);
     
    		//	Igrac
    		g.drawImage(igrac.getSlika(), igrac.getX(), igrac.getY(), null);
     
    		Toolkit.getDefaultToolkit().sync();
    		g.dispose();
    	}
     
    	//	GLAVNA PETLJA
    	public void actionPerformed(ActionEvent e) {
    		pomeriIgracaX();
     
    		repaint();
    	}
     
    	//	Kretanje igraca
    	public void pomeriIgracaX() {
    		int duhx = igrac.getXBezPomeraja() + igracDx;
    		igrac.setX(duhx);
    	}
     
     
    	//	KLASA ZA TASTATURU
    	private class Slusalac extends KeyAdapter {
    		public void keyPressed(KeyEvent e) {
    			int kod = e.getKeyCode();
    			//	pomeraj desno
    			if (kod == KeyEvent.VK_RIGHT) {
    				igracDx = igracV;
    			}
    			//	pomeraj levo
    			if (kod == KeyEvent.VK_LEFT) {
    				igracDx = -igracV;
    			}
    		}
    		public void keyReleased(KeyEvent e) {
    			int kod = e.getKeyCode();
    			if (kod == KeyEvent.VK_RIGHT) {
    				if (igracDx > 0)
    					igracDx = 0;
    			}
    			if (kod == KeyEvent.VK_LEFT) {
    				if (igracDx < 0)
    					igracDx = 0;
    			}
    		}
    	}
    }

    here is tabla class:
    import java.awt.Image;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
     
     
    public class Tabla {
    	private Image pozadina;
    	//	ove liste ce da sadrze objekte
    	private Igrac igrac;
     
    	public Tabla(int tabla) {
    		Scanner s;
     
    		String imefajla = "table/tabla"+tabla+".tbl";
    		File fajl = new File(imefajla);
     
     
    		try {
    			s = new Scanner(fajl);
    			int brreda = 0;
    			while (s.hasNextLine()) {
    				String red = s.nextLine();
    				for (int i = 0; i < red.length(); i++) {
    					if (red.charAt(i) == 'i') {
    						igrac = new Igrac(i, brreda);
    					}
    				}
    				brreda++;
    			}
    			s.close();
    		} catch (FileNotFoundException e) {}
    	}
     
    	//	GETERI
     
    	public Igrac getIgrac() {
    		return igrac;
    	}
    }

    and here is igrac class:
    import java.awt.Image;
    import java.awt.Rectangle;
     
    import javax.swing.ImageIcon;
     
     
    public class Igrac {
    	private Image slika;
    	private final int SIRINA = 30;
    	private final int VISINA = 30;
    	private int x;
    	private int y;
     
    	//	pomeraj za kameru
    	public static int kpx = 0;
    	public static int kpy = 0;
     
    	//	KONSTRUKTOR
    	public Igrac(int x, int y) {
    		this.x = x * 30;
    		this.y = y * 30;
    		String fajl = "slike/igrac.png";
    		slika = new ImageIcon(fajl).getImage();
    	}
     
    	//	GETERI
    	public Image getSlika() {
    		return slika;
    	}
     
    	public Rectangle getGranice() {
    		return new Rectangle(x, y, SIRINA, VISINA);
    	}
     
    	public int getXBezPomeraja() {
    		return x;
    	}
     
    	public int getYBezPomeraja() {
    		return y;
    	}
     
    	public int getX() {
    		return x + kpx;
    	}
     
    	public int getY() {
    		return y + kpy;
    	}
     
    	//	SETERI
    	public void setX(int x) {
    		this.x = x;
    	}
     
    	public void setY(int y) {
    		this.y = y;
    	}
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Platform game player doesn't respond to keyboard

    Did you ever have a Key Listener?

    Also, I'm not very familiar with those and could only possibly do things that weren't combinations, like Shift+F or something.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Platform game player doesn't respond to keyboard

    Yes, as you can see in Igra class i added key listener and made a private class (Slusalac) to serve as one, when i create Igrac object directly in my Igra class it all works, but when i create Igrac object within the Tabla class and then pass it to Igra class it doesn't work. I tried debugging but i get no errors.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default Re: Platform game player doesn't respond to keyboard

    jesamjasem,

    You might want to consider which window has focus for the key listener, I'd try making the JPanel request focus

    whilst you have set it to focusable you may need to request it

    also do actionPerformed ever get called, which is your position update function

    Chris
    Last edited by Freaky Chris; December 12th, 2010 at 08:16 AM.
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  5. The Following User Says Thank You to Freaky Chris For This Useful Post:

    jesamjasam (December 12th, 2010)

  6. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Platform game player doesn't respond to keyboard

    Yes that's it, requestFocus() did it. Thank you i would never figure this out by myself.

Similar Threads

  1. Java Mp3 player.
    By alex901 in forum Object Oriented Programming
    Replies: 4
    Last Post: November 18th, 2010, 08:12 AM
  2. Music player problem
    By abhinavhardikar in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 9th, 2010, 07:41 PM
  3. cross platform architecture in Java/J2ee
    By softwarebuzz in forum Web Frameworks
    Replies: 1
    Last Post: January 9th, 2010, 01:43 PM
  4. [ASK] JMF Class Player
    By bocahTuaNakalzz in forum Java SE APIs
    Replies: 2
    Last Post: December 8th, 2009, 02:40 AM
  5. MP3 Player: ID3 Tag Image Retrieval Problem for the Applet from internet
    By JavaJames in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 20th, 2009, 07:11 AM

Tags for this Thread