Kilobolt
  • Home
  • Tutorials
    • Game Development Tutorial >
      • Unit 1: Beginning Java >
        • Before you begin...
        • Day 1: Setting Up
        • Day 2: Java Basics
        • Day 3: More Basics
        • Day 4: Java Math
        • Day 5: More Math
        • Day 6: If... else...
        • Day 7: More Control Flow
        • Day 8: Looping
        • Day 9: More on Looping
        • Day 10: Inheritance, Interface
        • Day 11: Threads and Graphics
      • Unit 2: Creating a Game I >
        • Day 1: Foundations
        • Day 2: Basic Framework
        • Day 3: Taking User Input
        • Day 4: Enter the Robot
        • Day 5: Background and Sprites
        • Day 6: Adding Enemies
        • Day 7: Shooting Bullets
        • Day 8: Animations
        • Day 9: 2D-Arrays
        • Day 10: Painting the Tilemap
      • Unit 3: Creating a Game II >
        • Day 1: Level Creation - Part 1
        • Day 2: Level Creation - Part 2
        • Day 3: Level Creation - Part 3
        • Collision Detection Basics
        • Day 4: Collision Detection Part 1
        • Day 5: Collision Detection Part 2
        • Day 6: Collision Detection Part 3
        • Day 7: Health System & Death
        • Day 8: Basic AI & Final Touches
      • Unit 4: Android Game Development >
        • Day 1: Introduction to Android
        • Day 2: Setting up for Development
        • Day 3: Creating our First Android Application
        • Day 4: Parts of an Android Application
        • Day 5: The Android Game Framework: Part I
        • Day 6: The Android Game Framework: Part II
        • Create an Android Game From Scratch (or port your existing game)
        • Day 7: Creating an Android Game (From Start to Finish)
      • Reference Sheet
    • Zombie Bird Tutorial (Flappy Bird Remake) >
      • Unit 1: Building the Game >
        • Introduction
        • Day 1: Flappy Bird - An In-depth Analysis
        • Day 2: Setting up libGDX
        • Day 3: Understanding the libGDX Framework
        • Day 4: GameWorld and GameRenderer and the Orthographic Camera
        • Day 5: The Flight of the Dead - Adding the Bird
        • Day 6: Adding Graphics - Welcome to the Necropolis
        • Day 7: The Grass, the Bird and the Skull Pipe
        • Day 8: Collision Detection and Sound Effects
        • Day 9: Finishing Gameplay and Basic UI
        • Day 10: GameStates and High Score
        • Day 11: Supporting iOS/Android + SplashScreen, Menus and Tweening
        • Day 12: Completed UI & Source Code
    • Android Application Development Tutorial >
      • Unit 1: Writing Basic Android Apps >
        • Before you begin...
        • Day 1: Android 101
        • Day 2: Getting to Know the Android Project
        • Day 3: The Development Machine
        • Day 4: Building a Music App - Part 1: Building Blocks
        • Day 5: Building a Music App - Part 2: Intents
        • Day 6: Building a Music App - Part 3: Activity Lifecycles
  • New Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Facebook
  • Twitter

GAME DEVELOPMENT TUTORIAL: DAY 2-3: Taking User Input

10/9/2012

58 Comments

 
Picture
Lesson #2-6: Implementing KeyListener


Welcome to Day 3. Today we will be adding code that will allow our game to respond to the user's actions on the keyboard.

Here's how we will approach it:
1. In order for our game to "listen" for key presses and releases, we have to give it a listener.
2. To accomplish #1, we simply need to implement the "KeyListener" interface, and then add the KeyListener in our init() method.
3. Implementing an interface requires that you take every method defined in the KeyListener superclass and add it to your code. These three methods are: keyPressed(), keyReleased(), and keyTyped().


So let's get started by implementing the KeyListener.

1. Go to your StartingClass.java and examine your class declaration:


