0

嗨,我想在我的 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:

错误在:

游戏状态.java:

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

游戏线程.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" />

有人可以帮忙吗?

4

1 回答 1

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,但您还需要检查索引是否超过列表大小

于 2013-04-08T08:42:37.923 回答