Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Need aid on unresponsive keylistener in my game applet  (Read 783 times)
0 Members and 1 Guest are viewing this topic.
Offline Seiferia

Junior Newbie





« Posted 2005-07-04 07:18:37 »

Hi everyone, I am facing a dilemma regarding about my BreakOut game applet that I am developing for the PJEE3.0.2 environment.

It seems that the KeyListener I have implemented in my applet does not respond. I pressed the left button to move the player paddle left, nothing happens.

I tried to press the F1 button to start a new game, nothing happens. You get the idea.


My dilemma starts at the methods keyTyped and keyPressed.

The words RC.LEFT and RC.DOWN etc etc  are instances of a class which I have to work with to make this applet compatible with a Phillips Streamium device.

I would appreciate it very much if you to give me any advice on my problem. Thank you.



/**
 * @(#)Break.java
 *
 * Sample Applet application
 *
 * @author
 * @version 1.00 05/06/28
 */
 
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

import com.philips.streamium.TVApplet;
import com.philips.streamium.TVLayout;
import com.philips.streamium.ColorButton;
import com.philips.streamium.ColorButtonBar;
import com.philips.streamium.RC;
import com.philips.streamium.Debug;
import com.philips.streamium.LoadingScreen;
import com.philips.streamium.Streamium;


//KEY NOTES TO TAKE OFF WHEN PROGRAMMING AN STREAMIUM APPLET
//
//
//Some Streamium devices can support a Streamium Applet's maxiumum Width of 780
//
//Most Streamium can support an Applet's Width of 510
//Most Streamium can support an Applet's height of 480
//
//The left and right components of a streamium applet measure at :
//105 for its width
//480 for its height
//
//


public class Break extends TVApplet implements Runnable, KeyListener {
   
   Thread THREAD = null;
   
   Image ii;
   Graphics gh;
   
   private Image dbImage;
    private Graphics dbg;
   
   boolean   currently_playing_game;
   boolean   show_title = true;
   
   String status;
   
   int count;
   int number;
   int applet_width = 510;
   int applet_height = 480;
   int bricks_per_line = 16;
   int brick_width = 25;
   int brick_height = 10;
   int border_width = 15;
   int space_inbetween_bricks = 5;
   int number_of_brick_lines = 10;
   
   int starting_y_position_for_bricks = 50;
   
   int scoreheight = 20;
   
   int paddle_position_for_physics = 240;
   
   int paddle_x_position;
   int paddle_y_position = 50;
   
   int ball_x_position = 240;
   int ball_y_position = 305;
   
   int ball_y_position_speed = 2;
   int ball_x_position_speed;
   
   int paddle_width = 30;
   int paddle_height = 7;
   
   
   int top_border = 15;
   int left_border = 15;
   int right_border = 495;
   
   int player_score = 0;
   int lives_left = 4;
   
   int ball_size = 5;
   
   int movement_rate = 0;
   
   boolean[] show_bricks;
   
   
   private ColorButtonBar m_colorButtonBar = null;
    private ColorButton m_cbTypeface = null;
    private ColorButton m_cbSizeInc = null;
    private ColorButton m_cbSizeDec = null;
    private ColorButton m_cbExit = null;
   
   
   
   final int screen_delay = 300;
   
   public void init()
      {
         //initializes the game's values
         //init is simplified for the purposes of documenting the
         //game to be simple.
         
             m_colorButtonBar = new ColorButtonBar(this);

               m_cbTypeface = null;

              m_cbTypeface = new ColorButton("Typeface");
               m_colorButtonBar.setButton(ColorButtonBar.RED, m_cbTypeface);


               m_cbSizeInc = new ColorButton("Bigger");
               m_colorButtonBar.setButton(ColorButtonBar.GREEN, m_cbSizeInc);

               m_cbSizeDec = new ColorButton("Smaller");
               m_colorButtonBar.setButton(ColorButtonBar.YELLOW, m_cbSizeDec);

               m_cbExit = new ColorButton("Exit");
               m_colorButtonBar.setButton(ColorButtonBar.BLUE, m_cbExit);

      this.add(TVLayout.BUTTONS, m_colorButtonBar);
      validate();
      this.addKeyListener(m_colorButtonBar);
         
         
         
         
         this.addKeyListener(this);
         
         setBackground(Color.black);
         
         currently_playing_game = false;
         
         show_bricks = new boolean[bricks_per_line * number_of_brick_lines];
            
         gameInitiate();
         
         
      }
      