public class StartingClass extends Applet implements Runnable{

You will notice that you are already implementing the Runnable interface, so you simply add: 
", KeyListener" to the end like so:

public class StartingClass extends Applet implements Runnable, KeyListener{


2. When you add this, Java will give you an error saying "KeyListener cannot be resolved to a type". To resolve this error, just import KeyListener.
Picture
You need to import KeyListener before you can implement the interface.
3. You will now see an error: "The type StartingClass must implement the inherited abstract method KeyListener.keyReleased(KeyEvent)."

Recall that when you implement an interface, you must take all of its methods and declare them in your class. So you can easily resolve this by choosing: Add unimplemented methods.
Picture
To implement an interface, you must make use of all of its methods.
4. Adding unimplemented methods will do two things: First, it will add the three methods: keyPressed(), keyReleased(), and keyTyped() to your code. These three methods require a KeyEvent object as a parameter. Java is smart enough to import KeyEvent for you. 

5. Finally, add this implemented KeyListener to the current Applet by adding to the init() method:
addKeyListener(this);



The resulting code is as follows:

Figure 2-10: StartingClass.java

package kiloboltgame;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class StartingClass extends Applet implements Runnable, KeyListener{

   @Override
   public void init() {

      setSize(800, 480);
      setBackground(Color.BLACK);
      setFocusable(true);      
      addKeyListener(this);
      Frame frame = (Frame) this.getParent().getParent();
      frame.setTitle("Q-Bot Alpha");
}

   @Override
   public void start() {
   Thread thread = new Thread(this);
   thread.start();
   }

   @Override
   public void stop() {
   // TODO Auto-generated method stub
   }

   @Override
   public void destroy() {
   // TODO Auto-generated method stub
   }

   @Override
   public void run() {
      while (true) {

         repaint();
         try {
            Thread.sleep(17);
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    
}

@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    
}

@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
    
}

}
The first two methods are self-explanatory. keyTyped is slightly more complicated, but you can learn more about it here: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/KeyEvent.html

Lesson #2-7: Listening for Key Presses
In our 2D-platformer, we will need at least five buttons to start. The four directional arrows will each be a button, and for now we will use the Space Bar for jump.

We are now working in the keyPressed() method. Inside, we will create a switch that will carry out the appropriate action depending on which button is pressed:

@Override
public void keyPressed(KeyEvent e) {

   switch (e.getKeyCode()) {
   case KeyEvent.VK_UP:
   break;

   case KeyEvent.VK_DOWN:
   break;

   case KeyEvent.VK_LEFT:
   break;

   case KeyEvent.VK_RIGHT:
   break;

   case KeyEvent.VK_SPACE:
   break;
   }
}
Let's examine this in detail. We learned about switches in Unit 1. It is an alternative to a long list of if statements. A switch will compare the key, which is the variable we are checking for, with values, and then carry out the appropriate response.

In this case, the key is e.getKeyCode(). e.getKeyCode() will return the code of the button that you press on the keyboard. (If you were to type System.out.println(e.getKeyCode()); , each time that you press a button, it will display the key code on the console).

What the switch is doing here, then, is comparing the e.getKeyCode() returned from your button presses, and comparing it to multiple cases of values. 
Lesson #2-8: Listening for Key Releases
We will apply the exact same concept to Key Releases.
In this situation, e.getKeyCode() will return the key code upon release of a button (since it is in the keyReleased() method). Then it will carry out an appropriate response depending on which button is released.
@Override

public void keyReleased(KeyEvent e) {
   switch (e.getKeyCode()) {
   case KeyEvent.VK_UP:
      break;

   case KeyEvent.VK_DOWN:
      break;

   case KeyEvent.VK_LEFT:
      break;

   case KeyEvent.VK_RIGHT:
      break;
   case KeyEvent.VK_SPACE:
      break;

   }
}
At the present, we have no character to actually move or stop. Therefore, we will just output some text to the console like so:

Figure 2-12: StartingClass.java, End of Day 3

package kiloboltgame;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class StartingClass extends Applet implements Runnable, KeyListener {

    @Override
    public void init() {

        setSize(800, 480);
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(this);
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("Q-Bot Alpha");
    }

    @Override
    public void start() {
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
    }

    @Override
    public void run() {
        while (true) {

            repaint();
            try {
                Thread.sleep(17);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {

        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            System.out.println("Move up");
            break;

        case KeyEvent.VK_DOWN:
            System.out.println("Move down");
            break;

        case KeyEvent.VK_LEFT:
            System.out.println("Move left");
            break;

        case KeyEvent.VK_RIGHT:
            System.out.println("Move right");
            break;

        case KeyEvent.VK_SPACE:
            System.out.println("Jump");
            break;

        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_UP:
            System.out.println("Stop moving up");
            break;

        case KeyEvent.VK_DOWN:
            System.out.println("Stop moving down");
            break;

        case KeyEvent.VK_LEFT:
            System.out.println("Stop moving left");
            break;

        case KeyEvent.VK_RIGHT:
            System.out.println("Stop moving right");
            break;

        case KeyEvent.VK_SPACE:
            System.out.println("Stop jumping");
            break;

        }

    }

    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

}

That's today's lesson! On Friday, we will be adding the background, our main character, and then add some movement!

Thanks for following my tutorials, and let me know if I can help you in any way!
Picture
KiloboltGame - Day 3
File Size: 3 kb
File Type: zip
Download File

Instructions on importing projects can be found here.
Picture
Go to Day 2: Basic Framework
Go to Day 4: Enter the Robot
58 Comments
nightmare6m
10/10/2012 08:25:46 am

Thank you so much for these tutorials. I am an Actionscript developer trying to learn Java and Android. This has been beneficial already and promises to continue to help.

Reply
Ben
10/12/2012 11:47:00 am

Please continue updating these tutorials! They are the best I've found so far. They're so easy follow! I've never understood JAVA at all until I found these tutorials.

Reply
RAI
11/4/2012 04:56:47 pm

what does the @override mean? Is it necessary....??

Reply
Reece
11/18/2012 04:24:16 pm

I'm just learning myself but I think this could help:
http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why


+ Awesome Tutorials! thanks much!

Reply
Matt
11/11/2012 01:59:52 am

Great tutorials. Just thought I should let you know the source code links you to the top of the page.

Reply
hanjin
11/19/2012 08:35:39 pm

these are really nice. very satisfied.
just one thing I need to tell ya is the link to source file is wrong
I will appreciate you don't mind fix it
anyway ty ty xD

Reply
Andrew
11/22/2012 08:29:39 pm

Thanks for the tutorials!
These are a big help.
One thing I noticed is that when I run the applet and push the keys on the keyboard, nothing is actually printed to the console as per the system.out.println() calls would suggest. Do we have to trigger the listening for input with a call to addKeyListener() somewhere?

Reply
Steve
12/19/2012 09:59:51 am

Andrew, the system.out.println() prints it out in Eclipse. It works just the same as it has in previous tutorials.

Reply
Sean
2/27/2013 03:57:43 am

This is not true, the app does nothing at this point in the tutorial, not even print anything in the console upon keys being pressed.

Sean
2/27/2013 04:02:36 am

Cancel that- I had missed the addKeyListener in the init method

John Campbell
2/27/2013 05:58:43 am

Loving the tutorial so far, but sadly I have a problem that means I don't see anything show up on the console (When I press the buttons, it doesn't display on the console)
I think it may be due to my line "switch (e.getKeyCode()){", for some reason the e is underlined red

Reply
Chris Ouellette
2/28/2013 05:35:01 am

Hey John,

Is it possible the parameter passed in to your function isn't e? When I had it auto-make the functions, it defaulted to arg0.

Your code should read

public void keyPressed(KeyEvent e)

NOT

public void keyPressed(KeyEvent arg0)

Reply
Jan G
2/28/2013 06:23:44 pm

Hi John,

make sure you added this in the init method:
addKeyListener(this);

I followed it religiously and same thing happened. to me. :)

Bill Siegel
4/2/2013 10:10:26 am

Thanks Chris, I had the Same problem and didn't notice it until I read this reply, Changed the arg0 to e and it fixed the unresolved errors... thanks again.

zweyannaing
3/6/2013 10:43:37 am

what i do with downloaded zip files?

Reply
albert lopez
4/4/2013 08:19:18 am

Open it with win zip or another zip reading program.

Reply
Peter
3/17/2013 03:22:48 am

. To accomplish #1, we simply need to implement the "KeyListener" interface, and then add the KeyListener in our init() method.
3. Implementing an interface requires that you take every method defined in the KeyListener superclass and add it to your code. These three methods are: keyPressed(), keyReleased(), and keyTyped().


How did you know that the interface KeyListener had those three methods inside it? in the eclipse help it doesnt say stuff like wich methods are inside a interface and so on.

thank you

Reply
João
4/3/2013 06:33:00 am

Hey man, congrats,

Perfect tutorials, you explain java in a very understanding way!

Thank you so much.
Best luck,

Reply
Thomas Bull
4/10/2013 08:28:59 am

First of all thanks for such a great tutorial!

Im actully as far as unit 3 day seven, and don't have any problems as such. This section is most relevant to my query which is why im commenting here, hopefully the solution might help others with the same problem.

what i would like to know is how i get my robot to move when using eclipse for mac? it all moves and works fine in windows, but not in Mountain Lion

Im guessing some sort of addition/modification to the KeyPressed, KeyReleased or KeyType Method, but as im completely new to java i don't know what. Please help

Tom

Reply
Thomas Bull
4/10/2013 08:53:41 am

I have just solved this! Once the applet is running, i click the applet menu at the top, then properties, then apply and it works :) happy days!

Reply
Haris Praba Aditya link
5/20/2013 03:11:25 pm

Hi, thanks for the great tutorials.

I have a question in these code,

setSize(800, 480);
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("Real Game Alpha");

is it okay to add the addKeyListener(this); at the end of those code? or it will result something different.

Thank you. :)

