simple question

[lua]local w = display.contentWidth

local player = display.newRect ( 160, 360, 160, 10 )

local leftbtn = display.newRect ( w * 0.15, h * 0.9, 60, 40 )
leftbtn:setFillColor ( 0, 1, 0 )

local rightbtn = display.newRect ( w * 0.85, h * 0.9, 60, 40 )
rightbtn:setFillColor ( 0, 0, 1 )

function playerrotationleft ( event )
     if event.phase == “began” then
          player.rotation = player.rotation - 15
     end
end

     
function playerrotationright ( event )
     if event.phase == “began” then
          player.rotation = player.rotation + 15
     end
end

leftbtn:addEventListener ( “touch”, playerrotationleft )
rightbtn:addEventListener ( “touch” , playerrotationright)[/lua]

what i want to do here is, when i keep touching the leftbtn or rightbtn I want the bar to keep rotating

but when i set event.phase == “began” it active once

if i delete the event.phase == “began” it active twice

or event.phase == “moved” it active when i move in the btn

my question is what should i do to make the bar keep rotating if i keep “pressing” the btn

adjust the rotatePerFrame value to get the speed you want.

You’ll probably need to build a larger rectangle of 0.01 alpha around the buttons, catch if the user touches these areas and set player.direction to “none”, so if the user’s finger slides off the button rather than lifting you don’t get stuck in a particular state.

[lua]

local rotatePerFrame = 1

local w = display.contentWidth

local player = display.newRect ( 160 , 360 , 160 , 10 )

player.direction = “none”

       

local leftbtn = display.newRect ( w * 0.15 , h * 0.9 , 60 , 40 )

leftbtn:setFillColor ( 0 , 1 , 0 )

leftbtn.id = “left”

     

local rightbtn = display.newRect ( w * 0.85 , h * 0.9 , 60 , 40 )

rightbtn:setFillColor ( 0 , 0 , 1 )

rightbtn.id = “right”

local gameLoop = function ()

           

      if player.direction ~= “none” then

                 

            if player.direction == “left” then

                       

                  player.rotation = player.rotation - rotatePerFrame

            else

                       

                  player.rotation = player.rotation + rotatePerFrame

            end

      end

end

local touchButton = function (event)

      if event.phase == “began” then

            player.direction = event.target.id

      end

      if event.phase == “ended” then

                 

            player.direction = “none”

      end

end

leftbtn:addEventListener ( “touch”, touchButton )

rightbtn:addEventListener ( “touch” , touchButton)

Runtime:addEventListener(“enterFrame”, gameLoop)

[/lua]

adjust the rotatePerFrame value to get the speed you want.

You’ll probably need to build a larger rectangle of 0.01 alpha around the buttons, catch if the user touches these areas and set player.direction to “none”, so if the user’s finger slides off the button rather than lifting you don’t get stuck in a particular state.

[lua]

local rotatePerFrame = 1

local w = display.contentWidth

local player = display.newRect ( 160 , 360 , 160 , 10 )

player.direction = “none”

       

local leftbtn = display.newRect ( w * 0.15 , h * 0.9 , 60 , 40 )

leftbtn:setFillColor ( 0 , 1 , 0 )

leftbtn.id = “left”

     

local rightbtn = display.newRect ( w * 0.85 , h * 0.9 , 60 , 40 )

rightbtn:setFillColor ( 0 , 0 , 1 )

rightbtn.id = “right”

local gameLoop = function ()

           

      if player.direction ~= “none” then

                 

            if player.direction == “left” then

                       

                  player.rotation = player.rotation - rotatePerFrame

            else

                       

                  player.rotation = player.rotation + rotatePerFrame

            end

      end

end

local touchButton = function (event)

      if event.phase == “began” then

            player.direction = event.target.id

      end

      if event.phase == “ended” then

                 

            player.direction = “none”

      end

end

leftbtn:addEventListener ( “touch”, touchButton )

rightbtn:addEventListener ( “touch” , touchButton)

Runtime:addEventListener(“enterFrame”, gameLoop)

[/lua]