Hi,
Here is a problem I didn’t success to resolve…
If you try the code below ( please do it
), you can see that :
- You can drag any square on the x axis
- If you drag a square A SLOWLY and it’s close enough from another square B, the B square swap its position.
- I insist : it works well if drag movement is SLOW
- But if your drag movement is fast, you will see that squares positions become completely incoherent (squares superposition for instance)
WHAT I’D LIKE TO DO:
Drag movement can be as faster as I want, squares positions remain coherents (no squares superpositions for instance)
If someone has an idea, it will be nice!
Thanks for your help :))
CODE
main.lua file
local squareCreator = require("Square")
display.setStatusBar( display.HiddenStatusBar )
--global tab to store the 5 squares object
squareTab = {}
-- Creation of the 5 squares
local square5 = squareCreator.new{ x = 330, y = display.contentHeight/2, red = 255, green = 255 , blue = 0}
local square4 = squareCreator.new{ x = 260, y = display.contentHeight/2, red = 255, green = 0 , blue = 255}
local square3 = squareCreator.new{ x = 190, y = display.contentHeight/2, red = 0, green = 0 , blue = 255}
local square2 = squareCreator.new{ x = 120, y = display.contentHeight/2, red = 0, green = 255 , blue = 0}
local square1 = squareCreator.new{ x = 50, y = display.contentHeight/2, red = 255, green = 0 , blue = 0}
--We insert the 5 squares on the table we've just created above
--We will loop on this table during the drag movement to check if another square is close enough
table.insert( squareTab, square1 )
table.insert( squareTab, square2 )
table.insert( squareTab, square3 )
table.insert( squareTab, square4 )
table.insert( squareTab, square5 )
------------------------------------------------------------------------------------------
-- FUNCTION checkProximity
-- params : square, touchMovement
-- This function is called when we drag a square.
-- It checks if another square is close enough from the current drag square
-- If another square is clos enough, we move this square
-- Variable TouchMovement inform us from where comes the drag movement (left or right)
------------------------------------------------------------------------------------------
function checkProximity( square, touchMovement )
local xSquare = square.x
-- we loop on squareTab
for i = 1, #squareTab do
--We are not interested by the draggable square
if squareTab[i] ~= square then
local currentSquare = squareTab[i]
local xCurrentSquare = squareTab[i].x
-- we calculate the distance between our drag square and the current square on the table
local xDelta = math.abs( xCurrentSquare - xSquare )
-- if this distance is small enough...
if ( xDelta \< 10 ) then
local xDestination
-- ...we calculate the xDestination according to the touchMovement variable
if( touchMovement == "left" ) then
xDestination = currentSquare.x + currentSquare.width
elseif( touchMovement == "right" ) then
xDestination = currentSquare.x - currentSquare.width
end
-- finally, we move the current square to the xDestination we've just calculated above
currentSquare.x = xDestination
-- and we leave the loop
break
end
end
end
end
Square.lua file
module(..., package.seeall)
function new( params )
local square = display.newRect( params.x, params.y, 70, 70 )
square:setFillColor(params.red, params.green, params.blue)
square:setReferencePoint(display.TopLeftReferencePoint)
------------------------------------------------------------------------------------------
-- FUNCTION square:touch
------------------------------------------------------------------------------------------
-- touch listener function
function square:touch(event)
if event.phase == "began" then
-- begin focus
display.getCurrentStage():setFocus( square, event.id )
square.isFocus = true
square.markX = square.x -- store x location of square
square.markY = square.y -- store y location of square
-- This variable is used to know from where the drag movement comes (right or left), in correlation with xTouchCurrent
xTouchPrevious = square.x
elseif square.isFocus then
if event.phase == "moved" then
-- drag touch object
local x = (event.x - event.xStart) + square.markX
local y = (event.y - event.yStart) + square.markY
-- move object based on calculations above (only on x axe)
square.x = x
-- This variable is used to know from where the drag movement comes (right or left), in correlation with xTouchPrevious
xTouchCurrent = x
-- Below, we determine from where the drag movement comes (right or left)
if(xTouchCurrent - xTouchPrevious \> 0 ) then
touchMovement = "right"
elseif(xTouchCurrent - xTouchPrevious \< 0 ) then
touchMovement = "left"
elseif(xTouchCurrent - xTouchPrevious == 0 ) then
touchMovement = "left"
end
xTouchPrevious = x
-- we call the global checkProximity function (look in main.lua the function definition)
checkProximity( square, touchMovement )
elseif event.phase == "ended" or event.phase == "cancelled" then
-- end focus
display.getCurrentStage():setFocus( square, nil )
square.isFocus = false
end
end
return true
end
square:addEventListener( "touch", square )
return square
end
build.settings file
settings =
{
orientation =
{
default = "landscapeRight",
supported =
{
"landscapeLeft", "landscapeRight"
},
},
iphone =
{
plist =
{
CFBundleIconFile = "Icon.png",
CFBundleIconFiles = {
"Icon.png",
"Icon@2x.png",
"Icon-72.png",
},
},
}
}
[import]uid: 160159 topic_id: 30085 reply_id: 330085[/import]