Error al mover un objeto

Hola,

El script de más abajo tiene dos funciones, rotar y mover objetos, la rotación me va bien pero el evento para mover el objeto plataforma , no me funciona bien. Cuando arrastro el dedo o en este caso (en el simulador) arrastro el puntero del ratón antes de tocar dicho objeto, me da el error que figura en la imagen que adjunto.

Lo que pretendo es que el objeto solo se mueva cuando lo arrastre con el dedo justo encima y no próximo a el.

Espero haberme explicado bien, os dejo el script funcionando con los objetos por si alguien quiere probarlo.

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)

Saludosss :slight_smile:

He probado  sustituir:

    platformTouched.x = (event.x - event.xStart) + platformTouched.startMoveX     platformTouched.y = (event.y - event.yStart) + platformTouched.startMoveY

por:

    platformTouched.x, platformTouched.y = event.x, event.y 

Ahora puedo controlar mejor el movimiento del objeto pero cuando pincho sobre dicho objeto, este se me centra en el puntero del ratón. Alguien sabe cómo evitar esto?. Lo que intento es que si pincho sobre el objeto en un extremo, al moverlo con el ratón, que el puntero del ratón siga en el extremo del objeto y que no se me centre en el.

Espero haberme explicado bien.

Saludos.

quinohp - uno de nuestros ingenieros me dice esto:


I suspect is potentially happening just based on that has to do with how touch handling works.  Imaging for a second you have a 100 x 100 pixel square.  When you touch it, you don’t touch it in the center, but closer to one of the edges.  If you do:

square.x = event.x
square.y = event.y

You end up centering the square to where you’re touching and it causes the object to visually jump when you start dragging it around.  Our touch/drag sample app has some math that basically computes the offset of your touch from the center of the object and when dragging it around it adjusts the x, y of the object to compensate for the distance your touch is away from the center.  It took me a while to figure out what that code was doing, so would suggest that he look at the drag sample apps to see how to compute the offset of the touch from the center of the object.


Obviamente te lo puse en ingles. Si necesitas que lo traduzca dime…

He probado  sustituir:

    platformTouched.x = (event.x - event.xStart) + platformTouched.startMoveX     platformTouched.y = (event.y - event.yStart) + platformTouched.startMoveY

por:

    platformTouched.x, platformTouched.y = event.x, event.y 

Ahora puedo controlar mejor el movimiento del objeto pero cuando pincho sobre dicho objeto, este se me centra en el puntero del ratón. Alguien sabe cómo evitar esto?. Lo que intento es que si pincho sobre el objeto en un extremo, al moverlo con el ratón, que el puntero del ratón siga en el extremo del objeto y que no se me centre en el.

Espero haberme explicado bien.

Saludos.

quinohp - uno de nuestros ingenieros me dice esto:


I suspect is potentially happening just based on that has to do with how touch handling works.  Imaging for a second you have a 100 x 100 pixel square.  When you touch it, you don’t touch it in the center, but closer to one of the edges.  If you do:

square.x = event.x
square.y = event.y

You end up centering the square to where you’re touching and it causes the object to visually jump when you start dragging it around.  Our touch/drag sample app has some math that basically computes the offset of your touch from the center of the object and when dragging it around it adjusts the x, y of the object to compensate for the distance your touch is away from the center.  It took me a while to figure out what that code was doing, so would suggest that he look at the drag sample apps to see how to compute the offset of the touch from the center of the object.


Obviamente te lo puse en ingles. Si necesitas que lo traduzca dime…