1.11M Members

Move object in game

 
0
 

So, I'm stuck trying to figure out how to move the object. I am using the keyboard as a call to Key Event to move the object. Right now, it isn't moving. I'm pretty sure that this is correct.

import java.awt.Color;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Image;
import java.applet.Applet;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Base extends JPanel{
	private Image bottombase;
	private int x;
	private int y;
	private Character letter;
	
	
	
	public Base(){
		setBackground(Color.black);
		x = getWidth()/2;
		y = getHeight()/2;
		letter = 'a';
		
		bottombase = getImage("SIbase.gif");
		
		
		addKeyListener(new KeyListener(){
			@Override
			public void keyPressed(KeyEvent arg0){
				int code = arg0.getKeyCode();
				switch(code){
				case KeyEvent.VK_RIGHT : x -= 5; break;
				case KeyEvent.VK_LEFT  : x += 5; 
				}

			}
			@Override
			public void keyReleased(KeyEvent arg0){
			}
			@Override
			public void keyTyped(KeyEvent arg0){
				letter = arg0.getKeyChar();
				repaint();
			}
		});
	}
		
	
    @Override
	protected void paintComponent(Graphics g){
		super.paintComponent(g);
		g.drawImage(bottombase, x, y, null);
	}
    
    private Image getImage(String filename){
    	URL url = getClass().getResource(filename);
    	ImageIcon  icon = new ImageIcon(url);
    	return icon.getImage();
    }

}
 
0
 

Is your program actually calling repaint? I see no debugging... anywhere.

 
0
 

I don't think it is. I have the necessary libraries such as JUnit4 and UISpec.

Maybe I should go this route. http://www.cokeandcode.com/info/tut2d.html

What do you suggest?

 
0
 

I suggest that you use System.out.println in a lot of different places to see what's going on with your code... such as right above the call to repaint, to make sure repaint gets called when you are assuming it does.

 
0
 

its so useful for all of us here. i like this very much. thanks for this.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article