Make arrow(image) track objects, lika a compass

Here is my code:

local square = display.newRect( 100, 100, 50, 50) square:setFillColor( 1 ) function square:touch( event ) if event.phase == "began" then display.getCurrentStage():setFocus( self, event.id ) self.isFocus = true self.markX = self.x self.markY = self.y 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 elseif event.phase == "ended" or event.phase == "cancelled" then display.getCurrentStage():setFocus( self, nil ) self.isFocus = false end end return true end square:addEventListener( "touch", square ) local arrow = display.newImageRect("arrow.png",50,50) arrow.x=display.contentCenterX arrow.y=display.contentCenterY

Basically I have a square and an arrow image in the center (may have to get an image of an arrow to make it work)

The user can drag around the square.

What I want it to do:

Like a compass, I want the arrow image to rotate in the direction as the square when the square is being dragged.

How do i do that?

–Edit: Image of arrow is attached

you can calculate the angle using the math library (check documentation)

Agreed, @anaqim is right.

I would however like to note, that if you are an SSK user, this is easy as pie:

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions1/#facing-face

actions.gif

function arrow.enterFrame( self ) if( not self.target ) then return end ssk.actions.face( self, { target = self.target } ) end Runtime:addEventListener('enterFrame') -- .. later arrow.target = square

Or if you want the arrow to rotate at a limited angular rate to face the target:

function arrow.enterFrame( self ) if( self.target ) then ssk.actions.face( self, { target = self.target, rate = 180 } ) -- 180 degrees per second else ssk.actions.face( self, { pause = true } ) end end Runtime:addEventListener('enterFrame') -- .. later arrow.target = square

SSK seem to be a good toolset you should consider.

From what I understand it extends the corona API with numerous nice to have functions

RG also support his stuff well afaik.

Thanks guys!

I will defintily try out SSK.

Instead I got a close solution:

  arrow.rotation=360-square.y/2.5

Added this within the event code.

you can calculate the angle using the math library (check documentation)

Agreed, @anaqim is right.

I would however like to note, that if you are an SSK user, this is easy as pie:

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/actions1/#facing-face

actions.gif

function arrow.enterFrame( self ) if( not self.target ) then return end ssk.actions.face( self, { target = self.target } ) end Runtime:addEventListener('enterFrame') -- .. later arrow.target = square

Or if you want the arrow to rotate at a limited angular rate to face the target:

function arrow.enterFrame( self ) if( self.target ) then ssk.actions.face( self, { target = self.target, rate = 180 } ) -- 180 degrees per second else ssk.actions.face( self, { pause = true } ) end end Runtime:addEventListener('enterFrame') -- .. later arrow.target = square

SSK seem to be a good toolset you should consider.

From what I understand it extends the corona API with numerous nice to have functions

RG also support his stuff well afaik.

Thanks guys!

I will defintily try out SSK.

Instead I got a close solution:

  arrow.rotation=360-square.y/2.5

Added this within the event code.