-1

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.

1 Answers1

1

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.

Fermin Silva
  • 3,331
  • 2
  • 17
  • 21
  • This looks very good and was my question, although what do ido to this block.setY(three_quarters - 10); block2.setY(three_quarters - 10); block3.setY(three_quarters - 10); i dont mean about random the y axis but i mean the assignment block, block2, block 3?? – Charlton Santana Apr 09 '12 at 22:21
  • @CharltonSantana rather than having 3 individual variables for blocks, have a list with all them. Why 3? later on if you want 4, you need to add another variable, initialize it, etc. Using a list is way more dynamic. For the setY part, i'm editing the answer. – Fermin Silva Apr 09 '12 at 22:41
  • i dont know how to initalise it ... are you able to tell me.. i also have a collsion detection which is annoying because if i want 100 block i have to copy and past then edit 100 times(like you said) here is part of the collision for block 2 .. // bottom left touching block? if (sprite.bottomlx < block.bottomrx && sprite.bottomlx > block.bottomlx && sprite.bottomly < block.bottommy && sprite.bottomly > block.topry ){ Log.d(TAG, "Collided!!!!!!!!!!!!1"); } – Charlton Santana Apr 09 '12 at 22:47
  • Here to add more blocks i would have to change eg block.topry to block2.topry – Charlton Santana Apr 09 '12 at 22:49
  • @CharltonSantana initialize what? If you want 100 blocks random, then have a list with all them, why 100 hardcoded lines? For collision just use the same list and work with that. Maybe you should delve a little more on programming before taking such a difficult challenge as game programming is (no offense) – Fermin Silva Apr 09 '12 at 23:40
  • sorry i ment different instanses of an object not initilize lol .. and im not having 100 lines of code anymore... http://stackoverflow.com/questions/10091089/generating-bimaps-with-the-instance-title-different-everytime – Charlton Santana Apr 10 '12 at 14:57