变量值随机变化。在简单的Android游戏中

时间:2014-05-28 20:15:57

标签: java android screen

我有这个应用程序游戏,在开始时,创建两个3x3网格的正方形。我的一个函数setGridDimensions()进入并指定网格中每个方块的最小和最大X / Y值(基于屏幕大小)。然后用它来绘制网格。奇怪的是,有时正方形的x或y值会随着另一个正方形随机切换,导致网格中出现漏洞。我似乎无法弄清楚为什么会这样。任何帮助表示赞赏。 注意我知道绘图功能有效,这就是它不显示的原因。在调用x / y值之前会发生一些事情。

实例

gameGrid = new Grid(Settings.gridWidth, Settings.gridHeight);
    target = new Grid(Settings.gridWidth, Settings.gridHeight);

这是我的函数代码

private void setGridDimensions()
 {
     for (int x=0; x<gameGrid.grid.length; x++)
        {
          for (int y= 0; y< gameGrid.grid.length; y++)
          {
             gameGrid.grid[x][y].minX =  (int)((Settings.WIDTH * .2) + Settings.padding + (Settings.squareWidth * x));
             gameGrid.grid[x][y].maxX = gameGrid.grid[x][y].minX + Settings.squareWidth;
             gameGrid.grid[x][y].minY = Settings.padding + (Settings.squareHeight * y);
             gameGrid.grid[x][y].maxY = gameGrid.grid[x][y].minY + Settings.squareHeight;

             target.grid[x][y].minX = (int)((Settings.WIDTH * .8) + Settings.padding + (Settings.squareWidth/4 * x));
             target.grid[x][y].maxX =  target.grid[x][y].minX + Settings.squareWidth/4;
             target.grid[x][y].minY = Settings.padding + (Settings.squareHeight/4 * y);
             target.grid[x][y].maxY = target.grid[x][y].minY + Settings.squareHeight/4;

             System.out.println("minX " + target.grid[x][y].minX);

          }
        }
     System.out.println("0-0  " + target.grid[0][0].minX);

 }

下面的输出显示当至少有一个缺失的正方形位于我的数组的0,0位置时的打印件。 0-0输出应该与我认为的第一个minX输出相同。 你可以看到minX值从600变为626(我没有代表发布图片)

输出

minX 600

minX 600

minX 600

minX 626

minX 626

minX 626

minX 652

minX 652

minX 652

0-0 626

方形等级

    import android.graphics.Color;
class Square
{
  public boolean on = false; //If the square is on or off. For black and white board true = black, white = false
  public int colorOn = Color.BLACK;
  public int colorOff = Color.WHITE;
  public int sideWidth=10;
  public int sideHeight =10;
  public int minX =0;
  public int maxX=10;
  public int minY=0;
  public int maxY=10;

  public Square(boolean isOn, int cOn, int cOff, int sW, int sH)
  {
    on =isOn;
    colorOn = cOn;
    colorOff = cOff;
    sideWidth = sW;
    sideHeight = sH;
  }

  public void changeState()
  {
   on = !on;
  }

}

网格类

    import java.util.ArrayList;
    import java.util.Random;
    import android.graphics.Color;
    class Grid
    {
      Square[][] grid; //Contains a list of  gridY 's so grid can be thought of as a sort of gridX
      int gridWidth;
      int gridHeight;
      public int minimumX =0;
      public int maximumX=20;
      public int minimumY=0;
      public int maximumY=20;
      private static Random rand = new Random();
      //private ArrayList <Square> gridY;

      public Grid(int width, int height)
      {
        gridWidth= width;
        gridHeight= height;
        grid = new Square[gridWidth][gridHeight];
        fillWithSquares();
      }
      private void fillWithSquares()
      {

        for(int x =0; x< gridWidth; x++)
        {
         // gridY = new ArrayList<Square>();
            for (int y =0; y < gridHeight; y++)
            {
              //gridY.add(new Square(Settings.squareStartState, Settings.colorSquareOn, Settings.colorSquareOff, Settings.squareWidth, Settings.squareHeight));
              Square sqr = new Square(Settings.squareStartState, Settings.colorSquareOn, Settings.colorSquareOff, Settings.squareWidth, Settings.squareHeight);
              grid[x][y] = sqr;
            }
         // grid.add(gridY);
        }

  }
  public void fillRandom()
  {
    for(int x =0; x< gridWidth; x++)
    {
        for (int y =0; y < gridWidth; y++)
        {
         // Square sqr = new Square(randBool(), Settings.colorSquareOn, Settings.colorSquareOff, Settings.squareWidth, Settings.squareHeight);
          //grid[x][y] = sqr;
          grid[x][y].on = randBool();
        }
    }
  }

  private boolean randBool() //returns a random boolean value
  {
    return rand.nextBoolean();
  }

}

