GameView,Sprites和SurfaceHolder.Callback

时间:2018-06-02 11:34:53

标签: sprite surfaceholder

我买了书Android:为平板电脑编程游戏,Jeremyy Kerfs。我尝试运行第一个示例代码,它不起作用。我认为这是旧书,代码可能太旧了。这段代码改变了位图的位置,但它没有发生?我不知道为什么?

public class GameView extends SurfaceView implements SurfaceHolder.Callback
{
    private GameLogic gameLogic;
    private SpriteObject spriteObject;

    public GameView(Context context)
    {
        super(context);

        setFocusable(true);
        spriteObject = new SpriteObject(BitmapFactory.decodeResource(getResources(),R.drawable.star),150,150);
        gameLogic = new GameLogic(getHolder(),this);
        getHolder().addCallback(this);

    }


    @Override
    public void surfaceCreated(SurfaceHolder holder)
    {
        gameLogic.setGameState(GameLogic.RUNNING);
        gameLogic.start();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        canvas.drawColor(Color.YELLOW);
        spriteObject.draw(canvas);
    }
    public void update()
    {
        spriteObject.update();
    }
}

GameLogic类:

public class GameLogic extends Thread
{
    SurfaceHolder surfaceHolder;
    GameView gameView;
    private int gameState;
    public static final int PAUSE = 0;
    public static final int READY = 1;
    public static final int RUNNING = 2;

    public GameLogic(SurfaceHolder surfaceHolder, GameView gameView)
    {
        super();
        this.surfaceHolder = surfaceHolder;
        this.gameView = gameView;
    }
    public void setGameState(int gameState)
    {
        this.gameState = gameState;
    }
    public int getGameState()
    {
        return gameState;
    }

    @Override
    public void run()
    {
        super.run();
        Canvas canvas;
        while(gameState == RUNNING)
        {
            canvas = null;
            try
            {
                canvas = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder)
                {
                   gameView.update();
                   gameView.draw(canvas);
                }
            }
            finally
            {
                if(canvas != null)
                {
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
        }
    }
}

和Sprite类:

public class SpriteObject
{
    private Bitmap bitmap;
    private int x;
    private int y;
    private int xMove = 5;
    private int yMove = 5;

    public SpriteObject(Bitmap  bitmap,int x, int y)
    {
        this.bitmap = bitmap;
        this.x = x;
        this.y = y;
    }

    public int getX()
    {
        return x;
    }
    public int getY()
    {
        return y;
    }

    public Bitmap getBitmap()
    {
        return bitmap;
    }
    public void setX(int x)
    {
        this.x = x;
    }
    public void setY(int y)
    {
        this.y = y;
    }
    public void setBitmap(Bitmap bitmap)
    {
        this.bitmap = bitmap;
    }
    public void draw(Canvas canvas)
    {
        canvas.drawBitmap(bitmap,x-(bitmap.getWidth()/2),y-(bitmap.getHeight()/2),null);
    }
    public void update()
    {
        x += (xMove);
        y += (yMove);
    }
}

当我跑步时,我看到黑屏。我认为这是默认颜色?但是我画了黄色。在GameLogiic屏幕中第一次循环后,将更改为WHITE,以及应用程序中发生的一切...有人可以提供帮助吗?

0 个答案:

没有答案