Bug with transition.to ?

hi,

  • in this basic lua : a circle move to left and right with perpetual motion (transition.to)
  • why i when I press any button the transition.to is stopping and i change the coordinate x of the circle with math.random
  • after a time (timer.performwithdelay) i repeat the transition.to

the problem is after touch my button 2 time there is a bug: the circle appears from left to right so weird …

did I do something wrong ?

Thank you so much if you could help me.

--MAIN.LUA display.setStatusBar( display.HiddenStatusBar ) --TEXT INFORMATION------------------------------------------------------------------------------------------------------------------------------------- --to debug local textInfo = display.newText("", 240,105,native.systemFont,20) textInfo:setFillColor(1,1,1) local textInfo2 = display.newText("", 240,140,native.systemFont,20) textInfo2:setFillColor(1,1,1) local textInfo3 = display.newText("", 240,180,native.systemFont,20) textInfo2:setFillColor(1,1,1) local CircleonTransition = display.newRect( 150, 200, 100,200 ) CircleonTransition:setFillColor( 1,1,1 ) CircleonTransition.x = 150 CircleonTransition.y = 200 CircleonTransition.xScale = 0.1 CircleonTransition.yScale = 0.1 local circleLeftButton = display.newCircle(52,105,30) circleLeftButton.alpha = 0.5 --TIME----------------------------------------------------------------------------------------------------------------------------------------------- local timeStartFirstTransition = 100 local timeTransition = 1000 --SIDE----------------------------------------------------------------------------------------------------------------------------------------------- local sideLeft = 300 local sideRight = 20 --FUNCTION------------------------------------------------------------------------------------------------------------------------------------------- local function randomSide() --to move randomly the circleOnTransition     print("side", side)     side = math.random(10,290)     return side end function displacement() --perpetual motion    function displacementRight()     textInfo2.text = "(".."displacement"..")"     local displacementRightVariable = transition.to(CircleonTransition,         { tag="moveCharacter", time=timeTransition, x=sideRight, y=CircleonTransition.y, onComplete=displacement})     end     function displacementLeft()     textInfo3.text = "(".."displacement"..")"     local displacementLeftVariable = transition.to(CircleonTransition,         { tag="moveCharacter", time=timeTransition, x=sideLeft, y=CircleonTransition.y, onComplete=displacementRight})     end     displacementLeft() end local function stopDisplacement()     textInfo3.text = "(".."stop"..")"     transition.cancel("moveCharacter") end local function transitionRandom()       --stop the transition ; activate the random position ; wait a time ; repeat the function displacement     textInfo2.text = "(".."transitionRandom"..")"     stopDisplacement()     CircleonTransition.x= side     timer.performWithDelay( 2000, displacement ) end local function buttonTouch( event ) --the button to stop the transition    if ( event.phase == "began" ) then     textInfo.text = "(".."Button touch"..")"     stopDisplacement()     randomSide()     transitionRandom()     end end circleLeftButton:addEventListener( "touch", buttonTouch ) timer.performWithDelay( timeStartFirstTransition, displacement ) --the timer to iniate the first transition of the perpetual motion

anybody please ?

Hi @espace3d

You may want to set a flag to prevent double click.

The reason for this weird stuff is because the functions will be overlapped.

Try this.

-- add this on top local isButtonClick = true -- your button touch function local function buttonTouch( event ) --the button to stop the transition if ( event.phase == "began" and isButtonClick) then isButtonClick = false -- set to false immediately to prevent subsequent click textInfo.text = "(".."Button touch"..")" stopDisplacement() randomSide() transitionRandom() end end -- add this to somewhere inside your function displacement(). isButtonClick = true --- allows another click of the button

This way the button click function will not run again until the displacement starts.

Good Luck!

burhan

Thank you so much…I would never have been able to see where my mistake came
another string to my bow .

anybody please ?

Hi @espace3d

You may want to set a flag to prevent double click.

The reason for this weird stuff is because the functions will be overlapped.

Try this.

-- add this on top local isButtonClick = true -- your button touch function local function buttonTouch( event ) --the button to stop the transition if ( event.phase == "began" and isButtonClick) then isButtonClick = false -- set to false immediately to prevent subsequent click textInfo.text = "(".."Button touch"..")" stopDisplacement() randomSide() transitionRandom() end end -- add this to somewhere inside your function displacement(). isButtonClick = true --- allows another click of the button

This way the button click function will not run again until the displacement starts.

Good Luck!

burhan

Thank you so much…I would never have been able to see where my mistake came
another string to my bow .