using a button to rotate a physics object.

I intend to have 2 x buttons, which are a left arrow and a right arrow.

When the user touches one of the buttons, the player character,(player1) rotates constantly about it’s centre point. The left button rotates anti-clockwise and the right button clockwise.

The player is a physics body, so using player1.rotation gives unpredictable results.

the following code totates the player by an increment every time the button is pressed, but does not rotate smoothly until the user stops pressing the button.

How can I get the player to rotate smoothly and continuously while the button is pressed?

local leftArrow = display.newRect(0,0,20,20)  
leftArrow.x = \_W / 8  
leftArrow.y = \_H / \_H + \_H / 10  
leftArrow:setFillColor(0,255,0)  
leftArrow.alpha = .75  
  
local function lrot(event)  
 player1:rotate ( player1.rotation -10 )  
  
end  
  
leftArrow:addEventListener ("touch", lrot)  

Thanks in advance. [import]uid: 67933 topic_id: 14162 reply_id: 314162[/import]

something like that, i hope it will help
[lua]_W = display.contentWidth
_H = display.contentHeight

local leftArrow = display.newRect(0,0,20,20)
leftArrow.x = _W / 8
leftArrow.y = _H / _H + _H / 10
leftArrow:setFillColor(0,255,0)
leftArrow.alpha = .75

local touched = false

local player1 = display.newRect(0,0, 100,50)
player1.x = 50
player1.y = 200

local function lrot(event)
player1:rotate ( player1.rotation -10 )

end

local function touch(event)
if event.phase == “began” then
touched = true
elseif event.phase == “ended” then
touched = false
end
end

local function rotate()
if touched == true then
player1.rotation = player1.rotation - 10
elseif touched == false then return false
end
end

Runtime:addEventListener(“enterFrame”, rotate)
leftArrow:addEventListener (“touch”, touch)[/lua] [import]uid: 16142 topic_id: 14162 reply_id: 52112[/import]

Thanks Darkconsoles.

I tried something similar earlier but unfortunately it didn’t work as I had hoped.

The problem is that object.rotation affects only the sprite and not the physics body, so you get all kinds of odd glitches.

This would be a simple problem to solve if I was just trying to change the x or y position. The issue lies with:

object:rotate(--something)  

I tried the following as well, and again got unpredictable results, (unless I’m not understanding it correctly)

rot = 0  
  
local leftArrow = display.newRect(0,0,20,20)  
leftArrow.x = \_W / 8  
leftArrow.y = \_H / \_H + \_H / 10  
leftArrow:setFillColor(0,255,0)  
leftArrow.alpha = .75  
  
local function lrot(event)  
 local function changerot()  
 rot = rot + 1  
 end  
 player1:rotate ( -rot )  
 Runtime:addEventListener("enterFrame", changerot)  
 print(rot)  
end  
  
leftArrow:addEventListener ("touch", lrot)  

I suppose a better way to phrase my question is; How can I smoothly and continuously rotate a physics body around it’s centre-point whilst a button is touched?

Any other help would be most appreciated.

Thanks [import]uid: 67933 topic_id: 14162 reply_id: 52120[/import]

Actually, scrap my last.

I’ve fiddled about with your code and finally got it working.

Thanks very much for your assistance.

Dan [import]uid: 67933 topic_id: 14162 reply_id: 52364[/import]