0
votes

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

3

3 Answers

1
votes

I'm not sure I fully understand the question (and I know I don't know much about Corona SDK) but it sounds like the problem is that you aren't getting a "begin" phase event on your object when the drag/touch starts outside of your platforma object and are only getting the "moved" phase event when the finger crosses the object.

As-such platformTouched.startMoveX has no value when that happens and so you get the error in your screenshot.

Given that you don't want the finger dragging to do anything to the object in that case anyway it would seem that you should just be checking for platformTouched.startMoveX existing in the "moved" and "ended" phase blocks and ignoring the event if it doesn't exist.

0
votes

Thanks for answering Etan.

I tried to replace:

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

by:

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

Now I can better control the movement of the object but when I click on the object, this is me focusing on your mouse. Anyone know how to avoid this ?. What I intend is that if I click on the object at one end, to move the mouse, the mouse follow at the end of the object and I will not focus on it.

I hope I explained well.

Greetings.

0
votes

Fixed, explain how I solved if anyone needs it. I left it as it was just that I have added the event listener isfocus enabling it after the first Stage:

platformTouched.isFocus = true

and after the first position where it is stored, I have finished the event listener isfocus:

       platformTouched.markX = platformTouched.x
       platformTouched.markY = platformTouched.y


elseif platformTouched.isFocus then

and finally I added a false after the Stage end to end:

       display.getCurrentStage():setFocus( nil )
       platformTouched.isFocus = false

The same've done with the rotation, I do well and error to drag your finger or stylus across the screen.   A greeting and thanks for interesaros by subject.