   public void gameInitiate()
      {
         //initializes the game's gameplay values
         
         ball_x_position_speed = 0;
         
         count = screen_delay;
         
         paddle_x_position = 225;
         
         
         initiateGameBrick();
         
      }
      
   public void initiateGameBrick()
      {
         //initializes the bricks in the game
         
         int i;
                                       //* For every brick in the game,
          for (i=0; i<bricks_per_line * number_of_brick_lines; i++)      //* set true.
               show_bricks = true;      
         
      }
      
      
      
      
      
   public void keyPressed(KeyEvent keyEvent)
         {
         int key = keyEvent.getKeyCode();
         
         
         
         
         
         
         switch(key)
         {
          case RC.LEFT:
                //Make paddle go left
                paddle_x_position-=6;
                repaint();
               break;
         case RC.RIGHT:
               //Make paddle go right
               paddle_x_position+=6;
               repaint();
            break;
            
       case RC.RED:
             //Set a new game
             currently_playing_game = true;
             gameInitiate();
             repaint();
             break;
             
       case RC.GREEN:
             //Quit the game
             currently_playing_game = false;
             repaint();
            break;
                  
         case RC.BLUE:
               Streamium.exit(Streamium.EXIT_OK);
               break;
            }
            
         }
  

   public void keyTyped(KeyEvent keyEvent)
         {
            
         int key = keyEvent.getKeyCode();
      
      /*
          if (currently_playing_game)                           //*
          {                                 //*   Checks if the game is started,
            if (key == RC.LEFT)               //* if not started, it will only respond
                paddle_x_position-=6;                     //* to typing 's' or 'S'.
            if (key == RC.RIGHT)               //*
              paddle_x_position+=6;                        //* If game is started, the left, right,
            if (key == RC.GREEN)               //* escape buttons will respond.
              currently_playing_game=false;                     //*
           }                                 //* Left and right are the controls
       else                              //* Escape is to quit the game and go back
       {                                 //* to starting screen.
            if (key == RC.RED)
            {
                 currently_playing_game=true;
              gameInitiate();
            }
    
         }
         
      */
      
      
      
      
      
         switch(key)
            {
          case RC.LEFT:
                //Make paddle go left
                paddle_x_position-=6;
                repaint();
               break;
         case RC.RIGHT:
               //Make paddle go right
               paddle_x_position+=6;
               repaint();
            break;   
          case RC.RED:
                //Set a new game
             currently_playing_game = true;
             repaint();
               break;
               
         case RC.GREEN:
                //Quit the game
             currently_playing_game = false;
             repaint();
            break;
                  
         case RC.BLUE:
               Streamium.exit(Streamium.EXIT_OK);
               break;   
            }   
         }

   public void keyReleased(KeyEvent keyEvent){}
      
      
      
      
      
      
      
   
   public void start()
      {
         if (THREAD == null)
         {
            THREAD = new Thread(this);
            THREAD.start ();
         }
      }
   

   public void stop()
      {
          if (THREAD != null)
          {
               THREAD.stop();
               THREAD = null;
           }
      }
      
      
      
      
      
      
      
   public void run()
      {
         Graphics g;
         
         Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
         g=getGraphics();
         
         while(true)
          {
             
          if (currently_playing_game)
               PlayGame();
            else
               ShowIntroScreen();
             
             
             
            try
            {   
              paint(g);
              THREAD.sleep(50);
              
            }
            catch (Exception e)
            {
              break;
            }
         
         
         
      }
      
   }   
      
      
  
      public void update(Graphics g)
      {
         
         
      if (dbImage == null)
      {
      dbImage = createImage (510, 480);
      dbg = dbImage.getGraphics ();

      }

      // clear screen in background
      dbg.setColor (getBackground ());
      dbg.fillRect (0, 0,510,480);

      // draw elements in background
      dbg.setColor (getForeground());
      paint (dbg);

      // draw image on the screen
      g.drawImage (dbImage, 0, 0, this);
      
      
       //paint(g);
          
   
Offline Jackal von ÖRF

Junior Member





« Reply #1 - Posted 2005-07-04 11:38:21 »

      this.add(TVLayout.BUTTONS, m_colorButtonBar);
      validate();
      this.addKeyListener(m_colorButtonBar);
Is that supposed to be m_colorButtonBar.addKeyListener(this);?

Why do keyPressed and keyTyped contain the same code?

Have you tested whether the keyPressed etc. methods are being called at all? Print the KeyEvents to System.err whenever events are recieved.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (37 views)
2014-07-24 01:59:36

Riven (38 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (58 views)
2014-07-18 06:55:21

Zero Volt (49 views)
2014-07-17 23:47:54

danieldean (41 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!