2015-09-17 55 views
0

我一直在寫youtube pong ball canvas教程,它工作正常。j2me,退出畫布使用*鍵代碼按鈕

但是,我試圖通過先點擊midlet之前先進入列表來改變應用程序,然後再直接跳到畫布上。所以當你運行應用程序。乒乓球midlet出現,然後當你點擊它...它進入一個選項列表,發揮和幫助。當你點擊播放時,它會進入pongball畫布,當你點擊幫助時,它會轉到表單。

我希望能夠退出畫布並返回列表,而不必按下結束通話按鈕以退出畫布...我不確定在'j2me in a nutshell'這本書中看哪裏,我無法找到解決方案。

幫助。

這裏是代碼...兩個MIDlet類和pongball類..

import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 

public class CanvasTut extends MIDlet implements CommandListener { 

private Display disp = Display.getDisplay(this); 

public List Options; 

private Form fmHelp;  

private PongBallCanvas c1 = new PongBallCanvas(); 

public CanvasTut(){ 
    String Games[] = {"Play", "Help "}; 
    Options = new List("Pong Ball", List.IMPLICIT, Games, null); 

    Options.setCommandListener(this); 

    fmHelp = new Form("How To Play"); 
} 

public void startApp() { 

    disp.setCurrent(Options); 
} 

public void pauseApp() { 
} 

public void destroyApp(boolean unconditional) { 
} 

public void commandAction(Command c, Displayable d) { 
    if(d==Options){ 
     if(c==List.SELECT_COMMAND){ 
      switch(Options.getSelectedIndex()){ 
       case 0: 
        c1.start(); 
        Display.getDisplay(this).setCurrent(c1); 
        break; 

       case 1: 
        Display.getDisplay(this).setCurrent(fmHelp); 
        break; 
      } 
     } 
    } 
} 
} 


//--------------------------------------end of midlet class----------------- ----------------------------------------- 


import java.io.IOException; 
import javax.microedition.lcdui.*; 
import javax.microedition.lcdui.game.*; 

public class PongBallCanvas extends GameCanvas implements Runnable{ 

private Image ballImg; 
private Sprite ballSprite; 
private int ballX = getWidth()/2; 
private int ballY = getHeight()/2; 
private final int ballXmove = 3; 
private final int ballYmove = 1; 
private int ballDirection = 0; 
private int sleepTime = 20; 
private int Collision = 0; 

public PongBallCanvas(){ 
    super(false); 
} 

public void run() { 
    while(true){ 
     try{ 
      updateScreen(getGraphics()); 
      Thread.sleep(sleepTime); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 

public void start(){ 

    try { 
     ballImg = Image.createImage("/PongBallPic.png"); 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 


    ballSprite = new Sprite(ballImg, 32, 32); 
    ballSprite.defineReferencePixel(16, 16); 
    ballSprite.setRefPixelPosition(ballX, ballY); 

    Thread runner = new Thread(this); 
    runner.start(); 
} 

public void createBackground(Graphics g){ 
    g.setColor(0x000000); 
    g.fillRect(0, 0, getWidth(), getHeight()); 
    g.setColor(0xFFFFFF); 
    g.drawString("Collision: " + Collision, 0, 0, g.TOP | g.LEFT); 


} 

public void updateScreen(Graphics graphics){ 
    createBackground(graphics); 
    moveBall(); 
    ballSprite.setRefPixelPosition(ballX, ballY); 
    ballSprite.paint(graphics); 
    flushGraphics(); 
} 

public void moveBall(){ 
    if(ballDirection == 0){ 
     ballX += ballXmove; 
     ballY -= ballYmove; 
    }else if(ballDirection == 1){ 
     ballX += ballXmove; 
     ballY += ballYmove; 
    }else if(ballDirection == 2){ 
     ballX -= ballXmove; 
     ballY += ballYmove; 
    }else if(ballDirection == 3){ 
     ballX -= ballXmove; 
     ballY -= ballYmove; 
    } 
    if(ballDirection == 3 && ballX < 16){ 
     ballDirection = 0; 
     Collision++; 
    }else if(ballDirection == 3 && ballY < 16){ 
     ballDirection = 2; 
     Collision++; 
    }else if(ballDirection == 0 && ballY < 16){ 
     ballDirection = 1; 
     Collision++; 
    }else if(ballDirection == 0 && ballX > getWidth()- 16){ 
     ballDirection = 3; 
     Collision++; 
    }else if(ballDirection == 1 && ballX > getWidth() - 16){ 
     ballDirection = 2; 
     Collision++; 
    }else if(ballDirection == 1 && ballY > getHeight()- 16){ 
     ballDirection = 0; 
     Collision++; 
    }else if(ballDirection == 2 && ballX < 16){ 
     ballDirection = 1; 
     Collision++; 
    }else if(ballDirection == 2 && ballY > getHeight()- 16){ 
     ballDirection = 3; 
     Collision++; 
    } 
} 

} 
//-------------------------------end of pong ball class--------------------- ------------------------------- 

回答

0

由於GameCanvasCanvas延伸可以覆蓋在你的PongBallCanvas類keyPressed方法。

protected void keyPressed(int keyCode) { 
    if (keyCode == Canvas.KEY_STAR) { 
     // show list 
    } 
} 
+0

我想,我想,以及MIDlet跑,但沒反應......讓我再試一次,並確保...感謝 – Aviwe

+0

是的......沒有反應...... MIDlet的只是運行像代碼不存在 – Aviwe

+0

如果您替換System.out調用的「show list」註釋,您是否在輸出中看到該行? –