Problems with sine movement

Hi,

I have some problems making my display object (purpleFish) moving across the screen following a sinusoidal movement.

  I managed to make the object to climb exponentially along the y axis,but I don’t know how to follow a sine movement.

Long story short : purpleFish goes up,but doesn’t climb down to follow a sine movement

Some help,ideas would be apreciated.

Thank you!

This is what I tried…

local function sineMovement () transition.to (purpleFish,{time = 5000,x = Amplitude\*math.sin(2\*3.14+6),onComplete = sineMovement}) transition.to (purpleFish,{time = 5000,y = Amplitude\*math.sin(2\*3.14-5) ,onComplete = sineMovement}) end

Hi @bogdanmocanu2,

This will be much easier if you just use Corona’s built in sine easing, and a steady x movement across the screen. I worked up this bit of code, which you can adjust as needed to alter the height of the sine curve, the x movement rate, etc. It also uses the “tag” parameter for all transitions, so you can pause/resume/cancel this process using one tag name.

[lua]

local dot = display.newCircle( 0, 500, 10 )

local direction = “up”

local xRate = 80

local sineCurveHeight = 200

local sineCurveTime = 1000

local function doTrans( div )

   if ( direction == “up” ) then

      transition.to( dot, { time=sineCurveTime, y=dot.y-sineCurveHeight, tag=“sineMove”, transition=easing.inOutSine })

      transition.to( dot, { time=sineCurveTime, x=dot.x+xRate, tag=“sineMove”, onComplete=doTrans } )

      direction = “down”

   else

      transition.to( dot, { time=sineCurveTime, y=dot.y+sineCurveHeight, tag=“sineMove”, transition=easing.inOutSine })

      transition.to( dot, { time=sineCurveTime, x=dot.x+xRate, tag=“sineMove”, onComplete=doTrans } )

      direction = “up”

   end

end

doTrans()

[/lua]

Take care,

Brent

Hi Brent,

  Thank you for the code.It works like charm! :wink:

But when I’m switching to another level,it gives me an error that says “trying to upvalue ‘pupleFish’(a nil value)”.I set purpleFish to not getting removed once the level is completed and my problem was fixed,is it ok?

My second issue is that when my object exit the view there is no way bringing it back to where it started,if I do that the sine curve gets broke.I think I found a way,but first I have to remove doTrans() function first.How I do that?

Cheers,

Bogdan

Hi Bogdan,

The errors you’re seeing are simply related to Lua object reference and scoping. Before you change scenes (and before the fish is removed), you should cancel the transition cycle. This is why I assigned it a tag of “sineMove”. So basically, before you change scenes, do this:

[lua]

transition.cancel( “sineMove” )

[/lua]

Also, in the function “doTrans()” that I provided, add this to the beginning of the function’s code:

[lua]

   if ( purpleFish == nil ) then

      return

   end

[/lua]

Brent

Hi @bogdanmocanu2,

This will be much easier if you just use Corona’s built in sine easing, and a steady x movement across the screen. I worked up this bit of code, which you can adjust as needed to alter the height of the sine curve, the x movement rate, etc. It also uses the “tag” parameter for all transitions, so you can pause/resume/cancel this process using one tag name.

[lua]

local dot = display.newCircle( 0, 500, 10 )

local direction = “up”

local xRate = 80

local sineCurveHeight = 200

local sineCurveTime = 1000

local function doTrans( div )

   if ( direction == “up” ) then

      transition.to( dot, { time=sineCurveTime, y=dot.y-sineCurveHeight, tag=“sineMove”, transition=easing.inOutSine })

      transition.to( dot, { time=sineCurveTime, x=dot.x+xRate, tag=“sineMove”, onComplete=doTrans } )

      direction = “down”

   else

      transition.to( dot, { time=sineCurveTime, y=dot.y+sineCurveHeight, tag=“sineMove”, transition=easing.inOutSine })

      transition.to( dot, { time=sineCurveTime, x=dot.x+xRate, tag=“sineMove”, onComplete=doTrans } )

      direction = “up”

   end

end

doTrans()

[/lua]

Take care,

Brent

Hi Brent,

  Thank you for the code.It works like charm! :wink:

But when I’m switching to another level,it gives me an error that says “trying to upvalue ‘pupleFish’(a nil value)”.I set purpleFish to not getting removed once the level is completed and my problem was fixed,is it ok?

My second issue is that when my object exit the view there is no way bringing it back to where it started,if I do that the sine curve gets broke.I think I found a way,but first I have to remove doTrans() function first.How I do that?

Cheers,

Bogdan

Hi Bogdan,

The errors you’re seeing are simply related to Lua object reference and scoping. Before you change scenes (and before the fish is removed), you should cancel the transition cycle. This is why I assigned it a tag of “sineMove”. So basically, before you change scenes, do this:

[lua]

transition.cancel( “sineMove” )

[/lua]

Also, in the function “doTrans()” that I provided, add this to the beginning of the function’s code:

[lua]

   if ( purpleFish == nil ) then

      return

   end

[/lua]

Brent