Hey,
I had problems with doing this recently and wound up putting together the following code, which does exactly what you are asking.
You’ll need a 50x50 png of an arrow pointing to the right in the folder.
The bit you’re interested in in on lines 145 and 146.
I hope it helps.
Spider
physics = require("physics")
physics.start()
physics.setGravity(0,0)
--physics.setDrawMode( "hybrid" ) -- uncomment to see physics interactions.
\_H = display.contentHeight;
\_W = display.contentWidth;
function player()
player1 = display.newImage ("arrow.png")
player1.x = \_W / 2
player1.y = \_H / 2
--player1.rotation = 90 -- comment out for thrust code, \*\*\* rotate player1 90 for skate code \*\*\*
physics.addBody(player1, "dynamic", {density = 0, friction = 0, bounce = 0})
player1.myName = "player1"
function setplayerRot()
playerRot = 0
local playerRotText = display.newText( "playerRot: " .. playerRot, 10, 0, nil, 15)
playerRotText.x = \_W / 4
playerRotText.y = \_H / \_H + 50
playerRotText:setTextColor(255,255,255)
function getplayerRot()
playerRot = player1.rotation
playerRotText.text = "playerRot: " .. playerRot
end
Timer1 = timer.performWithDelay(10,getplayerRot, 0)
end
end
function radians()
p1Radians = player1.rotation \* math.pi / 180
--print (p1Radians)
end
function velocity()
velx, vely = player1:getLinearVelocity()
vx = math.cos (p1Radians) \* velx
print (vx)
end
function buttons()
function lArrow()
leftArrow = display.newRect(0,0,50,50)
leftArrow.x = \_W / \_W + 50
leftArrow.y = \_H - 50
leftArrow:setFillColor(0,255,0)
leftArrow.alpha = .75
ltouched = false
local function ltouch(event)
if event.phase == "began" then
ltouched = true
elseif event.phase == "ended" then
ltouched = false
end
end
local function lrot(event)
if ltouched == true then
player1.rotation = player1.rotation - 10
elseif ltouched == false then return false
end
end
leftArrow:addEventListener ("touch", ltouch)
Runtime:addEventListener("enterFrame", lrot)
end
function rArrow()
local rightArrow = display.newRect(0,0,50,50)
rightArrow.x = \_W - 50
rightArrow.y = \_H - 50
rightArrow:setFillColor(0,255,0)
rightArrow.alpha = .75
rtouched = false
local function rtouch(event)
if event.phase == "began" then
rtouched = true
elseif event.phase == "ended" then
rtouched = false
end
end
local function rrot(event)
if rtouched == true then
player1.rotation = player1.rotation + 10
elseif rtouched == false then return false
end
end
rightArrow:addEventListener ("touch", rtouch)
Runtime:addEventListener("enterFrame", rrot)
end
function thrustButton()
local thrustb = display.newRect(0,0,50,50)
thrustb.x = \_W/2 - thrustb.contentWidth / 2 - 5
thrustb.y = \_H - 50
thrustb:setFillColor(0, 0, 255)
thrustb.aplha = .75
ttouched = false
cando = true
local function ttouch(event)
if event.phase == "began" then
ttouched = true
elseif event.phase == "ended" then
ttouched = false
cando = true
end
end
local function ttap(event)
if ttouched == true then
if cando == true then
vel = 50 -- this is the arbitrary amount of velocity to increase player motion by.
vely = -500
p1Rads = player1.rotation \* math.pi / 180 -- step 1
local velX = math.cos (p1Rads) \* vel -- upper case (velX/velx) to feed into obj:setLinearVelocity(X,Y)
local velY = math.sin (p1Rads) \* vel
print(velX,velY,player1.rotation)
player1:setLinearVelocity(-velX,-velY)
local function stopMovement()
player1:setLinearVelocity(0,0)
end
Timer1 = timer.performWithDelay(2000,stopMovement, 1)
cando =false
end
elseif ttouched == false then return false
end
end
thrustb:addEventListener ("touch", ttouch)
Runtime:addEventListener("enterFrame", ttap)
end
function thrustrButton()
local thrustrb = display.newRect(0,0,50,50)
thrustrb.x = \_W/2 + thrustrb.contentWidth / 2 + 5
thrustrb.y = \_H - 50
thrustrb:setFillColor(0, 0, 255)
thrustrb.aplha = .75
ttouched = false
candor = true
local function trtouch(event)
if event.phase == "began" then
trtouched = true
elseif event.phase == "ended" then
trtouched = false
candor = true
end
end
local function trtap(event)
if trtouched == true then
if candor == true then
vel = 50 -- this is the arbitrary amount of velocity to increase player motion by.
vely = -500
p1Rads = player1.rotation \* math.pi / 180 -- step 1
local velX = math.cos (p1Rads) \* vel -- upper case (velX/velx) to feed into obj:setLinearVelocity(X,Y)
local velY = math.sin (p1Rads) \* vel
print(velX,velY,player1.rotation)
player1:setLinearVelocity(velX,velY)
local function stopMovement()
player1:setLinearVelocity(0,0)
end
Timer1 = timer.performWithDelay(2000,stopMovement, 1)
candor =false
end
elseif trtouched == false then return false
end
end
thrustrb:addEventListener ("touch", trtouch)
Runtime:addEventListener("enterFrame", trtap)
end
lArrow()
rArrow()
thrustButton()
thrustrButton()
end
function startGame()
player()
buttons()
setplayerRot()
end
startGame()
[import]uid: 67933 topic_id: 21482 reply_id: 85024[/import]