还是要重申一遍,因为只是转化,所以原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;
}
}