I am currently hard coding 10 different instances like the code below, but but I'd like to create many more. Instead of having the same layout for the new level, I was wondering if there is anyway to generate a random X value for each block (this will be how far into the level it is). A level 100,000 pixels wide would be good enough but if anyone knows a system to make the level go on and on, I'd like to know that too. This is basically how I define a block now (with irrelevant code removed):
block = new Block(R.drawable.block, 400, platformheight);
block2 = new Block(R.drawable.block, 600, platformheight);
block3 = new Block(R.drawable.block, 750, platformheight);
The 400
is the X position, which I'd like to place randomly through the level, the platformheight
variable defines the Y position which I don't want to change.
Considering that each block needs to be further than the previous one,
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
}
But this looks overly simplistic to me. Either i didn't understand your question or it actually was that simple.
Edit:
For assignments like these:
block.setY(three_quarters - 10);
block2.setY(three_quarters - 10);
block3.setY(three_quarters - 10);
You need to modify the loop with:
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
}
Another wise idea would be to generate the blocks on demand when the player is close to the edge of the map, so you have faster loading times.