Hi Brent,
my problem is this:
after the rectangles have collided with the relative “baskets” and are removed,
I wish they all reappear,
the simplest solution to understand when all rectangles are all removed
It is a score = 7,
since each rectangle collides with the right basket,
the rectangle is removed and there is a point in the score,
I tried using the function rectanglesNew
but it does not work,
my code is this
display.setStatusBar( display.HiddenStatusBar ) local centerX = display.contentCenterX local centerY = display.contentCenterY local \_W = display.contentWidth local \_H = display.contentHeight local bkg = display.newImage ("prato3.png", centerX, centerY,true) score = 0 local function rettanglesNew() rectangles = { { x=100, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=30, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=170, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=240, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=310, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=380, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=450, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, } end local scoreNumber = display.newText(score, display.contentCenterX + 60, 20, native.systemFontBold, 20) scoreNumber:setFillColor( 1, 0, 0 ) local function updateScore() score = score + 1 scoreNumber.text = score end local rectangles = { { x=100, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=30, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=170, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=240, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=310, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=380, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=450, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, } local baskets = {} -- table to hold a list of "baskets" -- Check if a basket is colliding with a rectangle of the same color, and if it does, remove the rectangle local function checkCollision(event) if event.target then local rect = event.target for i = 1, #baskets do local basket = baskets[i] local basketColor = basket.fillColor local rectColor = rect.fillColor local sameColor = basketColor[1] == rectColor[1] and basketColor[2] == rectColor[2] and basketColor[3] == rectColor[3] local distance = math.sqrt((basket.x - rect.x)^2 + (basket.y - rect.y)^2) if distance \< 20 and sameColor then display.remove(rect) updateScore() if score == 7 then rettanglesNew() end end end end end local function onTouch( event ) local t = event.target -- Do our collision checking checkCollision(event) local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true -- Store initial position t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then -- Make object move (we subtract t.x0,t.y0 so that moves are -- relative to initial grab point, rather than object "snapping"). t.x = event.x - t.x0 t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end -- Important to return true. This tells the system that the event -- should not be propagated to listeners of any objects underneath. return true end -- Iterate through rectangles array and create rounded rects (vector objects) for each item for \_,item in ipairs( rectangles ) do local button = display.newCircle(item.x, item.y, item.r ) button.fillColor = {item.red, item.green, item.blue} button:setFillColor(unpack(button.fillColor)) button.strokeWidth = 3 button:setStrokeColor(0) -- Make the button instance respond to touch events button:addEventListener( "touch", onTouch ) end -- Add "baskets" (just a single red circle "basket" for now) local basket = display.newCircle(100, 112, 14) basket.fillColor = {1, 0, 0} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(300, 112, 14) basket.fillColor = {1, 1, 1} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(200, 112, 14) basket.fillColor = {1, 1, 0} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(400, 112, 14) basket.fillColor = {1, 0, 1} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket)
how I can fix this?
thanks