如何在Android游戏中添加分数计数器?

时间:2013-04-08 08:25:30

标签: android multithreading image counter

您好我想实施一个'图像分数计数器'在我的Android游戏中,但它不能很好地工作。然而它编译得很好,但很快当球到达屏幕边缘应用程序返回错误:

04-08 10:07:31.639: E/AndroidRuntime(8599): FATAL EXCEPTION: Thread-1176
04-08
10:07:31.639: E/AndroidRuntime(8599): java.lang.NullPointerException
04-08 10:07:31.639: E/AndroidRuntime(8599):at com.Pong.GameState.update(GameState.java:154)
04-08 10:07:31.639: E/AndroidRuntime(8599):at com.Pong.GameThread.run(GameThread.java:41)

我有3个文件来管理游戏: GameState.java GameView.java GameThread.java:

错误在:

GameState.java:

TopCounter.setImageResource(myImageList[i+1]);

GameThread.java:

_state.update();

这就是GameThread.java的样子:

package com.Pong;

import....

    class GameThread extends Thread {
        private SurfaceHolder surfaceHolder;
        private GameView gameView;
        private Paint _paint;
        private GameState _state;
        private boolean run = false;
        public GameThread(SurfaceHolder surfaceHolder, GameView gameView, Context context, Handler handler) {
            this.surfaceHolder = surfaceHolder;
            this.gameView = gameView;
            _paint = new Paint();
            _state = new GameState();
        }

        public void setRunning(boolean run) {
            this.run = run;
        }

        public SurfaceHolder getSurfaceHolder() {
            return surfaceHolder;
        }

        @Override
        public void run() {
            Canvas c;
            while (run) {
                c = null;

                try {
                    c = surfaceHolder.lockCanvas(null);

                    synchronized (surfaceHolder) {
                         gameView.onDraw(c);
                        _state.update();  //here is the error
                        _state.draw(c, _paint);
                    }
                } finally {
                    if (c != null) {
                        surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }


public GameState getGameState()
{
return _state;
}
}

这就是GameState.java的样子(我剪掉了不必要的线条):

package com.Pong;

Import ...

public class GameState extends Activity implements View.OnTouchListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gamestate);
        TopCounter = (ImageView) findViewById(R.id.counterT);
        BottomCounter = (ImageView) findViewById(R.id.counterB);

}
    int _screenWidth = 270;
    int _screenHeight = 395;   
    int _ballSize = 10;
    int _ballX = 100;
    int _ballY = 100;
    int _ballVelocityX = 3;
    int _ballVelocityY = 3;
    int _batLength = 75;
    int _batHeight = 10;
    int _topBatX = (_screenWidth / 2+25) - (_batLength / 2);
    int _topBatY = 54;
    int _bottomBatX = (_screenWidth / 2+25) - (_batLength / 2); 
    int _bottomBatY = 392;
    int i=0;
    // under is matrix that contains images to my counter.
    int[] myImageList = new int[]{
            R.drawable.counter00,
            R.drawable.counter01,
            R.drawable.counter02,
            R.drawable.counter03,
            R.drawable.counter04,
            R.drawable.counter05,
            R.drawable.counter06,
            R.drawable.counter07,
            R.drawable.counter08,
            R.drawable.counter09
            };
    ImageView TopCounter;
    ImageView BottomCounter; 

    public GameState()
    {}

    public void update() {

        _ballX += _ballVelocityX;
        _ballY += _ballVelocityY;


    if( _ballY > _screenHeight){
            _ballX = _screenWidth/2;
            _ballY = _screenHeight/2;
            TopCounter.setImageResource(myImageList[i+1]); //here is the error
            _toppoint=_toppoint+1;
            _topBatX =_screenWidth/2-10;
            _bottomBatX =_screenWidth/2-10;
            }       

        if( _ballY < 50){
            _ballX = _screenWidth/2;
            _ballY = _screenHeight/2;
            BottomCounter.setImageResource(myImageList[i+1]);
            _bottompoint=_bottompoint+1;
            _topBatX =_screenWidth/2-10;
            _bottomBatX =_screenWidth/2-10;
            } 
        if (_toppoint==9){

        } 
        else if (_bottompoint==9) {

        }

        if(_ballX > _screenWidth || _ballX <= 40){
            _ballVelocityX *= -1;
            }
        if(_ballX > _topBatX && _ballX < _topBatX+_batLength && _ballY < _topBatY){
            _ballVelocityY *= -1;
        }
        if(_ballX > _bottomBatX && _ballX < _bottomBatX+_batLength && _ballY > _bottomBatY){
            _ballVelocityY *= -1;
        }
        if (_ballX == 40+_batLength) {
            _topBatX=40;
            } else if (_ballX > 40+_batLength && _ballX <=  _screenWidth){
                _topBatX=_ballX-_batLength+6;
                } 
        if (_bottomBatX < 46) {
            _bottomBatX=46;
        } else if (_bottomBatX+_batLength > _screenWidth){
            _bottomBatX=_screenWidth-_batLength+6;
        }
        }
    public void draw(Canvas c, Paint paint) {
    paint.setARGB(255, 234, 222, 199);
    c.drawRect(new Rect(_ballX,_ballY,_ballX + _ballSize,_ballY + _ballSize), paint);
    c.drawRect(new Rect(_topBatX, _topBatY, _topBatX + _batLength, _topBatY + _batHeight), paint); //top bat
    c.drawRect(new Rect(_bottomBatX, _bottomBatY, _bottomBatX + _batLength, _bottomBatY + _batHeight), paint); //bottom bat
    }


}

在我的XML文件中:

 <ImageView
        android:id="@+id/counterT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/counter00"
       android:layout_gravity="left|center_vertical"
         />
        <ImageView
        android:id="@+id/counterB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/counter00" />

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我认为这是你在GameState.java文件中的问题

// under is matrix that contains images to my counter.
int[] myImageList = new int[]{
        R.drawable.counter00,
        R.drawable.counter01,
        R.drawable.counter02,
        R.drawable.counter03,
        R.drawable.counter04,
        R.drawable.counter05,
        R.drawable.counter06,
        R.drawable.counter07,
        R.drawable.counter08,
        R.drawable.counter09
        };

这不是你宣布一个矩阵的方式!你想要一个整数数组 尝试将其声明为:

int[] myImageList = {
    R.drawable.counter00,
    R.drawable.counter01,
    R.drawable.counter02,
    R.drawable.counter03,
    R.drawable.counter04,
    R.drawable.counter05,
    R.drawable.counter06,
    R.drawable.counter07,
    R.drawable.counter08,
    R.drawable.counter09
    };

编辑:

    if (TopCounter != null){
       TopCounter.setImageResource(myImageList[i]);
       i++;
    }

你也不增加列表索引,它总是等于0,而不是那个 将索引初始化为1,然后在更改图像源时将其增加1 但是你还需要检查索引是否超过列表大小