Touch handlers

Hi all, 

Here is a bit of code that i thought would work like iOS converting CGPoints to move an object… But I get a choppy movement actually the player moves where it is directed to by code. But what I want to achieve is a smooth movement of the Player with my finger along the X axis… Where did i go wrong ? I thought the  playerAlex.hasFocus = true would accomplish that.

local function onObjectTouch( event )

  if event.phase == “began” then

    playerAlex.hasFocus = true

      display.getCurrentStage():setFocus(playerAlex)

        playerAlex.x = event.x

          elseif event.phase == “ended” then

              playerAlex.x = event.x 

  end

  return true 

end

  Runtime:addEventListener(“touch”,onObjectTouch)

Thanks 

JZ 

The .hasFocus/.isFocus parameter is not connected to any Corona underlying function. It’s simply a custom parameter used as a check to avoid firing the code within event.phase == “moved”/“ended” unless a “began” phase has fired already.
If I understood your question correctly, you would have to change the code like this:
 

local function onObjectTouch(event) if event.phase == "began" then display.getCurrentStage():setFocus(playerAlex); playerAlex.isFocus = true; playerAlex.x = event.x; elseif event.phase == "moved" and playerAlex.isFocus then playerAlex.x = event.x; elseif event.phase == "ended" and playerAlex.isFocus then display.getCurrentStage():setFocus(nil); playerAlex.isFocus = nil; playerAlex.x = event.x; end end Runtime:addEventListener("touch", onObjectTouch);

Great !! , I see it now , thank you so much 

Greatly appreciated

JZ

It worked perfectly 100 % , RagDogStudios  your llc ? great name… 

Thanks again 

JZ

The .hasFocus/.isFocus parameter is not connected to any Corona underlying function. It’s simply a custom parameter used as a check to avoid firing the code within event.phase == “moved”/“ended” unless a “began” phase has fired already.
If I understood your question correctly, you would have to change the code like this:
 

local function onObjectTouch(event) if event.phase == "began" then display.getCurrentStage():setFocus(playerAlex); playerAlex.isFocus = true; playerAlex.x = event.x; elseif event.phase == "moved" and playerAlex.isFocus then playerAlex.x = event.x; elseif event.phase == "ended" and playerAlex.isFocus then display.getCurrentStage():setFocus(nil); playerAlex.isFocus = nil; playerAlex.x = event.x; end end Runtime:addEventListener("touch", onObjectTouch);

Great !! , I see it now , thank you so much 

Greatly appreciated

JZ

It worked perfectly 100 % , RagDogStudios  your llc ? great name… 

Thanks again 

JZ