The script below has two functions, rotate and move objects, the rotation is going well but the event to move the platform object, I do not work well. When I drag your finger or in this case (in the simulator) drag the mouse pointer before touching the object, I get the error in the image attached. What I want is that the object only when you drag to move to the right above and not close to the finger. I hope I explained well, I leave the running objects in case anyone wants to try script.
adjustlevel = true
local function rotatePlatform(event)
alerttouched = event.target
if adjustlevel == true then
if (event.phase == "began") then
display.getCurrentStage():setFocus( alerttouched )
elseif (event.phase == "moved") then
platformTouched.x2 = event.x
platformTouched.y2 = event.y
angle1 = 180/math.pi * math.atan2(platformTouched.y1 - platformTouched.y , platformTouched.x1 - platformTouched.x)
angle2= 180/math.pi * math.atan2(platformTouched.y2 - platformTouched.y , platformTouched.x2 - platformTouched.x)
differencebetweenangles = angle1 - angle2
--rotate it
platformTouched.rotation = platformTouched.rotation - differencebetweenangles
platformTouched.x1 = platformTouched.x2
platformTouched.y1 = platformTouched.y2
elseif event.phase == "ended" or event.phase == "cancelled" then
display.getCurrentStage():setFocus( nil )
display.remove( rotationalert )
rotationalert = nil
end
end
end
local function movePlatform(event)
platformTouched = event.target
if adjustlevel == true then
if (event.phase == "began") then
display.getCurrentStage():setFocus( platformTouched )
display.remove( rotationalert )
rotationalert = nil
-- here the first position is stored in x and y
platformTouched.startMoveX = platformTouched.x
platformTouched.startMoveY = platformTouched.y
platformTouched.x1 = event.x
platformTouched.y1 = event.y
elseif (event.phase == "moved") then
-- here the distance is calculated between the start of the movement and its current position of the drag
platformTouched.x = (event.x - event.xStart) + platformTouched.startMoveX
platformTouched.y = (event.y - event.yStart) + platformTouched.startMoveY
elseif event.phase == "ended" or event.phase == "cancelled" then
rotationalert = display.newImage ("rotation.png")
rotationalert.x = platformTouched.x
rotationalert.y = platformTouched.y
rotationalert.alpha = 0.5
rotationalert:addEventListener ("touch", rotatePlatform)
--screenGroup:insert(rotationalert)
display.getCurrentStage():setFocus( nil )
end
return true
end
end
plataforma = display.newImage("plataforma.png")
plataforma.x = display.contentWidth*0.5
plataforma.y = display.contentHeight*0.5
plataforma:addEventListener( "touch", movePlatform)
sorry, for now I can not post pictures, I will put links:
http://www.wekin.es/pruebas/error.jpg
http://www.wekin.es/pruebas/plataforma.png
http://www.wekin.es/pruebas/rotation.png
regards