My Keyboard imput doesn't seem to work and i don't know why. The program is a little lengthy and I'm sorry about that. I'd really appreciate an answer.
-Main-
/* This code was produced by Kevin Spaeth. * Please do not use without permission. * Copyright May, 2011. Sp333th Productions. */ import java.applet.*; import java.awt.*; public class Main extends Applet implements Runnable { //Keys private boolean keyLeft; private boolean keyRight; private boolean keySpace; //Images private Image Greenman1; private Image Greenman2; private Image Greenman3; private Image Greenman4; private Image Greenman5; //Thread used for the game. private Thread gameThread; //Used for double-buffering. private Image dbImage; private Graphics dbGraphics; //Greenman enters! private Player Greenman; public void init() { //Sets the applet size. setSize(Constants.applet_width, Constants.applet_height); //Sets the name of the applet. setName(Constants.applet_name); //Gets the player images. getImages(); //Initializes the player. Greenman = new Player(300,300,this); //Sets keys to be false initially. keyLeft = false; keyRight = false; keySpace = false; //Sends images to the player class. Greenman.setImages(Greenman1, Greenman2, Greenman3, Greenman4, Greenman5); } public void start() { //Create a new thread. gameThread = new Thread(this); //Start thread. gameThread.start(); } public void run() { //GreenMan is REAL important! Thread.currentThread().setPriority(Thread.MAX_PRIORITY); //Loops the run part of the string. while(true) { //Helps to keep the applet from being resized. setSize(Constants.applet_width, Constants.applet_height); Greenman.playerMove(); //Repaints the graphics. repaint(); try { //Sleep for the game frame rate. Thread.sleep((1000/Constants.applet_framerate)); } catch(InterruptedException gameException) { System.out.println("Error occourred inside of the run loop."); //Don't do anything. } //Ensures that this thread still has the max priority. Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } public void stop() { //Handles stopping the thread. gameThread.stop(); } public void destroy() { //Handles stopping the thread. gameThread.stop(); } public void paint (Graphics gameGraphics) { Greenman.paintPlayer(gameGraphics); gameGraphics.drawImage(Greenman1, 200, 200, this); } public void update (Graphics gameGraphics) { //This code is used to help double-buffer the enviornment. if (dbImage == null) { dbImage = createImage (this.getSize().width, this.getSize().height); dbGraphics = dbImage.getGraphics (); } dbGraphics.setColor (getBackground ()); dbGraphics.fillRect (0, 0, this.getSize().width, this.getSize().height); dbGraphics.setColor (getForeground()); paint(dbGraphics); gameGraphics.drawImage (dbImage, 0, 0, this); } private void getImages() { //Mediatracker picks up images. MediaTracker tracker = new MediaTracker(this); Greenman1 = getImage(getCodeBase(), "Sprites/GreenmanRun-01.png"); tracker.addImage(Greenman1, 1); Greenman1 = getImage(getCodeBase(), "Sprites/GreenmanRun-02.png"); tracker.addImage(Greenman2, 2); Greenman1 = getImage(getCodeBase(), "Sprites/GreenmanRun-03.png"); tracker.addImage(Greenman3, 3); Greenman1 = getImage(getCodeBase(), "Sprites/GreenmanRun-04.png"); tracker.addImage(Greenman4, 4); Greenman1 = getImage(getCodeBase(), "Sprites/GreenmanRun-05.png"); tracker.addImage(Greenman5, 5); //Makes sure that all images are properly loaded. try { tracker.waitForAll(); } catch (Exception exception) {} } //Handles variables when a key is presed. public boolean keyDown (Event gameEvent, int key) { if(key == Event.LEFT) { keyLeft = true; Greenman.playerWalkLeft(true); } else if (key == Event.RIGHT) { keyRight = true; Greenman.playerWalkRight(true); } else if (key == 97) { keySpace = true; Greenman.playerJump(true); } System.out.println ("Charakter: " + (char)key + " Integer Value: " + key); return true; } }
-Player-
/* This code was produced by Kevin Spaeth. * Please do not use without permission. * Copyright May, 2011. Sp333th Productions. */ import javax.swing.ImageIcon; import java.awt.*; public class Player { //Counters private int countPicture; private int countStep; private int countJump; //Determines Directions. private boolean lookLeft; private boolean walkLeft; private boolean walkRight = true; //Determines jumping actions. private boolean jumping; private boolean falling; private boolean ableToJump; //Provides location of all angles of a sprite. private int xPosLeft; private int xPosRight; private int yPosUp; private int yPosDown; private int gamexPos; //Images private Image Greenman1; private Image Greenman2; private Image Greenman3; private Image Greenman4; private Image Greenman5; //What is this used for?! private Component parent; public Player(int x, int y, Component parent) { xPosLeft = x; yPosUp = y; xPosRight = Constants.xSize+xPosLeft; yPosDown = Constants.ySize+yPosUp; gamexPos = x+Constants.xSize/2; countPicture = 0; countStep = 0; countJump = 0; ableToJump = true; lookLeft = false; this.parent = parent; } public void setImages(Image Greenman1, Image Greenman2, Image Greenman3, Image Greenman4, Image Greenman5) { //Fetches images from the Mediatracker in the Main class. this.Greenman1 = Greenman1; this.Greenman2 = Greenman2; this.Greenman3 = Greenman3; this.Greenman4 = Greenman4; this.Greenman5 = Greenman5; } //Determines if the player is moving left. public void playerWalkLeft(boolean value) { walkLeft = value; } //Determines if the player is moving right. public void playerWalkRight(boolean value) { walkRight = value; } //Determines if the player is jumping. public void playerJump(boolean value) { //Reset the jump counter. if(!jumping && ableToJump && value) { countJump = 0; } //Sets the value of jumping. (Can't jump if falling.) if(falling) { jumping = false; } else { jumping = value; } } public void playerMove() { if(walkLeft) { xPosLeft -= Constants.x_speed; xPosRight -= Constants.x_speed; gamexPos -= Constants.x_speed; } } public void paintPlayer(Graphics gameGraphics) { gameGraphics.drawImage(Greenman1, xPosLeft, yPosUp, parent); } }
-Constants-
/* This code was produced by Kevin Spaeth. * Please do not use without permission. * Copyright May, 2011. Sp333th Productions. */ public class Constants { //Name of the Applet. public static final String applet_name = "GreenMan Rev. 1.01"; //Dimensions of the game. public static final int applet_width = 800; public static final int applet_height = 600; //Sets the framerate. public static final int applet_framerate = 30; //Variables for player speed. public final static int x_speed = 3; public final static int y_speed1 = 1; public final static int y_speed2 = 2; public final static int y_speed3 = 4; public final static int y_speed4 = 8; //Keys to be pressed. public boolean keyDown; public boolean keyUp; public boolean keyLeft; public boolean keyRight; public boolean keySpace; //Size of Greenman (while running.) public final static int xSize = 75; public final static int ySize = 200; }