游戏网格无法在Android Studio中输出? [英] Game grid not outputting in Android Studios?

查看:63
本文介绍了游戏网格无法在Android Studio中输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在制作一个可玩益智游戏hashi的android应用.我目前正在努力输出游戏网格.我想输出一个2D数组,像波纹管-

I am currently making an android app which plays the puzzle game hashi. I am currently struggling to output the game grid. i want to output a 2d array like bellow-

1  0  0  0  1
0  2  0  0  2
2  0  3  0  1
0  0  0  0  0
0  0  2  0  2

但是,当我在模拟器中运行应用程序时,它只会输出空白的空白屏幕.

however when i run the application in the emulator it outputs just a blank white screen.

主要活动-

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new boardView(this));
    //sets the view to the board view to show the puzzle on open.
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

游戏板类-

public class boardView extends View {

    public float IslandX;
    public float IslandY;
    public int islandDiameter;

    private Canvas canvas;

    public boardView(Context context) {
        super(context);
    }
    int gameBoard[][] = {{0, 1, 0, 0, 1}, {0, 2, 0, 0, 2}, {2, 0, 3, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 2, 0, 2}};
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)

    public void DrawBoard(Canvas canvas){
        Paint Island = new Paint();
        Island.setStyle(Paint.Style.FILL);

        Island.setStyle(Paint.Style.FILL);

        float stepX = canvas.getWidth() / 5.f;
        float stepY = canvas.getHeight() / 5.f;

        for (int i = 0; i < 5; i++) {
            for (int R = 0; R < 5; R++) {
                IslandX = i * stepX;
                IslandY = R * stepY;
                if (gameBoard[i][R] == 0) {
                    Island.setColor(Color.BLUE);
                    canvas.drawOval(IslandX, IslandY, 50, 50, Island);

                } else if (gameBoard[i][R] == 1) {
                    Island.setColor(Color.BLACK);
                    canvas.drawOval(IslandX, IslandY, 50, 50, Island);

                } else if (gameBoard[i][R] == 2) {
                    Island.setColor(Color.BLUE);
                    canvas.drawOval(IslandX, IslandY, 50, 50, Island);

                }


            }
        }

        }

}

推荐答案

要使您的面板可见,您必须将代码从Drawboard移至覆盖的方法onDraw,并在onCreate方法中,必须在以下位置调用invalidate() boardView类的实例,该实例调用onDraw方法以更新屏幕(如果可见).

To make your board visible, you have to move your code from Drawboard to the overwritten method onDraw and in the onCreate method, you have to call invalidate() on an instance of the class boardView, that calls the onDraw method to update the screen, if visible.

这篇关于游戏网格无法在Android Studio中输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