This site requires JavaScript, please enable it in your browser!
Greenfoot back
Entity1037
Entity1037 wrote ...

2014/3/7

JCreator: bad source file (when it's not)??

Entity1037 Entity1037

2014/3/7

#
I'm getting this error every time I try to create an instance of InputHandler inside of my GameEngine main class. It makes no sense because the files are both in the same directory, and the names are all correct. I'll post the code for the classes below if it helps (it's mostly from a java tutorial I found on compsci.ca though). GameEngine.java
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;


public class GameEngine extends JFrame{
	
	public static void main(String[] args)
    {
    	GameEngine game = new GameEngine();
    	game.run();
    	System.exit(0);
    }
    
    /**
    * This method starts the game and runs it in a loop
    */
    public void run()
    {
    	boolean isRunning=true;
    	initialize();
    	
    	while (isRunning){
    		long time = System.currentTimeMillis();
    		
    		update();
    		draw();
    		//delay for each frame - time it took for each frame
    		time = (1000/70)-(System.currentTimeMillis()-time);
    		
    		if (time>0){
    			try{
    				Thread.sleep(time);
    			}catch(Exception e){}
    		}
    	}
    	setVisible(false);
    }
    
    /**
     * This method will set up everything need for the game to run
     */
     
    static int windowWidth=600;
    static int windowHeight=600;
    static BufferedImage backBuffer;
    static Insets insets;
    static InputHandler user;
    
    void initialize()
    {
    	setTitle("Test Game");
    	setSize(windowWidth,windowHeight);
    	setResizable(false);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    	insets = getInsets();
    	setSize(insets.left+windowWidth+insets.right,insets.top+windowHeight+insets.bottom);
    	setVisible(true);
    	
    	backBuffer = new BufferedImage(windowWidth,windowHeight,BufferedImage.TYPE_INT_RGB);
    	InputHandler = new InputHandler(this);
    }
    
    /**
     * This method will check for input, move things
     * around and check for win conditions, etc
     */
     
    static int x=0;
    static int y=0;
    static int xmove=0;
    static int ymove=0;
    
    void update()
    {
    	xmove=0;
    	ymove=0;
        if (user.isKeyDown(VK_LEFT))xmove=-5;
        if (user.isKeyDown(VK_RIGHT))xmove=5;
        if (user.isKeyDown(VK_UP))ymove=-5;
        if (user.isKeyDown(VK_DOWN))ymove=5;
        //Call collision physics method here
        x+=xmove;
        y+=ymove;
    }
    
    /**
    * This method will draw everything
    */ 
    void draw()
    {
        Graphics g = getGraphics();
        
        Graphics bbg = backBuffer.getGraphics();
        
        bbg.setColor(Color.WHITE);
        bbg.fillRect(0,0,windowWidth,windowHeight);
        
        bbg.setColor(Color.BLACK);
        bbg.drawOval(x,60,20,20);
        
        g.drawImage(backBuffer,insets.left,insets.top,this);
    }
    	
}
InputHandler.java
import java.awt.Component;
import java.awt.event.*;

/**
 * Makes handling input a lot simpler
 */
public class InputHandler implements KeyListener
{
        /**
         * Assigns the newly created InputHandler to a Component
         * @param c Component to get input from
         */
        public InputHandler(Component c)
        {
            c.addKeyListener(this);
        }
        
        /**
         * Checks whether a specific key is down
         * @param keyCode The key to check
         * @return Whether the key is pressed or not
         */
        public boolean isKeyDown(int keyCode)
        {
        	if (keyCode>0&&keyCode<256){
        		return keys(keyCode);
        	}
        	return false;
        }
        
        /**
         * Called when a key is pressed while the component is focused
         * @param e KeyEvent sent by the component
         */
        public void keyPressed(KeyEvent e)
        {
            if (e.getKeyCode()>0&&e.getKeyCode()<256){
            	keys[e.getKeyCode]=true;
            }
        }

        /**
         * Called when a key is released while the component is focused
         * @param e KeyEvent sent by the component
         */
        public void keyReleased(KeyEvent e)
        {
            if (e.getKeyCode()>0&&e.getKeyCode()<256){
            	keys[e.getKeyCode]=false;
            }
        }

        /**
         * Not used
         */
        public void keyTyped(KeyEvent e){}
}
danpost danpost

2014/3/7

#
Line 62 of the GameEngine class seems to be using the name of the class 'InputHandler' where the name of the field 'user' should probably be.
Entity1037 Entity1037

2014/3/8

#
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFACEPALM! OK thank you!
Entity1037 Entity1037

2014/3/8

#
Aw..., now it's telling me that it cannot resolve keys to a variable. The tutorial doesn't seem to define the variable keys in the InputHandler class. Any help?
lordhershey lordhershey

2014/3/11

#
I think you input handler needs a Boolean array of 256 codes. and change this:
public boolean isKeyDown(int keyCode)  
        {  
            if (keyCode>0&&keyCode<256){  
                return keys(keyCode);  
            }  
            return false;  
        }  
to this:
public boolean isKeyDown(int keyCode)  
        {  
            if (keyCode>0&&keyCode<256){  
                return keys[keyCode];  
            }  
            return false;  
        }  
You need to login to post a reply.