【问题标题】:Game grid not outputting in Android Studios?游戏网格未在 Android Studios 中输出?
【发布时间】:2017-01-30 20:36:04
【问题描述】:

我目前正在制作一个玩益智游戏哈希的安卓应用。我目前正在努力输出游戏网格。我想输出一个像下面这样的二维数组-

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

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

主要活动-

@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 方法
  • 在哪里以及如何称呼它?
  • 在您的 boardView 视图中覆盖 void onDraw (Canvas canvas) 并将您的代码移到那里
  • 这行得通,但是它仍然只输出左上角的第一个椭圆,而没有其他椭圆?
  • 请停止重复同样的问题。只需使用您尝试过的任何新信息或代码编辑您的原始帖子,或者解释为什么提供的答案不起作用,它就会被挤到活动队列的顶部。没有必要有多个相同的问题。我已经关闭了其他人作为这个重复,因为你在这里得到了答案和赞成票。

标签: java android android-studio mobile


【解决方案1】:

为了让你的板子可见,你必须将你的代码从 Drawboard 移动到被覆盖的方法 onDraw 并且在 onCreate 方法中,你必须在类 boardView 的一个实例上调用 invalidate(),它调用 onDraw 方法来更新屏幕(如果可见)。

【讨论】:

  • 我在 oncreate 方法中写了什么代码?我是否复制 on create 方法中的代码?
  • 类似的东西:boardView bv = = (boardView) findViewById(R.id."insert ID of boardView item in XML-Layout here"); bv.invalidate();
  • “我是否复制 on create 方法中的代码”,你的意思是从public void DrawBoard(Canvas canvas) 编码,不,你把这段代码复制到@Override protected void onDraw(Canvas canvas)
  • 它仍然只输出左上角数组中的第一个整数,而不输出数组的其余部分?
  • 是否在 XML-Layout 文件中将视图定义为水平和垂直的 match_parent/fill_parent?
猜你喜欢
  • 2012-01-06
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 2017-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多