DRAG AND ROTATE MULTIPLE OBJECTS ( one at time )

I’m trying to drag + rotate some objects in a game

here is the code i arrived for 1 specific defined object  but not working; it works only the drag part of the code.

----- Scivolo DX  Drag Move  ------------------------------- 

    

    scivolodx1= display.newImage(“scivolodx001legnoviti160.png”)

    screenGroup:insert(scivolodx1)

    scivolodx1.x = scivolodx1.contentWidth *1.5 + 30

    scivolodx1.y = display.contentHeight - scivolodx1.contentHeight/2 -30

    scivolodx1.rotation = 0

    physics.addBody( scivolodx1, “dynamic”, physicsData:get(“scivolodx001legnoviti160”) )

     scivolodx1.isSleepingAllowed = false

    

    

    function scivolodx1:touch( event )

    if event.phase ==“began” then

        display.getCurrentStage():setFocus( self, event.id )

        self.isFocus = true

        

        self.markX = self.x

        self.markY = self.y

          -------------------------------------------test to rotate ( made simple )

        elseif self.isFocus then

        

            if event.phase == “stationary” then

            self.rotation = self.rotation + 45

                    

            end

        -------------------------------------------drag touch object

        elseif self.isFocus then

            if event.phase == “moved” then

            self.x = event.x - event.xStart + self.markX

            self.y = event.y - event.yStart + self.markY

            

           ----------------------- Limit themoving Object at display size ( or borders defined by other objects)

            

            if self.x < self.contentWidth/2 + BordoSX.contentWidth then 

                self.x = self.contentWidth/2 + BordoSX.contentWidth

            end

            if self.x > display.contentWidth - self.contentWidth/2   then

                self.x = display.contentWidth - self.contentWidth/2 

            end

            if self.y <  self.contentHeight/2  then

                self.y = self.contentHeight/2 

            end

            if self.y >   display.contentHeight - self.contentHeight/2 - interfaccia1.contentHeight then

                self.y = display.contentHeight - self.contentHeight/2 - interfaccia1.contentHeight

            end

            

        elseif event.phase == “ended” or event.phase == “cancelled” then

-------------------------- end focus

display.getCurrentStage():setFocus( self, nil )

self.isFocus = false

end

end

---------------------------- event handled

return true

end

– scivolosx1 Touch Event Listener

scivolodx1:addEventListener( “touch”, scivolodx1) 

another question is:  if  have 100 object spawned with a repeated function or a cicle for i = 1, 100

which is the fastest way to build a Function to drag move and rotate the object  i put the finger on ?

Thanks in advance

Take a look here: http://springboardpillow.blogspot.co.uk/2012/04/sample-code.html

You want to have a look at the “Implementing Pinch-Zoom Rotate” and “Touch Listener Function for quick Copy/Paste”.

Here is a small function for dragging a physics object, which should not be done by directly modifying the x and y values:

-- perform physics touch joint drag of a physics object function physTouch( e ) &nbsp;&nbsp;&nbsp;&nbsp;if (e.phase == "began") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display.getCurrentStage():setFocus(e.target) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.touchjoint = physics.newJoint("touch",e.target,e.x,e.y) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.hasFocus = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true &nbsp;&nbsp;&nbsp;&nbsp;elseif (e.target.hasFocus) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (e.phase == "moved") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.touchjoint:setTarget(e.x,e.y) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display.getCurrentStage():setFocus(nil) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.touchjoint:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.touchjoint = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.hasFocus = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;return false end -- attach touch listener to your physics object physObj:addEventListener("touch", physTouch)

tnx , but  why it sould be not  done modifying the x and y values ?

and my problem is the rotation not the drag-move…

still unsolved…

i have modified my code with yours… but. the simulator gives me and error…

“attempt to call method” setTarget" a nil value"

function physTouch( e )

    if (e.phase == “began”) then

        display.getCurrentStage():setFocus(e.target)

        e.target.touchjoint = physics.newJoint(“touch”,e.target,e.x,e.y)

        e.target.hasFocus = true

        return true

    elseif (e.target.hasFocus) then

        if (e.phase == “moved”) then

            e.target:setTarget(e.x,e.y)

        else

            display.getCurrentStage():setFocus(nil)

            e.target.touchjoint:removeSelf()

            e.target.touchjoint = nil

            e.target.hasFocus = false

        end

        return true

    end

    return false

end

scivolodx1:addEventListener(“touch”, physTouch)

Physics bodies should not be absolutely positioned because you end up fighting the physics engine and may get very unwanted results.

I’ve added the .touchjoint:setTarget() to the move phase.

but the problem is that i need to make object as physics bodiees because my game is a physics based one …

so i need it.

Take a look here: http://springboardpillow.blogspot.co.uk/2012/04/sample-code.html

You want to have a look at the “Implementing Pinch-Zoom Rotate” and “Touch Listener Function for quick Copy/Paste”.

Here is a small function for dragging a physics object, which should not be done by directly modifying the x and y values:

-- perform physics touch joint drag of a physics object function physTouch( e ) &nbsp;&nbsp;&nbsp;&nbsp;if (e.phase == "began") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display.getCurrentStage():setFocus(e.target) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.touchjoint = physics.newJoint("touch",e.target,e.x,e.y) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.hasFocus = true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true &nbsp;&nbsp;&nbsp;&nbsp;elseif (e.target.hasFocus) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (e.phase == "moved") then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.touchjoint:setTarget(e.x,e.y) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;display.getCurrentStage():setFocus(nil) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.touchjoint:removeSelf() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.touchjoint = nil &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.target.hasFocus = false &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;return false end -- attach touch listener to your physics object physObj:addEventListener("touch", physTouch)

tnx , but  why it sould be not  done modifying the x and y values ?

and my problem is the rotation not the drag-move…

still unsolved…

i have modified my code with yours… but. the simulator gives me and error…

“attempt to call method” setTarget" a nil value"

function physTouch( e )

    if (e.phase == “began”) then

        display.getCurrentStage():setFocus(e.target)

        e.target.touchjoint = physics.newJoint(“touch”,e.target,e.x,e.y)

        e.target.hasFocus = true

        return true

    elseif (e.target.hasFocus) then

        if (e.phase == “moved”) then

            e.target:setTarget(e.x,e.y)

        else

            display.getCurrentStage():setFocus(nil)

            e.target.touchjoint:removeSelf()

            e.target.touchjoint = nil

            e.target.hasFocus = false

        end

        return true

    end

    return false

end

scivolodx1:addEventListener(“touch”, physTouch)

Physics bodies should not be absolutely positioned because you end up fighting the physics engine and may get very unwanted results.

I’ve added the .touchjoint:setTarget() to the move phase.

but the problem is that i need to make object as physics bodiees because my game is a physics based one …

so i need it.