额外输出 Output for a grid in with some missing blocks

不同丢失块的另一个输出仍然包括0,0 enter image description here

这是设置类。它来自PC版本,因为我暂时无法访问Android版本的文件。其中绝大多数是相同的。

    import java.awt.Color;
class Settings
{
  public static int WIDTH = 600;
  public static int HEIGHT =400;
  public static int gridWidth = 3;
  public static int gridHeight = 3;
  public static int templateWidth = 2;
  public static int templateHeight = 2;
  public static Color colorSquareOn = Color.BLUE;
  public static Color colorSquareOff = Color.GREEN;
  public static Boolean squareStartState = false;
  public static int padding = 10;
  public static int gameSideLength = (int) ((WIDTH * .6) -padding); // needs to be changed to addapt based on size
  public static int squareWidth = (int) (((WIDTH * .6) -padding) / gridWidth);
  public static int squareHeight = (int) (((HEIGHT * .6)-padding) / gridHeight);
  public static int overlayPerTemplate = 1;
  public static int numOfTemplates = 4;

}

3 个答案:

答案 0 :(得分:0)

尝试创建变量,而不是调用Settings.WIDTHSettings.padding等内容。

创建一个名为width = Settings.WIDTHpadding = Settings.paddingsquareWidth = Settings.squareWidthsquareHeight = Settings.squareHeight的变量。

然后循环就像这样:

for (int x=0; x<gameGrid.grid.length; x++) {
    for (int y= 0; y< gameGrid.grid.length; y++) {
        gameGrid.grid[x][y].minX =  (int)((width * .2) + padding + (squareWidth * x));
        gameGrid.grid[x][y].maxX = gameGrid.grid[x][y].minX + squareWidth;
        gameGrid.grid[x][y].minY = padding + (squareHeight * y);
        gameGrid.grid[x][y].maxY = gameGrid.grid[x][y].minY + squareHeight;

        target.grid[x][y].minX = (int)((width * .8) + padding + (squareWidth/4 * x));
        target.grid[x][y].maxX =  target.grid[x][y].minX + squareWidth/4;
        target.grid[x][y].minY = padding + (squareHeight/4 * y);
        target.grid[x][y].maxY = target.grid[x][y].minY + squareHeight/4;

        System.out.println("minX " + target.grid[x][y].minX);

    }
}

这样你可以确保在循环执行时变量不会改变。

我不知道Settings.WIDTHSettings.paddingSettings.squareWidthSettings.squareHeight是什么类型,这就是我没有编写整个函数的原因。试试看它是怎么回事。

答案 1 :(得分:0)

不确定这会有效,但请尝试

float tempMax = target.grid[x][y].minX + Settings.squareWidth/4.f;
target.grid[x][y].maxX = tempMax;

我认为过去制作游戏时我可能遇到类似的问题。

答案 2 :(得分:0)

好的,我解决了。显然在简单函数createTarget()中,我给每个方块的目标网格随机的真/假值。 (Square.on = true)我有这条线

target.grid[pos_x + x][pos_y + y] = templates[selectedTemplate].grid[x][y];
// It should have been this
target.grid[pos_x + x][pos_y + y].on = templates[selectedTemplate].grid[x][y].on;

它在某些时候起作用的原因是因为正方形本身被复制了。这似乎引起了参考问题。通过直接引用布尔变量,对象不会以任何方式链接。

向@Uwais大声喊出他帮助我消除了所有其他可能的原因,因此,最终,我必须查看代码中最不可能的部分。