Reply
M Nur Fadillah
9/7/2013 07:24:38 pm

yeah its okay

Reply
sankar narayanan
6/2/2013 04:00:08 pm

Gr8 tutorials man! keep it up !

Reply
Parker
6/23/2013 10:22:04 am

Hey, I have completed this up through day 6. But I am having a problem running the code. The section that says
public void init(){
setSize(800, 480);
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("Space Shooter");
}

returns an error for the Frame frame = (Frame) this.getParent().getParent() line.
The error is as follows:
"Exception in thread "main" java.lang.NullPointerException
at spaceshooter.SpaceShooter.init(SpaceShooter.java:42)
at spaceshooter.SpaceShooter.main(SpaceShooter.java:156)
Java Result: 1"

I am using the latest version of NetBeans.

any help is appreciated!

Reply
Narayan
7/9/2013 07:36:17 pm

download link doesn't work

Reply
cakGuess
11/15/2013 09:30:01 am

There is no zip file in the download link of KiloboltGame-Day 3. Please James fix it!
Many thanks for your GREAT TUTORIAL!!

Reply
Nalini
8/7/2013 07:00:37 pm

Hey
Great tutorials!! Helped me lot..you explained everything in simple way..Thank you so much

Reply
vahid
10/25/2013 12:05:42 am

