help needed
i have two squares moving in the direction that i swipe them but i need help on making them not go over each other.
here is my code and it was done in galaxy S3 view
[lua]
local whiteBox = display.newRect( 0,0,240, 240)
whiteBox:setReferencePoint( display.CenterReferencePoint )
whiteBox.x = 360; whiteBox.y = 640
local greenBox = display.newRect( 0,0,80, 80)
greenBox:setFillColor( 0, 180, 0)
greenBox:setReferencePoint( display.CenterReferencePoint )
greenBox.x = 280 greenBox.y = 560
local redBox = display.newRect( 0,0,80, 80)
redBox:setFillColor( 255, 0, 0)
redBox:setReferencePoint( display.CenterReferencePoint )
redBox.x = 360; redBox.y = 560
local swiped = false
–function for sliding greenBox
local function slideIt(e)
if e.target.id ~= “nil” then
local greenBox = e.target --Assign e.target as greenBox
if e.phase == “began” then – on began phase set focus and set up for swipe
local parent = greenBox.parent
parent:insert( greenBox )
display.getCurrentStage():setFocus( greenBox, e.id )
greenBox.isFocus = true
xa = e.x; ya = e.y
xPos = greenBox.x
end
if e.phase == “moved” then
xb = e.x; yb = e.y
local xDir = xb - xa – variables to be used to determine the swipe direction
local yDir = yb - ya
if xDir < 20 and xDir > -20 and yDir > 25 and swiped == false then
print(“down”)
if greenBox.y < 720 then – This checks to make sure the green square isn’t too low to transition down
swiped = true
yPos = greenBox.y + 80
transition.to( greenBox, {time = 500, y = yPos, onComplete = function() swiped = false end})
end
end
if yDir < 20 and yDir > -20 and xDir > 25 and swiped == false then
print(“right”)
if greenBox.x < 440 then – This checks to make sure the green square isn’t too far right to transition right
swiped = true
xPos = greenBox.x + 80
transition.to( greenBox, {time = 500, x = xPos, onComplete = function() swiped = false end})
end
end
if xDir < 20 and xDir > -20 and yDir < -25 and swiped == false then
print(“up”)
if greenBox.y > 560 then – This checks to make sure the green square isn’t too far up to transition up
swiped = true
yPos = greenBox.y - 80
transition.to( greenBox, {time = 500, y = yPos, onComplete = function() swiped = false end})
end
end
if yDir < 20 and yDir > -20 and xDir < -25 and swiped == false then
print(“left”)
if greenBox.x > 280 then – This checks to make sure the green square isn’t too far left to transition left
swiped = true
xPos = greenBox.x - 80
transition.to( greenBox, {time = 500, x = xPos, onComplete = function() swiped = false end})
end
end
end
if e.phase == “ended” then
display.getCurrentStage():setFocus( greenBox, nil ) – Release the touch focus on “ended” phase
greenBox.isFocus = false
end
return true
end
end
–function for sliding redBox
local function slideIt(e)
if e.target.id ~= “nil” then
local redBox = e.target --Assign e.target as redBox
if e.phase == “began” then – on began phase set focus and set up for swipe
local parent = redBox.parent
parent:insert( redBox )
display.getCurrentStage():setFocus( redBox, e.id )
redBox.isFocus = true
xa = e.x; ya = e.y
xPos = redBox.x
end
if e.phase == “moved” then
xb = e.x; yb = e.y
local xDir = xb - xa – variables to be used to determine the swipe direction
local yDir = yb - ya
if xDir < 20 and xDir > -20 and yDir > 25 and swiped == false then
print(“down”)
if redBox.y < 720 then – This checks to make sure the red square isn’t too low to transition down
swiped = true
yPos = redBox.y + 80
transition.to( redBox, {time = 500, y = yPos, onComplete = function() swiped = false end})
end
end
if yDir < 20 and yDir > -20 and xDir > 25 and swiped == false then
print(“right”)
if redBox.x < 440 then – This checks to make sure the red square isn’t too far right to transition right
swiped = true
xPos = redBox.x + 80
transition.to( redBox, {time = 500, x = xPos, onComplete = function() swiped = false end})
end
end
if xDir < 20 and xDir > -20 and yDir < -25 and swiped == false then
print(“up”)
if redBox.y > 560 then – This checks to make sure the red square isn’t too far up to transition up
swiped = true
yPos = redBox.y - 80
transition.to( redBox, {time = 500, y = yPos, onComplete = function() swiped = false end})
end
end
if yDir < 20 and yDir > -20 and xDir < -25 and swiped == false then
print(“left”)
if redBox.x > 280 then – This checks to make sure the red square isn’t too far left to transition left
swiped = true
xPos = redBox.x - 80
transition.to( redBox, {time = 500, x = xPos, onComplete = function() swiped = false end})
end
end
end
if e.phase == “ended” then
display.getCurrentStage():setFocus( redBox, nil ) – Release the touch focus on “ended” phase
redBox.isFocus = false
end
return true
end
end
greenBox:addEventListener( “touch”, slideIt)
redBox:addEventListener( “touch”, slideIt)
[lua] [import]uid: 218976 topic_id: 35570 reply_id: 335570[/import]