为什么我的阵列保持呈现相同数量的每个变量? [英] Why does my array keep rendering the same number for each variable?

查看:145
本文介绍了为什么我的阵列保持呈现相同数量的每个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个for循环是循环通过矩形阵列,因为它们出现。对于被添加到阵列的每个新的矩形子图形绘制在它的上面。我想绘制矩形的顶部有一个改变子图形绘制其自己的变量每个精灵。而屏幕上的每一个主要的矩形应该有不同的精灵绘制它----------------------------------- -I找不出如何,得到与出赋予同一样的一到所有其他精灵的各精灵其自己的变量。这code最终绘制新的精灵一样每个矩形上。以下是我迄今为止..

  INT arrayplace = 0;

 对于(矩形雨:rectangleArray){

    numberArray.add(arrayPlace,MathUtils.random(3);
    //添加变量在一定点阵列上的
    //选取范围内随机#给出
    arrayplace ++;

          如果(numberArray.get(arrayPlace)== 1){
              spritebatch.draw(spriteOne,rain.x,rain.y);
          };
          如果(numberArray.get(arrayPlace)== 2){
              spritebatch.draw(spriteTwo,rain.x,rain.y);
          }
          如果(numberArray.get(arrayPlace)== 3){
              spritebatch.draw(spriteThree,rain.x,rain.y);
          }
  }
 

解决方案

该错误是:

  arrayplace ++;
 

您递增 arrayplace 太早,所以它指向你背后附加值的索引。

I have a for loop that is looping through an array of rectangles as they appear. For each new rectangle that is added to the array a sprite is drawn on top of it. I want each sprite drawn on top of the rectangles to have its own variable that changes the sprite drawing. While on the screen mostly every rectangle should have a different sprite drawn on it ------------------------------------I cant figure out how to give each sprite its own variable with out giving the same one to all of the other sprites. This code ends up drawing the new same sprite on every rectangle. Here is what i have so far..

  int arrayplace = 0;

 for(Rectangle  rain: rectangleArray) { 

    numberArray.add(arrayPlace, MathUtils.random(3);
    //adds a variable to a certain spot on the array
    // picks random# within range given
    arrayplace++;           

          if (numberArray.get(arrayPlace)==1){
              spritebatch.draw(spriteOne, rain.x, rain.y);
          };
          if (numberArray.get(arrayPlace)==2){
              spritebatch.draw(spriteTwo, rain.x, rain.y);
          }
          if (numberArray.get(arrayPlace)==3){
              spritebatch.draw(spriteThree, rain.x, rain.y);
          }
  }

解决方案

The mistake is:

arrayplace++;  

You increment arrayplace too early, so it points to an index behind the value you added.

这篇关于为什么我的阵列保持呈现相同数量的每个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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