Hello ! I'm reading your tutorial during my travel to another city in the bus ! I opened my laptop and suddenly I found that there is no internet connection !! I got upset because I was not able to open your tutorial with no internet but I found the solution :) I turned on my mobile data and made a hotspot in my phone and shared my gprs connection and connected my laptop to it ! :D now I'm going through the tutorial step by step and compiling the result ! ;) thank you for your great tutorial. I'm eager to continue.

Reply
Claudiu
11/25/2013 11:25:14 pm

hello guys. Thx you very much for your efforts to bring this awsome tutorial!!

It's our duty to post you comment for not working link like here:

The first two methods are self-explanatory. keyTyped is slightly more complicated, but you can learn more about it here: http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/KeyEvent.html

It's good if you guys post another useful link for us. Thx you and kee up the good work!!!!!!

Reply
muneer
12/28/2013 02:38:05 am

i wrote a program to detect which key is pressed.but my program not working. i don't know what is problem with it...pls help me..
program:
import java.awt.event.KeyEvent;


public class hellow {
public static void main(String[] arg){
keypressed();}
public static void keypressed() {
KeyEvent e = null;
// TODO Auto-generated method stub
System.out.println(e.getKeyCode());
}

}

Reply
Aleksandar link
1/18/2014 09:17:49 pm

