Monday, November 4, 2013

GameAction

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class GameActionExample extends MIDlet
{
 private Display d;
 private MyCanvas2 c;
 public GameActionExample()
 {
  d=Display.getDisplay(this);
  c=new MyCanvas2(this);
 }
 protected void startApp()
 {
  d.setCurrent(c);
 }
 protected void pauseApp()
 {
 }
 protected void destroyApp(boolean b)
 {
 }

class MyCanvas2 extends Canvas implements CommandListener
{
 private Command exit;
 private String m,dir;
 private GameActionExample gae;
 private int x,y;
 public MyCanvas2 (GameActionExample gae)
 {
  x=5;
  y=5;
  dir="use Game keys";
  this.gae=gae;
  exit=new Command("Exit",Command.EXIT,1);
  addCommand(exit);
  setCommandListener(this);
 }
 protected void paint(Graphics g)
 {
  g.setColor(255,255,255);
  g.fillRect(0,0,getWidth(),getHeight());
  g.setColor(255,0,0);
  g.drawString(m,x,y,Graphics.TOP|Graphics.LEFT);
 }
 public void commandAction(Command c, Displayable dd)
 {
  if(c==exit)
  {
   destroyApp(true);
  notifyDestroyed();
  }
}
protected void keyPressed(int key)
{
 switch(getGameAction(key))
 {
  case Canvas.UP:
    m="up";
    y--;
    break;
  case Canvas.DOWN:
   m="down";
   y++;
   break;
  case Canvas.LEFT:
   m="left";
   break;
  case Canvas.RIGHT:
   m="right";
   x++;
   break;
  case Canvas.FIRE:
   m="FIRE";
   break;
  }//switch
  repaint();
 }
}  
 

No comments:

Post a Comment