It isn't a major thing, but I find it can be useful.
I have not yet thought of a way to do it without physics.
How I do it:
The code will make an object called "arr" (I don't know why I called it that) which is attached to the player. The players rotation is found by finding the angle between the player and the arr object.
This will run on its own:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | physics = require( "physics" ) physics.start() physics.setGravity( 0, 0 ) ---[[ physics.setDrawMode( "hybrid" ) --]] display.setStatusBar(display.HiddenStatusBar) screenW = display.contentWidth screenH = display.contentHeight local player = display.newRect(screenW/2-100,screenH/2-100,100,100) physics.addBody(player) local arr = display.newCircle(player.x,player.y-50,5,1) arr:setFillColor( 50,205,50 ) physics.addBody(arr,{radius = 1}) local arrjoint = physics.newJoint( "pivot", player, arr, player.x,player.y-50) local function checkpress(event) eventx = event.x eventy = event.y if event.phase == "began" then touch = true elseif event.phase == "ended" then touch = false end end local tdiffer = 0 local function move() local playerangle = math.floor(180-math.atan2(arr.x - player.x, arr.y - player.y)*(180/math.pi)) if touch == true then local touchangle = math.floor(180-math.atan2(eventx - player.x, eventy - player.y)*(180/math.pi)) local differ = touchangle - playerangle if differ < -180 then tdiffer = 360 + differ elseif differ > 180 then tdiffer = differ - 360 else tdiffer = differ end if playerangle ~= touchangle then player.rotation = player.rotation + tdiffer/2 end end end Runtime:addEventListener("enterFrame", move) Runtime:addEventListener("touch", checkpress) |
if you want to do it without using physics you can use the code below
1 2 3 4 5 6 7 8 9 10 11 12 | display.setStatusBar(display.HiddenStatusBar) screenW = display.contentWidth screenH = display.contentHeight local player = display.newRect(screenW/2-100,screenH/2-100,100,100) local function checkpress(event) if event.phase == "moved" then local playerangle = math.floor(180-math.atan2(event.x - player.x, event.y - player.y)*(180/math.pi)) player.rotation =playerangle end end Runtime:addEventListener("touch", checkpress) |
That is true.
There is always a much similar way -.-
My code makes the movement smooth, but I am sure that could be put into yours.
I have a question, I am trying to figure something out.
I bolted on some additional code
--I added a square
--I added some code for shooting
The behavior now is you touch the middle larger square and he shoots the small square down in the corner.
The behavior I would like
Shoot a bullet out based on where the line in the middle square is pointing.
I suppose there are ways to interpret this, so here is more specific
1) wherever I touch on screen shoot a bullet
alternate method
2) wherever the line is pointing according to touch (the line on the square in the middle) shoot a bullet in that direction (so the bullet shoots in that direction and goes off screen, then gets cleaned up by some remove self code).
Here is your code, with some stuff I bolted on (square and bullet code)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | ---View as Iphone 4 (or you wont see other square physics = require( "physics" ) physics.start() physics.setGravity( 0, 0 ) -- normal, hybrid, debug physics.setDrawMode( "debug" ) display.setStatusBar(display.HiddenStatusBar) screenW = display.contentWidth screenH = display.contentHeight local player = display.newRect(screenW/2-100,screenH/2-100,100,100) physics.addBody(player) local arr = display.newCircle(player.x,player.y-50,5,1) arr:setFillColor( 50,205,50 ) physics.addBody(arr,{radius = 1}) local arrjoint = physics.newJoint( "pivot", player, arr, player.x,player.y-50) --Added a square so that I could demonstrate shooting a bullet, this is the target local square = display.newRect(512, 800, 50,50) physics.addBody(square) local function checkpress(event) eventx = event.x eventy = event.y if event.phase == "began" then touch = true elseif event.phase == "ended" then touch = false end end local tdiffer = 0 local function move() local playerangle = math.floor(180-math.atan2(arr.x - player.x, arr.y - player.y)*(180/math.pi)) if touch == true then local touchangle = math.floor(180-math.atan2(eventx - player.x, eventy - player.y)*(180/math.pi)) local differ = touchangle - playerangle if differ < -180 then tdiffer = 360 + differ elseif differ > 180 then tdiffer = differ - 360 else tdiffer = differ end if playerangle ~= touchangle then player.rotation = player.rotation + tdiffer/2 end end end --shooting local function shoot (event) local bullet = display.newCircle(0,0,10,10) bullet:setFillColor( 0, 255, 0) bullet.radius = 10 bullet.x = player.x bullet.y = player.y bullet.isBullet = true transition.to (bullet, {time = 400, x = square.x, y = square.y}) physics.addBody( bullet, { friction=0, bounce=.6, } ) print "shooting a bullet" end --density = 0 player:addEventListener ("tap", shoot) Runtime:addEventListener("enterFrame", move) Runtime:addEventListener("touch", checkpress) </code> |
Nice ! thanks for posting.