Zip link is

http://www.kilobolt.com/uploads/1/2/5/7/12571940/kiloboltgame3.zip

Reply
Teddy
2/11/2014 06:32:45 pm

Hi, I have tried till day 3 but it says applet not initialized.
What could be the problem

Reply
Gershie
2/11/2014 11:21:48 pm

Download file for today's lesson is not working... link goes to next day's lesson... Otherwise excellent.. keep it up

Reply
Jon
2/16/2014 12:50:12 am

Thanks for these tutorials, however for some reason i cant run this code. When i attempt to run it i get java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "modifyThreadGroup")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at sun.applet.AppletSecurity.checkAccess(Unknown Source)
at java.lang.ThreadGroup.checkAccess(Unknown Source)
at java.lang.Thread.init(Unknown Source)
at java.lang.Thread.init(Unknown Source)
at java.lang.Thread.<init>(Unknown Source)
at kiloboltgame.StartingClass.start(StartingClass.java:21)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Has anyone else had this problem

Reply
Hasan
3/22/2014 04:50:16 am

yeah me too
i have no solution if you know it please help

Reply
Hasan
3/22/2014 04:50:29 am

yeah me too
i have no solution if you know it please help

Reply
Sven
2/21/2014 06:40:07 pm

I encounter the error THE SERIALIZABLE CLASS ... DOES NOT DECLARE A STATIC FINAL SERIALVERSIONUID FIELD OF TYPE LONG

Fixed it with @SuppressWarnings("serial")

but i wonder this is not mentioned in the tutorial

any explanations for me?

Reply
Jesus
3/9/2014 03:31:22 pm

Im getting a warning message on the class definition.

The serializable class StartingClass does not declare a static final serialVersionUID field of type long

On the quick fix options, the first one says: "Add default serial version ID"
which adds the following line:

private static final long serialVersionUID = 1L;

What's this warning about?

Reply
Hasan
3/22/2014 04:48:42 am

Great Lessons really
but i cant run my program this is the code :
package kiloboltgame;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class StartingClass extends Applet implements Runnable, KeyListener {

@Override
public void init() {

setSize(800, 480);
setBackground(Color.BLACK);
setFocusable(true);
addKeyListener(this);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("Q-Bot Alpha");
}

@Override
public void start() {
Thread thread = new Thread(this);
thread.start();
}

@Override
public void stop() {
// TODO Auto-generated method stub
}

@Override
public void destroy() {
// TODO Auto-generated method stub
}

@Override
public void run() {
while (true) {

repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

@Override
public void keyPressed(KeyEvent e) {

switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Move up");
break;

case KeyEvent.VK_DOWN:
System.out.println("Move down");
break;

case KeyEvent.VK_LEFT:
System.out.println("Move left");
break;

case KeyEvent.VK_RIGHT:
System.out.println("Move right");
break;

case KeyEvent.VK_SPACE:
System.out.println("Jump");
break;

}

}

@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Stop moving up");
break;

case KeyEvent.VK_DOWN:
System.out.println("Stop moving down");
break;

case KeyEvent.VK_LEFT:
System.out.println("Stop moving left");
break;

case KeyEvent.VK_RIGHT:
System.out.println("Stop moving right");
break;

case KeyEvent.VK_SPACE:
System.out.println("Stop jumping");
break;

}

}

@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}

}

please help and this is the errors :
java.lang.Error: Unresolved compilation problem:
The public type StartingClass must be defined in its own file

at kiloboltgame.StartingClass.<init>(Statingclass.java:9)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
HELP

Reply
Joe
4/4/2014 12:11:41 am

Hasan, it has been a couple of weeks since your post. Did you figure it out? My guess is you did not create the StartingClass.java file in the kilobotgame package. Go back to the beginning of the game lesson for instructions.

Reply
Brian A.
4/11/2014 02:19:06 pm

