How to move an object around the screen?

Hi Everyone,

I have been working on an app where the player controls a plane using buttons that appear on the screen. When you press one of the buttons, it slightly rotates the plane.

What I am trying to do now is to make the plane always move in the direction it’s facing, so that it looks like it is constantly flying forwards. Any ideas?

Thanks in advance!  :slight_smile:

Here is my code:

--MISC/VARIABLES-- display.setStatusBar(display.HiddenStatusBar) system.activate("multitouch") physics = require("physics") local motion = 0 local turnspeed = 5 \_W = display.contentWidth \_H = display.contentHeight physics.start() --BACKGROUND-- sky = display.newImage("Assets/Sky.png") sky.x = \_W/2 sky.y = \_H/2 --PLANE-- local plane = display.newImage("Assets/Plane.png") plane.x = \_W/2 plane.y = \_H/2 function planerotate (event) plane.rotation = plane.rotation + motion; end Runtime:addEventListener("enterFrame",planerotate) function stoprotate (event) if event.phase == "ended" then motion = 0; end end Runtime:addEventListener("touch", stoprotate) --ARROWS-- arrow\_left = display.newImage("Assets/Arrow.png") arrow\_left.x = 85 arrow\_left.y = \_H - 85 arrow\_left.rotation = 270 function arrow\_left:touch() motion = -turnspeed; end arrow\_left:addEventListener("touch", arrow\_left) local arrow\_right = display.newImage("Assets/Arrow.png") arrow\_right.x = \_W - 85 arrow\_right.y = \_H - 85 arrow\_right.rotation = 90 function arrow\_right:touch() motion = turnspeed; end arrow\_right:addEventListener("touch", arrow\_right)

Hello, liam99,

At the top of your code, in the MISC/VARIABLES part, add a “local movespeed = 2” variable and change it as you want.

In the “planerotate” enterFrame listener, add this :

function planerotate (event) plane.rotation = plane.rotation + motion; local deltax = math.cos(math.rad(plane.rotation))\*movespeed local deltay = math.sin(math.rad(plane.rotation))\*movespeed plane.x,plane.y = plane.x+deltax,plane.y+deltay end

It uses basic trigonometry to calculate the vector needed to move the plane.

You can also use plane:setLinearVelocity(deltax,deltay); to move your plane, but you will need to activate physics first.

Ulysse

@ulydev5,

Thank you for the reply!

It has helped me a lot. :slight_smile:

Hello, liam99,

At the top of your code, in the MISC/VARIABLES part, add a “local movespeed = 2” variable and change it as you want.

In the “planerotate” enterFrame listener, add this :

function planerotate (event) plane.rotation = plane.rotation + motion; local deltax = math.cos(math.rad(plane.rotation))\*movespeed local deltay = math.sin(math.rad(plane.rotation))\*movespeed plane.x,plane.y = plane.x+deltax,plane.y+deltay end

It uses basic trigonometry to calculate the vector needed to move the plane.

You can also use plane:setLinearVelocity(deltax,deltay); to move your plane, but you will need to activate physics first.

Ulysse

@ulydev5,

Thank you for the reply!

It has helped me a lot. :slight_smile: