perpetual motion

hi, 

i want to move a character to right and next to left and next to right, …

i don’t see my error…have you an idea ?

thanks 

-- -- main.lua -- ----------------------------------------------------------------------------------------- local pupus = display.newImageRect("Pustulle.png", 100, 100) pupus.x = 160 pupus.y = 450 pupus.xScale=0.6 pupus.yScale=0.6 side = 320 function movetoside (event) print(pupus.x, side)    if pupus.x == 0 then        side = 320    end    if pupus.x == 320 then     side = 0    end return side     end Runtime:addEventListener("enterFrame", movetoside) transition.to(pupus, { tag="movecharacterend", time=500, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo })    

Hi @espace3d,

I would highly suggest that you just implement this with “onComplete” listeners and transitions, instead of a Runtime listener. Just start the first transition, then when it completes, trigger another one in the reverse direction, and when that completes, start a transition in the opposite direction, and so on perpetually.

Take care,

Brent

hi, thanks for your response, i need the value side for later.

I have tried with onComplete but the second transition don’t work. however the value side is correct…i see her in the terminal.

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local pupus = display.newImageRect("Pustulle.png", 100, 100) pupus.x = 0 pupus.y = 450 pupus.xScale=0.6 pupus.yScale=0.6 side = 320 function movetoside (event) print("pupus.x",pupus.x, "side", side)     if pupus.x == 0 then         side = 320     end     if pupus.x == 320 then     side = 0     end return side     end Runtime:addEventListener("enterFrame", movetoside) function next()     transition.to(pupus, { tag="movecharacter", time=500, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo })      end transition.to(pupus, { tag="mcharacter", time=500, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo, onComplete=next })     

finded, i must let the time to calculate the value side…so with 300 and 5 it’s correct.

-- -- main.lua -- ----------------------------------------------------------------------------------------- local pupus = display.newImageRect("Icon.png", 100, 100) pupus.x = 0 pupus.y = 450 pupus.xScale=0.6 pupus.yScale=0.6 side = 320 function movetoside (event) print("pupus.x",pupus.x, "side", side) &nbsp;&nbsp;&nbsp; if pupus.x \<= 5 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; side = 320 &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if pupus.x \>= 300 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; side = 0 &nbsp;&nbsp;&nbsp; end return side end Runtime:addEventListener("enterFrame", movetoside) function next() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(pupus, { tag="movecharacter", time=500, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo, onComplete=tr }) end function tr() transition.to(pupus, { tag="movecharacter", time=500, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo, onComplete=next }) end tr()

i would after a random time pause my transition to put a random side.

but the transition.pause must be after the character have go to a side (0 or 320)

have you an idea to do that ?

-- -- main.lua -- ----------------------------------------------------------------------------------------- local pupus = display.newImageRect("Icon.png", 100, 100) pupus.x = 0 pupus.y = 450 pupus.xScale=0.6 pupus.yScale=0.6 side = 320 -- prob=math.random(1,2) timetransition=math.random(1000,5000) flag=true function movetoside (event) print("pupus.x",pupus.x, "side", side, "prob", prob) if flag ==true then &nbsp;&nbsp;&nbsp; if pupus.x \<= 1 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; side = 320 &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if pupus.x \>= 310 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; side = 0 &nbsp;&nbsp;&nbsp; end else &nbsp;&nbsp;&nbsp; side = math.random(0,320) end return side end Runtime:addEventListener("enterFrame", movetoside) function next() transition.to(pupus, { tag="movecharacter", time=timetransition, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo, onComplete=tr }) prob=math.random(1,2) return prob end function randomside() side = math.random(0,320) return side end&nbsp; &nbsp; function tr() transition.to(pupus, { tag="movecharacter", time=timetransition, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo, onComplete=next }) return delaypause end function pauseside() flag=false transition.pause("movecharacter") print("yahououououou") randomside() tr() return flag end timer.performWithDelay( math.random(500,2000), pauseside ) tr()

Hi @espace3d,

I would trigger the “pause” timer but, instead of immediately pausing the transition, just set a variable like “willPause” to "true. Then, when the transition completes (after the “onComplete”) check if “willPause” is true. If yes, then pause the transition, and do whatever else you need in that step.

Brent

hi,

after a bad night to find the solution…can you help me.

I want only stop an transition to put a another (with transitionRight or transitionLeft ) but i can’t do it

  • my button right and left don’t work good
  • text touchright and touchleft don’t appear
  • sometimes i have graphical problem because my circle jump to an opposite position

it would be nice if you can take a look on this simplified snippet

