還是要重申一遍,因為只是轉化,所以原J2ME的問題,我不會進行修改。所以這個游戲還是那個樣,各種不搭,非常不搭。。。因為實例需要,我在androidSprite中添加了幾個方法,也修改paint方法.在轉化過程中,遇到了最大的難點是matrix 的使用,他用在圖片的翻轉上。下面給出效果圖,和源碼.
- package com.wjh.demon_9;
- import com.wjh.midp_me.androidSprite;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.media.MediaPlayer;
- import android.util.Log;
- import android.vIEw.KeyEvent;
- import android.vIEw.SurfaceHolder;
- import android.view.SurfaceVIEw;
- import android.vIEw.SurfaceHolder.Callback;
- public class MainView extends SurfaceVIEw implements Callback,Runnable{
- int keyCode = 0;
- String keyAction = "";
- Thread gameThread = null;
- boolean isGame = true;
- SurfaceHolder holder = null;
-
- public androidSprite m_spDancer; //管理MM精靈
- public ArrowSprite m_aArr_S[]; //管理靜態箭頭
- public ArrowSprite m_aArr_M[]; //管理動態箭頭
- public int m_aSteps[] = { //存儲舞步
- 1, 0, 2, 3, 1, 0, 1, 3, 0, 1, 2, 3,
- 3, 1, 1, 2, 1, 2, 1, 0, 1, 2, 1, 1,
- 0, 0, 1, 3, 0, 0, 2, 2, 1, 1, 2, 2,
- 1, 0, 2, 1, 3, 3, 0, 3, 0, 0, 2, 0 };
- public int m_nStep = 0; //下一個舞步在m_aSteps中的索引
- MediaPlayer m_Player;
-
- Paint backPaint = null;
- Paint forePaint = null;
- public MainVIEw(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- setFocusable(true);
- getHolder().addCallback(this);
- holder = this.getHolder();
-
- //讀取MM精靈圖片,設置MM精靈的參考點及位置
- Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.dancer);
- m_spDancer = new androidSprite( image, 61, 80 );
-
-
- //讀取箭頭圖片,創建兩種箭頭數組
- Bitmap image2 = BitmapFactory.decodeResource(getResources(), R.drawable.arrow);
- m_aArr_S = new ArrowSprite[4];
- for( int n = 0; n < m_aArr_S.length; n ++ ){
- m_aArr_S[n] = new ArrowSprite( image2, 15, 15 );
- m_aArr_S[n].StartStatic( 15 + n * 20, 30, n );
- }
- m_aArr_M = new ArrowSprite[6]; //最多同時顯示6個動態箭頭
- for( int n = 0; n < m_aArr_M.length; n ++ ){
- m_aArr_M[n] = new ArrowSprite( image2, 15, 15 );
- }
-
- m_Player = MediaPlayer.create(context, R.raw.music);
- m_Player.setLooping(true);
- m_Player.start();
-
- backPaint = new Paint();
- backPaint.setColor(Color.BLACK);
- forePaint = new Paint();
-
- }
- @Override
- public void run() {
- //獲取系統當前時間,並將時間換算成以毫秒為單位的數
- long T1 = System.currentTimeMillis();
- long T2 = T1;
- while(isGame){
- T2 = System.currentTimeMillis();
- if( T2 - T1 > 100 ){ //間隔100毫秒
- T1 = T2;
- input();
- logic();
- doDraw();
- }
- }
-
- }
- //開始游戲主線程
- public void start()
- {
- m_spDancer.defineReferencePixel( 30, 40 );
- m_spDancer.setRefPixelPosition(getWidth() - 50,
- getHeight()/2);
- if(gameThread == null)
- {
- gameThread = new Thread(this);
- gameThread.start();
- }
- }
- //停止游戲主線程
- public void stop()
- {
- isGame = false;
- if(gameThread != null)
- {
- try {
- gameThread.join();
- this.m_Player.stop();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- //輸入判斷
- public void input()
- {
- m_spDancer.setFrame( 0 );
- //如果按下方向鍵的上鍵,MM向上跳舞
- if( keyCode == KeyEvent.KEYCODE_DPAD_UP)
- m_spDancer.setFrame( 1 );
- //如果按下方向鍵的下鍵,MM向下跳舞
- if( keyCode == KeyEvent.KEYCODE_DPAD_DOWN )
- m_spDancer.setFrame( 2 );
-
- //如果按下方向鍵的左鍵,MM向左跳舞
- if( keyCode == KeyEvent.KEYCODE_DPAD_LEFT)
- m_spDancer.setFrame( 3 );
- //如果按下方向鍵的右鍵,MM向右跳舞
- if( keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
- m_spDancer.setFrame( 4 );
-
- keyCode = -1;
- }
- public long m_LogicT1 = System.currentTimeMillis();
- //邏輯判斷
- public void logic()
- {
- for( int n = 0; n < m_aArr_M.length; n ++ )
- m_aArr_M[n].Logic();
-
- long mT2 = System.currentTimeMillis();
- if( mT2 - m_LogicT1 > 1500 ){
- m_LogicT1 = mT2;
- for( int n = 0; n < m_aArr_M.length; n ++ ){
- if( m_aArr_M[n].isVisible() == false ){
- m_aArr_M[n].StartMove( 15 + m_aSteps[n] * 20,
- 110, 30, 1, m_aSteps[n] );
- break;
- }
- }
- m_nStep ++;
- if( m_nStep > m_aSteps.length )
- m_nStep = 0;
- }
- }
- public void doDraw()
- {
- Canvas c = null;
- try
- {
- c = holder.lockCanvas();
- synchronized (holder) {
- paint(c);
- }
- }finally{
- if(c != null)
- {
- holder.unlockCanvasAndPost(c);
- }
- }
- }
- //畫圖
- public void paint(Canvas canvas)
- {
- //用黑色清屏
- canvas.drawRect(0, 0, getWidth(), getHeight(), backPaint);
- //顯示MM及所有箭頭圖像
- m_spDancer.paint(canvas,forePaint);
- for( int n = 0; n < m_aArr_S.length; n ++ ){
- m_aArr_S[n].paint(canvas,forePaint);
- }
- for( int n = 0; n < m_aArr_M.length; n ++ ){
- m_aArr_M[n].paint(canvas,forePaint);
- }
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- // TODO Auto-generated method stub
-
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- start();
-
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- stop();
-
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- // TODO Auto-generated method stub
- this.keyCode = keyCode;
- return true;
- }
-
- }