The Daily Click ::. Forums ::. Klik Coding Help ::. Create a grid of random different sized objects
 

Post Reply  Post Oekaki 
 

Posted By Message

vetmora120



Registered
  07/01/2010
Points
  273
1st April, 2015 at 01/04/2015 22:33:53 -

Hello,

I've been trying for a while to set up a loop that generates a grid of blocks at the start of the frame. This is easy, however I'd like each block to be random out of a pool of X amount of types.
See example below.

https://dl.dropboxusercontent.com/u/9919361/GRID.jpg

I'm also trying to make it dynamic, so it will be easy to add new block types in the future.

I know there would be a way to do this, I'm just trying to figure out the best way.

Any ideas?

Thanks in advance.

 
n/a

UrbanMonk

BRING BACK MITCH

Registered
  07/07/2008
Points
  49566

Has Donated, Thank You!Little Pirate!ARGH SignKliktober Special Award TagPicture Me This Round 33 Winner!The Outlaw!VIP MemberHasslevania 2!I am an April FoolKitty
Picture Me This Round 32 Winner!Picture Me This Round 42 Winner!Picture Me This Round 44 Winner!Picture Me This Round 53 Winner!
2nd April, 2015 at 02/04/2015 15:52:31 -

I would have all the block images contained in one object. If you don't need animations for the blocks you could use the animated frames for this. That will allow you to add as many blocks as you like in the future.

Next create another object the size of a single grid space and have that scan through your playfield using a loop until it finds an empty space. Then you could use a 2nd loop to cycle through all the blocks at random until it finds one that fits that space. Rinse and repeat.

This is the easiest way I could think of, but there is likely a more efficient way to go about it. Collision detection is expensive, so you might be able to speed it up by using an array and then storing different block sizes in another array and check for overlap manually.

Good luck!

 
n/a

vetmora120



Registered
  07/01/2010
Points
  273
6th April, 2015 at 06/04/2015 23:55:05 -

Thanks for the reply UrbanMonk. Sort of the path I was going down so good to hear I wasn't far off.

I've linked below what I have so far and for some reason it doesn't seem to be working as expected. Are you able to take a look when you get a chance? Would be much appreciated, thanks!

https://dl.dropboxusercontent.com/u/9919361/Blocks.mfa

 
n/a

UrbanMonk

BRING BACK MITCH

Registered
  07/07/2008
Points
  49566

Has Donated, Thank You!Little Pirate!ARGH SignKliktober Special Award TagPicture Me This Round 33 Winner!The Outlaw!VIP MemberHasslevania 2!I am an April FoolKitty
Picture Me This Round 32 Winner!Picture Me This Round 42 Winner!Picture Me This Round 44 Winner!Picture Me This Round 53 Winner!
7th April, 2015 at 07/04/2015 18:23:19 -

I gave it a shot, but it appears as if changing the animation of an object in a loop doesn't change it's collision mask. Either that or some other related problem.

So it seems this sorta thing is more difficult to do than I expected. I could prolly pull it off using an array and a list of block sizes, but it would take me way more time than I would want to spend on this.

 
n/a

vetmora120



Registered
  07/01/2010
Points
  273
12th April, 2015 at 12/04/2015 23:40:24 -

Hi UrbanMonk,

Thanks for all your help.

I managed to get it working by creating a loop for each block size type, that starts from the largest and checks the values in an array at the random position before creating the block. Then it runs a loop at the end for the single grid size blocks that iterates through and basically fills the gaps.

Not as clean or as manipulative as I would have liked but it does work well!

 
n/a

UrbanMonk

BRING BACK MITCH

Registered
  07/07/2008
Points
  49566

Has Donated, Thank You!Little Pirate!ARGH SignKliktober Special Award TagPicture Me This Round 33 Winner!The Outlaw!VIP MemberHasslevania 2!I am an April FoolKitty
Picture Me This Round 32 Winner!Picture Me This Round 42 Winner!Picture Me This Round 44 Winner!Picture Me This Round 53 Winner!
14th April, 2015 at 14/04/2015 03:10:48 -

Ok nice! Points for thinking outside the... box

 
n/a
   

Post Reply



 



Advertisement

Worth A Click