display.setStatusBar( display.HiddenStatusBar ) local CircleonTransition = display.newCircle( 150, 200, 100 ) CircleonTransition:setFillColor( 1,1,1 ) CircleonTransition.x = 150 CircleonTransition.y = 200 CircleonTransition.xScale = 0.1 CircleonTransition.yScale = 0.1 local textInfo = display.newText("", 240,105,native.systemFont,40) textInfo:setFillColor(1,1,1) local number = 1 local timeTransition = 5000 local circleLeftButton = display.newCircle(52,105,30) circleLeftButton.alpha = 0.5 local circleRightButton = display.newCircle(427,105,30) circleRightButton.alpha = 0.5 local side = 200 local sideLeft = 380 local sideRight = 50 local souffleRightx =200 local souffleLeftx = 300 function randomSide() &nbsp;&nbsp;&nbsp; print("randomside") choix = math.random(0,1) if choix == 0 then &nbsp;&nbsp;&nbsp; side = sideLeft else &nbsp;&nbsp;&nbsp; side = sideRight end return side end &nbsp; function displacement() randomSide()&nbsp; &nbsp; function displacementitself() textInfo.text = "(".."displacement"..")" transition.to(CircleonTransition, { tag="movecharacter", time=timeTransition, x=side, y=CircleonTransition.y, transition=easing.inOutExpo, onComplete=displacement}) return side end displacementitself() end local function stopDisplacement() textInfo.text = "(".."stop"..")" transition.cancel("movecharacter") end local function transitionRight() textInfo.text = "(".."right"..")" stopDisplacement() &nbsp; transition.to(CircleonTransition, {tag="souffleRight", time=timeTransition, x=souffleRightx, y=CircleonTransition.y, rotation=-720, transition=outQuad, onComplete=displacement}) return textInfo end&nbsp; &nbsp; local function transitionLeft() textInfo.text = "(".."left"..")" stopDisplacement() transition.to(CircleonTransition, {tag="souffleLeft", time=timeTransition, x=souffleLeftx, y=CircleonTransition.y, rotation=45, transition=outQuad, onComplete=displacement}) return textInfo end circleLeftButton.touch = function( self, event ) if event.phase == "began" then textInfo.text = "(".."touchleft"..")" &nbsp;&nbsp;&nbsp; transitionLeft() end return textInfo end Runtime:addEventListener( "touch", circleLeftButton ) circleRightButton.touch = function( self, event ) if event.phase == "began" then &nbsp;&nbsp;&nbsp; textInfo.text = "(".."touchright"..")" &nbsp;&nbsp;&nbsp; transitionRight() end return textInfo end Runtime:addEventListener( "touch", circleRightButton ) timer.performWithDelay( 100, displacement )

Hi @espace3d,

It’s difficult for me to envision what is supposed to happen, for example, the difference between “transitionLeft”/“transitionRight” and “displacement”. I think this must be a simple process, but it has become too complicated and you’ve created too many functions.

Can you write the specific steps of what should happen, in exact order? For example, “1) user touches button, 2) object begins moving to some position, 3) something happens when object reaches that position”, etc.

Thanks,

Brent

hi Brent,

i understand that’s not simple to understand the code of another.

to have a button that could be touch is my snippet good ? Because in my case i can’t do the distinction between the left and right button…it always print “touchright”

local circleLeftButton = display.newCircle(52,105,30) circleLeftButton.alpha = 0.5 local circleRightButton = display.newCircle(427,105,30) circleRightButton.alpha = 0.5 circleLeftButton.touch = function( self, event ) if event.phase == "began" then textInfo.text = "(".."touchleft"..")" &nbsp;&nbsp;&nbsp; transitionLeft() end return textInfo end Runtime:addEventListener( "touch", circleLeftButton ) circleRightButton.touch = function( self, event ) if event.phase == "began" then &nbsp;&nbsp;&nbsp; textInfo.text = "(".."touchright"..")" &nbsp;&nbsp;&nbsp; transitionRight() end return textInfo end Runtime:addEventListener( "touch", circleRightButton )

below just a snippet for remember myself   :slight_smile:

function begin() flag= true print(flag) function stop() flag = false print(flag) return flag end timer.performWithDelay(5000,stop) return flag end begin()

Hi again,

Why are you using Runtime listeners for buttons? Most people use listeners on the object itself, for example:

[lua]

local circleLeftButton = display.newCircle(52,105,30)

circleLeftButton.alpha = 0.5

local function buttonTouch( event )

   if ( event.phase == “began” ) then

      --do something

   end

