将值保存到对象,直到满足某个条件

时间:2014-06-02 13:19:08

标签: java android eclipse canvas

我正在开发一款与游戏相似的应用程序。我正在使用Java为Android做这个。到目前为止,我已经完成了游戏的基本机制,但是我在将随机生成的颜色设置为屏幕上弹出的对象时遇到了麻烦。每一帧,我都使用Canvas绘制一个新的矩形。问题是它每次都给它一种新的颜色,因此它可以在颜色之间快速切换。

    // Some important variables
private RectF rowBlock; // The blocks that the player has to avoid
private int rowSpace = 100; // The space between rows
private RectF rowGap; // Places characters can go through
public double blockY[] = new double[50];
public double gapX[] = new double[50];

    // Constructor
public PlayScreen(Context context) {
    super(context);
        int nor = screenHeight / rowSpace;

    for (int row = 0; row < nor + 1; row ++) {
        Random randomNum = new Random();
        final int gap = randomNum.nextInt(550) + 20;

        blockY[row] = 0 + row * rowSpace;
        gapX[row] = gap;

    }

}

    //Draw Method

@Override protected void onDraw(Canvas canvas) {

       int nor = screenHeight / rowSpace;
   for (int row = 0; row < nor + 1; row ++) {               
        blockY[row] = blockY[row] + rowSpeed;
        gapX[row] = gapX[row];

        rowBlock.set(0, (float) blockY[row], screenWidth, (float) blockY[row] + 20);
        rowGap.set((float) gapX[row], (float)blockY[row], (float) gapX[row] + 50, (float) blockY[row] + 20);

        final int color = random.nextInt(5);

        if (color == 1) {
            canvas.drawRect(rowBlock, colorRed);                
        }
        if (color == 2) {
            canvas.drawRect(rowBlock, colorBlue);               
        }
        if (color == 3) {
            canvas.drawRect(rowBlock, colorWhite);              
        }
        if (color == 4) {
            canvas.drawRect(rowBlock, colorYellow);             
        }     
        if (color == 5) {
            canvas.drawRect(rowBlock, colorGreen);
        }   
        canvas.drawRect(rowGap, colorBlack);


        if (blockY[row] < 0) {
            Random randomNum = new Random();
            final int gap = randomNum.nextInt(screenWidth - 50) + 10; // between 10 and screenWidth - 50(assures gaps on screen)

            blockY[row] = screenHeight;
            gapX[row] = gap;

        }


    }

每次绘制时都会为对象绘制一个新的随机颜色。是否有任何方法可以将颜色的值“保存”到对象,直到满足某个条件(它不在视图中并生成一个新的)

如果有人能帮我一把,我真的很感激!谢谢你们!

这应该是什么样子

What it should look like.

0 个答案:

没有答案