Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a good container to drag and drop objects in (corona SDK)?
    primarykey
    data
    text
    <p>I'm new to Corona SDK.</p> <p>I'm implementing a test using drag-and-drop for an application.</p> <p>I implemented the code bellow for a container that "attract" the dragging object that is near it.</p> <pre><code>display.setStatusBar( display.HiddenStatusBar ) local posX, posY = 100, 200 local sizeX, sizeY = 150, 100 local container = display.newRect( posX, posY, sizeX, sizeY ) container:setFillColor( 255,0,0 ) container.strokeWidth = 3 container:setStrokeColor(100, 100, 100) local myObject = display.newRect( 0, 0, sizeX, sizeY ) myObject.alpha = 0.6 myObject:setFillColor( 0,0,255 ) -- dcenter, d1, d2, d3, d4 are test points: IF THERE ARE AT LEAST 2 POINTS INSIDE THE TARGET CONTAINER, THAN THE OBJECT WILL BE ATRACTED INTO THE CONTAINER -- uncomment the code bellow to see the test points -- local dcenter = display.newCircle( (sizeX/2) - 1, (sizeY/2) - 1, 2 ) -- dcenter:setFillColor(120,120,120) -- -- local d1 = display.newCircle( (sizeX/2) - (((1/3) * sizeX)/2) - 1, (sizeY/2) - (((1/3) * sizeY)/2) - 1, 2 ) -- d1:setFillColor(120,120,120) -- -- local d2 = display.newCircle( (sizeX/2) - (((1/3) * sizeX)/2) - 1, (sizeY/2) + (((1/3) * sizeY)/2) - 1, 2 ) -- d2:setFillColor(120,120,120) -- -- local d3 = display.newCircle( (sizeX/2) + (((1/3) * sizeX)/2) - 1, (sizeY/2) - (((1/3) * sizeY)/2) - 1, 2 ) -- d3:setFillColor(120,120,120) -- -- local d4 = display.newCircle( (sizeX/2) + (((1/3) * sizeX)/2) - 1, (sizeY/2) + (((1/3) * sizeY)/2) - 1, 2 ) -- d4:setFillColor(120,120,120) -- touch listener function function myObject:touch( event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x, self.y = x, y -- move object based on calculations above -- uncomment the code bellow to see the test points (following myObject) -- dcenter.x, dcenter.y = x, y -- d1.x, d1.y = x - (((1/3) * sizeX)/2), y - (((1/3) * sizeY)/2) -- d2.x, d2.y = x - (((1/3) * sizeX)/2), y + (((1/3) * sizeY)/2) -- d3.x, d3.y = x + (((1/3) * sizeX)/2), y - (((1/3) * sizeY)/2) -- d4.x, d4.y = x + (((1/3) * sizeX)/2), y + (((1/3) * sizeY)/2) if (((x &gt;= ((sizeX/2) + posX - ((2/3) * sizeX))) and (y &gt;= ((sizeY/2) + posY - ((1/3) * sizeY))) and (x &lt;= ((sizeX/2) + posX + ((2/3) * sizeX))) and (y &lt;= ((sizeY/2) + posY + ((1/3) * sizeY)))) or ((x &gt;= ((sizeX/2) + posX - ((1/3) * sizeX))) and (y &gt;= ((sizeY/2) + posY - ((2/3) * sizeY))) and (x &lt;= ((sizeX/2) + posX + ((1/3) * sizeX))) and (y &lt;= ((sizeY/2) + posY + ((2/3) * sizeY)))) or ((x &gt;= ((sizeX/2) + posX - ((1/2) * sizeX))) and (y &gt;= ((sizeY/2) + posY - ((1/2) * sizeY))) and (x &lt;= ((sizeX/2) + posX + ((1/2) * sizeX))) and (y &lt;= ((sizeY/2) + posY + ((1/2) * sizeY))))) then container:setFillColor( 100,0,0 ) else container:setFillColor( 255,0,0 ) end elseif event.phase == "ended" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY -- main condition: I calculated 3 areas to atract the object to the target container, 2 areas that atract it when it's 1/3 in the target and 1 area that atract it when it's 1/4 in the target if (((x &gt;= ((sizeX/2) + posX - ((2/3) * sizeX))) and (y &gt;= ((sizeY/2) + posY - ((1/3) * sizeY))) and (x &lt;= ((sizeX/2) + posX + ((2/3) * sizeX))) and (y &lt;= ((sizeY/2) + posY + ((1/3) * sizeY)))) or ((x &gt;= ((sizeX/2) + posX - ((1/3) * sizeX))) and (y &gt;= ((sizeY/2) + posY - ((2/3) * sizeY))) and (x &lt;= ((sizeX/2) + posX + ((1/3) * sizeX))) and (y &lt;= ((sizeY/2) + posY + ((2/3) * sizeY)))) or ((x &gt;= ((sizeX/2) + posX - ((1/2) * sizeX))) and (y &gt;= ((sizeY/2) + posY - ((1/2) * sizeY))) and (x &lt;= ((sizeX/2) + posX + ((1/2) * sizeX))) and (y &lt;= ((sizeY/2) + posY + ((1/2) * sizeY))))) then self.x, self.y = posX + (sizeX/2), posY + (sizeY/2); -- uncomment the code bellow to see the test points (following myObject) -- dcenter.x, dcenter.y = posX + (sizeX/2), posY + (sizeY/2) -- d1.x, d1.y = posX - (((1/3) * sizeX)/2) + (sizeX/2), posY - (((1/3) * sizeY)/2) + (sizeY/2) -- d2.x, d2.y = posX - (((1/3) * sizeX)/2) + (sizeX/2), posY + (((1/3) * sizeY)/2) + (sizeY/2) -- d3.x, d3.y = posX + (((1/3) * sizeX)/2) + (sizeX/2), posY - (((1/3) * sizeY)/2) + (sizeY/2) -- d4.x, d4.y = posX + (((1/3) * sizeX)/2) + (sizeX/2), posY + (((1/3) * sizeY)/2) + (sizeY/2) end end return true end myObject:addEventListener( "touch", myObject ) </code></pre> <p>I just want to know if there is some api for it already.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload