考虑到每个块都需要比前一个块更远,
List<Block> blocks = new LinkedList<Block>();
Random rnd = new Random(System.currentTimeMillis());
int x = 400;
while (youNeedMoreBlocks)
{
int offset = rnd.nextInt(400) + 100; //500 is the maximum offset, this is a constant
x += offset; //ofset will be between 100 and 400
blocks.add(new Block(R.drawable.block, x, platformheight));
//if you have enough blocks, set youNeedMoreBlocks to false
}
但这在我看来过于简单。要么我不明白你的问题,要么实际上就是这么简单。
编辑:
对于这样的任务:
block.setY(three_quarters - 10);
block2.setY(three_quarters - 10);
block3.setY(three_quarters - 10);
您需要修改循环:
List<Block> blocks = new LinkedList<Block>();
Random rnd = new Random(System.currentTimeMillis());
int x = 400;
while (youNeedMoreBlocks)
{
int offset = rnd.nextInt(400) + 100; //500 is the maximum offset, this is a constant
x += offset; //ofset will be between 100 and 400
Block tmp = new Block(R.drawable.block, x, platformheight);
tmp.setY(three_quarters - 10);
//do with tmp everything you need to apply to each block
blocks.add(tmp);
//if you have enough blocks, set youNeedMoreBlocks to false
}
另一个明智的想法是在玩家靠近地图边缘时按需生成块,这样你的加载时间就会更快。