The link for KeyEvent is broken. This is probably the updated one:

http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html

Reply
sri
4/26/2014 04:33:29 pm

what does it mean?
Frame frame = (Frame) this.getParent().getParent();

Reply
adnanjt
6/23/2014 10:00:13 am

the link about the key events ( http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/KeyEvent.html ) is no longer a valid one.

Reply
Kailas
7/2/2014 07:48:03 pm

Incredible...

Really good tutorial, self-explanatory. Just wished to have the comments be displayed in priority of the dates, latest first. So that other users can be updated on them.

2) http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/event/KeyEvent.html link is no longer valid now.

Reply
Miguel
7/9/2014 05:23:30 am

Thanks for the tutorial!
Just some questions, I didn't really get how things keep working, instead of working one time only or something like that.
Are KeyEvents running on threads or something like that?
I'm a little confused, and I want to understand better how things work.
Thanks in advance. o/

Reply
James link
7/11/2014 04:09:23 am

Hey guys, so I am planning on going onto day 4 but for some reason my robot moves but only so far, he cannot move go to the end of the right side, What I mean is the character can only move on the left side and cant get to the right side of the scene.

Reply
Duncan
7/14/2014 05:06:33 am

The window applet that pops up just shows the background, I cant see anything else.
I imported the code into Intelli J Idea, no errors pop up.

Reply
Duncan
7/14/2014 05:09:06 am

Oh, and i can see in the console that it is running. When pressing arrow keys it says move, up stop moving up, etc. and it keeps repeating "Do not scroll the background"

Reply
Raja kayala
7/14/2014 05:35:39 pm

Hi,

very nice in explaining and covering the structure.. Thanks a ton..
I have one question. when we are painting an image in paint method, why is it calling mutiple times. i am able to see the method was calling repeatedly.. I created my own applet class and just overridden the paint method

Reply
acidpoison
12/2/2014 03:56:27 am

link to oracle site about keytyped is broken.

Reply
Shakti link
12/2/2014 04:31:14 pm

Nice tutorial . Please update the link for documentation of KeyTyped event . It's broken

Reply
Ryan link
12/14/2014 12:04:52 am

Thanks! This has been a really great tutorial so far. You explain things really well and I'm excited about how easy this actually seems to be! Will be following the whole tut and coming back for more. :)

Reply
Adam
12/28/2014 11:15:01 pm

Loving this tutorial so far, it's much more interesting following a tutorial that creates a game, as opposed to some less tactile, bean-counting app you'd find in a typical programming book.

One question. When I hold down a direction key, the console shows "Move left", "Stopped moving left" (for example) repeatedly until I remove my finger from the key. Shouldn't you only see the "Move left" entry appear over and again until you let go of the button, at which point "Stopped moving left" would appear? Can't see that I've missed anything here, happy to be proven wrong though!

Reply
Iwen
1/21/2015 10:16:09 pm

The "e" off :"public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
switch(e.getKeyCode()){."
Has a error!
Does somebody know how i can fix it ?

Reply
Rudolf
2/12/2015 02:11:03 am

This problem has been stated in an earlier comment, with replies that answer your problem

Reply
Matrix
2/15/2015 04:36:32 am

"Lesson #2-7: Listening for Key Presses

In this case, the key is e.getKeyCode(). e.getKeyCode() will return the code of the button that you press on the keyboard. (If you were to type System.out.println(e.getKeyCode()); , each time that you press a button, it will display the key code on the console)"

I don't quite understand this part, e.getKeyCode() why is it 'e', can it be any other letter?

Thanks.

Amazing tutorial series, thank you so much for this!

Reply
ms
7/9/2015 04:15:06 pm

what does this mean?

Frame frame = (Frame) this.getParent().getParent();

Reply
Josh
5/2/2016 03:02:48 pm

It is initializing the frame object to the grandparent(parent of parent) object.

Reply



Leave a Reply.

    Author

    James Cho is the lead developer at Kilobolt Studios. He is a college student at Duke University and loves soccer, music, and sharing knowledge. 


© 2014 Kilobolt, LLC. All rights reserved.