Better way to code this line moving back and forth?

[lua]

local function moveVerticalLine(self)

local line = self

local function doubleAnimation()

local function animStart3()

if line~= nil then

transitionStash.newTransition = transition.to( line,  { time=250, alpha=1, x=(self.x - 60), y=(self.y - 60)} )

end

end

local function animStart2()

if line ~= nil then

transitionStash.newTransition = transition.to( line,  { time=250, alpha=1, x=(self.x + 60), y=(self.y + 60), onComplete = animStart3} )

end

end

local function animStart1()

if line ~= nil then

transitionStash.newTransition = transition.to( line,  { time=150, alpha=1, x=(self.x + 60), y=(self.y + 60), onComplete = animStart2} )

end

end

if line ~= nil then

transitionStash.newTransition = transition.to( line,  { time=150, alpha=1.0, x=(self.x - 60), y = (self.y - 60), onComplete=animStart1} )

end

end

doubleAnimation()

timerStash.doubleAnimationTimer = timer.performWithDelay(1000, doubleAnimation,1)

end

[/lua]

You can do it with “enterFrame” listener and check if the line has reached it’s destination, then multiply it’s xSpeed with -1 and move on with it.

Indeed. I’d never use transitions for this. I believe that the one thing a lot of people miss out on when starting is the skill of creating a good and solid frameLoop. The power you get from this is unparalleled.

In all esssence, create a runtime enterframe listener function that “does”:

  • Move the line by xSpeed pixels this frame

  • if (xSpeed is > 0 and xPosition > 300) or (xSpeed is < 0 and xPosition < 0) then multiply xSpeed by -1

And that’s it. You’ll need to translate this into code of course. Let me know if you have any trouble doing that.

as per others, you could accomplish this with a frame loop

though you appear to have a bit more “logic” in there than just “negate x at bounds” for your animation…

  1. line moves “slower” (250ms) diagonally to upper-left

  2. line returns “slower” (250ms) diagonally to origin

  3. line moves “faster” (150ms) diagonally to lower-right

  4. line returns “faster” (150ms) diagonally to origin

  5. line pauses static at origin (200ms)

repeat

still, adding relevant “state” and “counter” properties to your line could handle that.  your frame update routine might look ROUGHLY (just pseudocode to convey ideas, NOT run as is) like:

if state==1 then &nbsp; x=x-6 &nbsp; y=y-6 limit = 10 elseif state==2 then &nbsp; x=x+6 &nbsp; y=y+6 limit = 10 elseif state==3 then &nbsp; x=x+10 &nbsp; y=y+10 limit = 6 elseif state==4 then &nbsp; x=x-10 &nbsp; y=y-10 limit = 6 elseif state==5 then &nbsp; -- static limit = 8 end counter = counter+1 if counter\>limit then &nbsp; state = state + 1 &nbsp; if state\>5 then state=1 end counter = 0 end

You can do it with “enterFrame” listener and check if the line has reached it’s destination, then multiply it’s xSpeed with -1 and move on with it.

Indeed. I’d never use transitions for this. I believe that the one thing a lot of people miss out on when starting is the skill of creating a good and solid frameLoop. The power you get from this is unparalleled.

In all esssence, create a runtime enterframe listener function that “does”:

  • Move the line by xSpeed pixels this frame

  • if (xSpeed is > 0 and xPosition > 300) or (xSpeed is < 0 and xPosition < 0) then multiply xSpeed by -1

And that’s it. You’ll need to translate this into code of course. Let me know if you have any trouble doing that.

as per others, you could accomplish this with a frame loop

though you appear to have a bit more “logic” in there than just “negate x at bounds” for your animation…

  1. line moves “slower” (250ms) diagonally to upper-left

  2. line returns “slower” (250ms) diagonally to origin

  3. line moves “faster” (150ms) diagonally to lower-right

  4. line returns “faster” (150ms) diagonally to origin

  5. line pauses static at origin (200ms)

repeat

still, adding relevant “state” and “counter” properties to your line could handle that.  your frame update routine might look ROUGHLY (just pseudocode to convey ideas, NOT run as is) like:

if state==1 then &nbsp; x=x-6 &nbsp; y=y-6 limit = 10 elseif state==2 then &nbsp; x=x+6 &nbsp; y=y+6 limit = 10 elseif state==3 then &nbsp; x=x+10 &nbsp; y=y+10 limit = 6 elseif state==4 then &nbsp; x=x-10 &nbsp; y=y-10 limit = 6 elseif state==5 then &nbsp; -- static limit = 8 end counter = counter+1 if counter\>limit then &nbsp; state = state + 1 &nbsp; if state\>5 then state=1 end counter = 0 end