App运行一个简单的球应用程序粉碎

时间:2014-12-21 10:06:21

标签: java android multithreading

我正在尝试启动一个非常基本的应用程序 - 一个应该在屏幕上反弹的球但我在尝试在设备上启动应用时遇到错误。

这是我的代码,我可能有一些错误,但请向我解释它们是什么以及如何修复它们,我是Android开发的新手。

    public class Ball {

    float x;
    float y;

    boolean movingRight;
    boolean movingBottom;

    float screenWidth;
    float screenHeight;

    public Ball(float x, float y, float screenWidth, float screenHeight) {
        this.x = x;
        this.y = y;
        this.screenWidth = screenWidth;
        this.screenHeight = screenHeight - 150;
        this.movingBottom = false;
        this.movingRight = false;


    }

    public void move() {
        if (movingRight)
            this.x++;
        else
            this.x--;

        if (movingBottom)
            this.y++;
        else
            this.y--;

        if (this.x >= this.screenWidth)
            this.movingRight = false;

        else if (this.x <= 1)
            this.movingRight = true;


        if (this.y >= this.screenHeight)
            this.movingBottom = false;
        else if (this.y <= 1)
            this.movingBottom = true;
    }


    public float getX(){
        return this.x;
    }

    public float getY() {
        return this.y;
    }

public class MySurfaceView extends SurfaceView{
    private Ball ball;
    private SurfaceHolder holder;
    Paint paint;

    public Paint getBallPaint()
    {
        if(paint == null)
        {
            paint = new Paint();
            paint.setColor(Color.RED);
            paint.setStrokeWidth(3);
        }
        return this.paint;
    }
    public MySurfaceView(Context context) {
        super(context);
        holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                Canvas c = holder.lockCanvas(null);
                draw(c);
                holder.unlockCanvasAndPost(c);
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                                       int width, int height) {
            }
        });

    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        canvas.drawCircle(ball.x,ball.y,15,getBallPaint());
    }
}

    public class GameLoopThread extends Thread {
        private MySurfaceView view;
        private boolean running = false;

        public GameLoopThread(MySurfaceView view) {
            this.view = view;
        }

        public void setRunning(boolean run) {
            running = run;
        }

        @Override
        public void run() {
            while (running) {
                Canvas c = null;
                try {
                    c = view.getHolder().lockCanvas();
                    synchronized (view.getHolder()) {
                        view.draw(c);
                    }
                } finally {
                    if (c != null) {
                        view.getHolder().unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }
    public class MainActivity extends ActionBarActivity {

        Ball ball;

        MySurfaceView mySurfaceView;
        GameLoopThread gameLoopThread;
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            int screenWidth;
            screenWidth = getWindowManager().getDefaultDisplay().getWidth();
            int screenHeight;
            screenHeight = getWindowManager().getDefaultDisplay().getHeight();
            ball = new Ball(screenWidth / 2, screenHeight / 2, screenWidth,screenHeight);
            gameLoopThread = new GameLoopThread(mySurfaceView);
            gameLoopThread.start();
            mySurfaceView = new MySurfaceView(this);
            setContentView(mySurfaceView);
        }

1 个答案:

答案 0 :(得分:0)

我无法判断是否存在其他错误,但最明显的是您在创建之前使用mySurfaceView。

您应该将初始化顺序更改为:

        mySurfaceView = new MySurfaceView(this);
        setContentView(mySurfaceView);
        gameLoopThread = new GameLoopThread(mySurfaceView);
        gameLoopThread.start();