I need help on wring a code for swiping on an object and making it move in the direction that I swiped a certain amount of pixels, tell me if its possible to do.
Thanks [import]uid: 218976 topic_id: 35364 reply_id: 335364[/import]
It is definitely possible. Take a look at some of the code samples here:
http://springboardpillow.blogspot.co.uk/2012/04/sample-code.html
If that doesn’t help I can write a more detailed explanation if you can provide some more info. [import]uid: 8271 topic_id: 35364 reply_id: 140550[/import]
Okay. I have a physical box with inside dimensions of 100x100 pixels and inside I have a cube of 50x50 pixels that is sitting on the top left of the box. I want it to move to top right when I swipe to the right or when I swipe down I want it to move down but I dont want to slide pass the box walls even if the cube is at the bottom and I swibe to the bottom again. Basically I wantto to move in the horizontal or vertical direction of the swipe but not go through other physical objects [import]uid: 218976 topic_id: 35364 reply_id: 140552[/import]
Sorry about it looking messy, something to do with copy/paste from textWrangler I think. It’s nice and neat in my window. [import]uid: 40731 topic_id: 35364 reply_id: 140575[/import]
Something like this?
local W = display.contentWidth
local H = display.contentHeight
local whiteBox = display.newRect( 0,0,220, 220)
whiteBox:setReferencePoint( display.CenterReferencePoint )
whiteBox.x = W/2; whiteBox.y = H/2
local greenBox = display.newRect( 0,0,100, 100)
greenBox:setFillColor( 0, 180, 0)
greenBox:setReferencePoint( display.CenterReferencePoint )
greenBox.x = (W/2-50); greenBox.y = (H/2-50)
local swiped = false
--function for sliding squares
local function slideIt(e)
if e.target.id ~= "nil" then
local greenSquare = e.target --Assign e.target as greenSquare
if e.phase == "began" then -- on began phase set focus and set up for swipe
local parent = greenSquare.parent
parent:insert( greenSquare )
display.getCurrentStage():setFocus( greenSquare, e.id )
greenSquare.isFocus = true
xa = e.x; ya = e.y
xPos = greenSquare.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 greenSquare.y \< 290 then -- This checks to make sure the green square isn't too low to transition down
swiped = true
yPos = greenSquare.y + 100
transition.to( greenSquare, {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 greenSquare.x \< 210 then -- This checks to make sure the green square isn't too far right to transition right
swiped = true
xPos = greenSquare.x + 100
transition.to( greenSquare, {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 greenSquare.y \> 190 then -- This checks to make sure the green square isn't too far up to transition up
swiped = true
yPos = greenSquare.y - 100
transition.to( greenSquare, {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 greenSquare.x \> 110 then -- This checks to make sure the green square isn't too far left to transition left
swiped = true
xPos = greenSquare.x - 100
transition.to( greenSquare, {time = 500, x = xPos, onComplete = function() swiped = false end})
end
end
end
if e.phase == "ended" then
display.getCurrentStage():setFocus( greenSquare, nil ) -- Release the touch focus on "ended" phase
greenSquare.isFocus = false
end
return true
end
end
greenBox:addEventListener( "touch", slideIt)
*View as iPhone. [import]uid: 40731 topic_id: 35364 reply_id: 140574[/import]
Take a look at the mathlib I posted (my link above) and try using the lengthOf() and maybe angleBetweenPoints() functions. In the ended phase of a touch event you would want to test the length of the xStart,yStart and x,y locations. Using the angle between them would tell you the direction and you could even check the time spent making the swipe.
You’ve done pretty well above, so let me know if this gets you anywhere or I can write it for you later. Have a stab at it… [import]uid: 8271 topic_id: 35364 reply_id: 140588[/import]
thanks brian nexus that helped alot, exactly what i was looking for, now i need that to work with more then one green boxes in the same white box and not to go over each other.
want to help with that?
thanks [import]uid: 218976 topic_id: 35364 reply_id: 140597[/import]
It is definitely possible. Take a look at some of the code samples here:
http://springboardpillow.blogspot.co.uk/2012/04/sample-code.html
If that doesn’t help I can write a more detailed explanation if you can provide some more info. [import]uid: 8271 topic_id: 35364 reply_id: 140550[/import]
Okay. I have a physical box with inside dimensions of 100x100 pixels and inside I have a cube of 50x50 pixels that is sitting on the top left of the box. I want it to move to top right when I swipe to the right or when I swipe down I want it to move down but I dont want to slide pass the box walls even if the cube is at the bottom and I swibe to the bottom again. Basically I wantto to move in the horizontal or vertical direction of the swipe but not go through other physical objects [import]uid: 218976 topic_id: 35364 reply_id: 140552[/import]
Sorry about it looking messy, something to do with copy/paste from textWrangler I think. It’s nice and neat in my window. [import]uid: 40731 topic_id: 35364 reply_id: 140575[/import]
Something like this?
local W = display.contentWidth
local H = display.contentHeight
local whiteBox = display.newRect( 0,0,220, 220)
whiteBox:setReferencePoint( display.CenterReferencePoint )
whiteBox.x = W/2; whiteBox.y = H/2
local greenBox = display.newRect( 0,0,100, 100)
greenBox:setFillColor( 0, 180, 0)
greenBox:setReferencePoint( display.CenterReferencePoint )
greenBox.x = (W/2-50); greenBox.y = (H/2-50)
local swiped = false
--function for sliding squares
local function slideIt(e)
if e.target.id ~= "nil" then
local greenSquare = e.target --Assign e.target as greenSquare
if e.phase == "began" then -- on began phase set focus and set up for swipe
local parent = greenSquare.parent
parent:insert( greenSquare )
display.getCurrentStage():setFocus( greenSquare, e.id )
greenSquare.isFocus = true
xa = e.x; ya = e.y
xPos = greenSquare.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 greenSquare.y \< 290 then -- This checks to make sure the green square isn't too low to transition down
swiped = true
yPos = greenSquare.y + 100
transition.to( greenSquare, {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 greenSquare.x \< 210 then -- This checks to make sure the green square isn't too far right to transition right
swiped = true
xPos = greenSquare.x + 100
transition.to( greenSquare, {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 greenSquare.y \> 190 then -- This checks to make sure the green square isn't too far up to transition up
swiped = true
yPos = greenSquare.y - 100
transition.to( greenSquare, {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 greenSquare.x \> 110 then -- This checks to make sure the green square isn't too far left to transition left
swiped = true
xPos = greenSquare.x - 100
transition.to( greenSquare, {time = 500, x = xPos, onComplete = function() swiped = false end})
end
end
end
if e.phase == "ended" then
display.getCurrentStage():setFocus( greenSquare, nil ) -- Release the touch focus on "ended" phase
greenSquare.isFocus = false
end
return true
end
end
greenBox:addEventListener( "touch", slideIt)
*View as iPhone. [import]uid: 40731 topic_id: 35364 reply_id: 140574[/import]
Take a look at the mathlib I posted (my link above) and try using the lengthOf() and maybe angleBetweenPoints() functions. In the ended phase of a touch event you would want to test the length of the xStart,yStart and x,y locations. Using the angle between them would tell you the direction and you could even check the time spent making the swipe.
You’ve done pretty well above, so let me know if this gets you anywhere or I can write it for you later. Have a stab at it… [import]uid: 8271 topic_id: 35364 reply_id: 140588[/import]
thanks brian nexus that helped alot, exactly what i was looking for, now i need that to work with more then one green boxes in the same white box and not to go over each other.
want to help with that?
thanks [import]uid: 218976 topic_id: 35364 reply_id: 140597[/import]