end

circleLeftButton:addEventListener( “touch”, buttonTouch )

[/lua]

Hi @espace3d,

I would highly suggest that you just implement this with “onComplete” listeners and transitions, instead of a Runtime listener. Just start the first transition, then when it completes, trigger another one in the reverse direction, and when that completes, start a transition in the opposite direction, and so on perpetually.

Take care,

Brent

hi, thanks for your response, i need the value side for later.

I have tried with onComplete but the second transition don’t work. however the value side is correct…i see her in the terminal.

----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local pupus = display.newImageRect("Pustulle.png", 100, 100) pupus.x = 0 pupus.y = 450 pupus.xScale=0.6 pupus.yScale=0.6 side = 320 function movetoside (event) print("pupus.x",pupus.x, "side", side) &nbsp; &nbsp; if pupus.x == 0 then &nbsp; &nbsp; &nbsp; &nbsp; side = 320 &nbsp; &nbsp; end &nbsp; &nbsp; if pupus.x == 320 then &nbsp; &nbsp; side = 0 &nbsp; &nbsp; end return side &nbsp; &nbsp; end Runtime:addEventListener("enterFrame", movetoside) function next() &nbsp; &nbsp; transition.to(pupus, { tag="movecharacter", time=500, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo }) &nbsp; &nbsp;&nbsp; end transition.to(pupus, { tag="mcharacter", time=500, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo, onComplete=next }) &nbsp; &nbsp;&nbsp;

finded, i must let the time to calculate the value side…so with 300 and 5 it’s correct.

-- -- main.lua -- ----------------------------------------------------------------------------------------- local pupus = display.newImageRect("Icon.png", 100, 100) pupus.x = 0 pupus.y = 450 pupus.xScale=0.6 pupus.yScale=0.6 side = 320 function movetoside (event) print("pupus.x",pupus.x, "side", side) &nbsp;&nbsp;&nbsp; if pupus.x \<= 5 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; side = 320 &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if pupus.x \>= 300 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; side = 0 &nbsp;&nbsp;&nbsp; end return side end Runtime:addEventListener("enterFrame", movetoside) function next() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transition.to(pupus, { tag="movecharacter", time=500, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo, onComplete=tr }) end function tr() transition.to(pupus, { tag="movecharacter", time=500, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo, onComplete=next }) end tr()

i would after a random time pause my transition to put a random side.

but the transition.pause must be after the character have go to a side (0 or 320)

have you an idea to do that ?

-- -- main.lua -- ----------------------------------------------------------------------------------------- local pupus = display.newImageRect("Icon.png", 100, 100) pupus.x = 0 pupus.y = 450 pupus.xScale=0.6 pupus.yScale=0.6 side = 320 -- prob=math.random(1,2) timetransition=math.random(1000,5000) flag=true function movetoside (event) print("pupus.x",pupus.x, "side", side, "prob", prob) if flag ==true then &nbsp;&nbsp;&nbsp; if pupus.x \<= 1 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; side = 320 &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if pupus.x \>= 310 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; side = 0 &nbsp;&nbsp;&nbsp; end else &nbsp;&nbsp;&nbsp; side = math.random(0,320) end return side end Runtime:addEventListener("enterFrame", movetoside) function next() transition.to(pupus, { tag="movecharacter", time=timetransition, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo, onComplete=tr }) prob=math.random(1,2) return prob end function randomside() side = math.random(0,320) return side end&nbsp; &nbsp; function tr() transition.to(pupus, { tag="movecharacter", time=timetransition, x=side, y=450, yScale=0.6, xScale=0.6, transition=easing.inOutExpo, onComplete=next }) return delaypause end function pauseside() flag=false transition.pause("movecharacter") print("yahououououou") randomside() tr() return flag end timer.performWithDelay( math.random(500,2000), pauseside ) tr()

Hi @espace3d,

I would trigger the “pause” timer but, instead of immediately pausing the transition, just set a variable like “willPause” to "true. Then, when the transition completes (after the “onComplete”) check if “willPause” is true. If yes, then pause the transition, and do whatever else you need in that step.

Brent

hi,

after a bad night to find the solution…can you help me.

I want only stop an transition to put a another (with transitionRight or transitionLeft ) but i can’t do it

  • my button right and left don’t work good
  • text touchright and touchleft don’t appear
  • sometimes i have graphical problem because my circle jump to an opposite position

it would be nice if you can take a look on this simplified snippet

