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.