Extremely Hard Array Question Need a hand!

To the people of corona,

I am creating a game where icecubes are randomly generated around the screen on an X,Y axis. 

X coords are in a table

Y coords are in a table.

How do i loop the spawning of icecubes so that they are constantly randomly spawning and do not spawn on top of one another. And How do I make it so when I tap the ice cube it changes image?

Many Thanks

James Mallon

I’m not sure there’s any way to avoid overlapping except to test a proposed (x1,y1)-(x2,y2) against all previous boxes.  Borrowing from http://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other … something like this might work:

local flag = true while( flag ) do -- propose coords local x1 = math.random(1,maxX) local y1 = math.random(1,maxY) local x2 = x1 + math.random(1,maxWidth) local y2 = y1 + math.random(1,maxHeight) -- does it overlap any existing box? local overlap = false for i=1,#currentBoxes do local box = currentBoxes[i] if( (x1 \< box.x2) and (x2 \> box.x1) and (y1 \< box.y2) and (y2 \> box.y1) ) then -- OVERLAP! overlap = true break end end if( overlap == false ) then -- no overlap .. we found a good location -- TODO: add (x1,y1)-(x2,y2) to the currentBoxes list flag = false end end

Re: the tap can be handled by addEventListener.  I’m not sure in the new graphics 2.0, you might be able to over-write the image directly by setting a new fill-image on the icecube.  In the older graphics engine, you’d have to delete the old image and create a new one; or create them both up-front, then set each one’s isVisible property to true/false accordingly.

Hey there,

I really did not understand this, is there anyway you could explain this further? I’m sorry im terrible at interpreting things.

There are 20 spaces in my grid for my icecubes. with specific X and Y coords. How would I compare the variable xposit and yposit of my icecube against all other 19 cube posits so that I can put my cube in there?

desperately in need of help. 

James x

what I would do is

  1. shuffle tables
  2. then just choose values in order,1-…
  3. when I restart or change levels start over at step 1

you shouldn’t have any repeats

Ahh … I didn’t really read the question right.  

jstrahan’s solution is what you want – you’re picking from 1-of-N positions, not just any (x,y) coord on the screen.

Can’t Believe i didnt think of this! thank you so much!

I’m not sure there’s any way to avoid overlapping except to test a proposed (x1,y1)-(x2,y2) against all previous boxes.  Borrowing from http://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other … something like this might work:

local flag = true while( flag ) do -- propose coords local x1 = math.random(1,maxX) local y1 = math.random(1,maxY) local x2 = x1 + math.random(1,maxWidth) local y2 = y1 + math.random(1,maxHeight) -- does it overlap any existing box? local overlap = false for i=1,#currentBoxes do local box = currentBoxes[i] if( (x1 \< box.x2) and (x2 \> box.x1) and (y1 \< box.y2) and (y2 \> box.y1) ) then -- OVERLAP! overlap = true break end end if( overlap == false ) then -- no overlap .. we found a good location -- TODO: add (x1,y1)-(x2,y2) to the currentBoxes list flag = false end end

Re: the tap can be handled by addEventListener.  I’m not sure in the new graphics 2.0, you might be able to over-write the image directly by setting a new fill-image on the icecube.  In the older graphics engine, you’d have to delete the old image and create a new one; or create them both up-front, then set each one’s isVisible property to true/false accordingly.

Hey there,

I really did not understand this, is there anyway you could explain this further? I’m sorry im terrible at interpreting things.

There are 20 spaces in my grid for my icecubes. with specific X and Y coords. How would I compare the variable xposit and yposit of my icecube against all other 19 cube posits so that I can put my cube in there?

desperately in need of help. 

James x

what I would do is

  1. shuffle tables
  2. then just choose values in order,1-…
  3. when I restart or change levels start over at step 1

you shouldn’t have any repeats

Ahh … I didn’t really read the question right.  

jstrahan’s solution is what you want – you’re picking from 1-of-N positions, not just any (x,y) coord on the screen.

Can’t Believe i didnt think of this! thank you so much!