display.setStatusBar( display.HiddenStatusBar ) local CircleonTransition = display.newCircle( 150, 200, 100 ) CircleonTransition:setFillColor( 1,1,1 ) CircleonTransition.x = 150 CircleonTransition.y = 200 CircleonTransition.xScale = 0.1 CircleonTransition.yScale = 0.1 local textInfo = display.newText("", 240,105,native.systemFont,40) textInfo:setFillColor(1,1,1) local number = 1 local timeTransition = 5000 local circleLeftButton = display.newCircle(52,105,30) circleLeftButton.alpha = 0.5 local circleRightButton = display.newCircle(427,105,30) circleRightButton.alpha = 0.5 local side = 200 local sideLeft = 380 local sideRight = 50 local souffleRightx =200 local souffleLeftx = 300 function randomSide() &nbsp;&nbsp;&nbsp; print("randomside") choix = math.random(0,1) if choix == 0 then &nbsp;&nbsp;&nbsp; side = sideLeft else &nbsp;&nbsp;&nbsp; side = sideRight end return side end &nbsp; function displacement() randomSide()&nbsp; &nbsp; function displacementitself() textInfo.text = "(".."displacement"..")" transition.to(CircleonTransition, { tag="movecharacter", time=timeTransition, x=side, y=CircleonTransition.y, transition=easing.inOutExpo, onComplete=displacement}) return side end displacementitself() end local function stopDisplacement() textInfo.text = "(".."stop"..")" transition.cancel("movecharacter") end local function transitionRight() textInfo.text = "(".."right"..")" stopDisplacement() &nbsp; transition.to(CircleonTransition, {tag="souffleRight", time=timeTransition, x=souffleRightx, y=CircleonTransition.y, rotation=-720, transition=outQuad, onComplete=displacement}) return textInfo end&nbsp; &nbsp; local function transitionLeft() textInfo.text = "(".."left"..")" stopDisplacement() transition.to(CircleonTransition, {tag="souffleLeft", time=timeTransition, x=souffleLeftx, y=CircleonTransition.y, rotation=45, transition=outQuad, onComplete=displacement}) return textInfo end circleLeftButton.touch = function( self, event ) if event.phase == "began" then textInfo.text = "(".."touchleft"..")" &nbsp;&nbsp;&nbsp; transitionLeft() end return textInfo end Runtime:addEventListener( "touch", circleLeftButton ) circleRightButton.touch = function( self, event ) if event.phase == "began" then &nbsp;&nbsp;&nbsp; textInfo.text = "(".."touchright"..")" &nbsp;&nbsp;&nbsp; transitionRight() end return textInfo end Runtime:addEventListener( "touch", circleRightButton ) timer.performWithDelay( 100, displacement )

Hi @espace3d,

It’s difficult for me to envision what is supposed to happen, for example, the difference between “transitionLeft”/“transitionRight” and “displacement”. I think this must be a simple process, but it has become too complicated and you’ve created too many functions.

Can you write the specific steps of what should happen, in exact order? For example, “1) user touches button, 2) object begins moving to some position, 3) something happens when object reaches that position”, etc.

Thanks,

Brent

hi Brent,

i understand that’s not simple to understand the code of another.

to have a button that could be touch is my snippet good ? Because in my case i can’t do the distinction between the left and right button…it always print “touchright”

local circleLeftButton = display.newCircle(52,105,30) circleLeftButton.alpha = 0.5 local circleRightButton = display.newCircle(427,105,30) circleRightButton.alpha = 0.5 circleLeftButton.touch = function( self, event ) if event.phase == "began" then textInfo.text = "(".."touchleft"..")" &nbsp;&nbsp;&nbsp; transitionLeft() end return textInfo end Runtime:addEventListener( "touch", circleLeftButton ) circleRightButton.touch = function( self, event ) if event.phase == "began" then &nbsp;&nbsp;&nbsp; textInfo.text = "(".."touchright"..")" &nbsp;&nbsp;&nbsp; transitionRight() end return textInfo end Runtime:addEventListener( "touch", circleRightButton )

below just a snippet for remember myself   :slight_smile:

function begin() flag= true print(flag) function stop() flag = false print(flag) return flag end timer.performWithDelay(5000,stop) return flag end begin()

Hi again,

Why are you using Runtime listeners for buttons? Most people use listeners on the object itself, for example:

[lua]

local circleLeftButton = display.newCircle(52,105,30)

circleLeftButton.alpha = 0.5

local function buttonTouch( event )

   if ( event.phase == “began” ) then

      --do something

   end

end

circleLeftButton:addEventListener( “touch”, buttonTouch )

[/lua]