drag object and collision event

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

Hi @android.masc,

I suggest that you re-think this somewhat. If you want to “replace” all 7 rectangles at some point, you don’t need to remove them entirely (destroy them). Instead, you should move them far off screen or make them invisible/inactive. Then, later, reposition the exact same 7 rectangles where needed.

Brent

If you are going to use only 7 physical items, like Brent said, don’t remove them. Use something like item.isVisible = false, and turn it to true whenever you need them to appear again. If you are going to make them invisible for a considerable ammount of time, you might want to remove the physical body (not the item) and add it again after you make it visible again.

https://docs.coronalabs.com/api/type/DisplayObject/isVisible.html

https://docs.coronalabs.com/api/library/physics/removeBody.html

If you are going to use removeBody right after the colision, you will need to do it some miliseconds after it just to make sure it works (like the documents say): timer.performWithDelay(10,function()physics.removeBody(item);end,1)