So I have an object in my game which I want it to wobble like a floating balloon.
Here is what I mean:
I see there is some easing effects for transitions but I dont think it will work.
Any ideas?
So I have an object in my game which I want it to wobble like a floating balloon.
Here is what I mean:
I see there is some easing effects for transitions but I dont think it will work.
Any ideas?
Firstly you have to define how the balloon need to move.
If you want to have your own special way you can use enterframe event but I think transition will be enought
local function onComplete1( obj ) transition.to( obj, { time=1500, x=20, transition=easing.continuousLoop,onComplete=onComplete1} ) end local function onComplete2( obj ) transition.to( baloon, {time=3000, y=40, transition=easing.continuousLoop,onComplete=onComplete2} ) end onComplete1(baloon) local function listener( event ) onComplete2(baloon) end timer.performWithDelay( 1500, listener )
Does it answer to your problem?
If I see well, the balloon draw a kind of 8
Use sine waves.
Create an enterframe event function. In this function do this:
balloon.x = math.sin(event.time/xSpeed)*xAmplitude
balloon.y = math.sin(event.time/ySpeed)*yAmplitude
Higher xSpeed and ySpeed values mean slower movement.
Higher xAmplitude and yAmplitude values mean bigger movement.
Can I suggest a small change to remi’s solution? As it stands, that won’t work because of a visibility issue. Also, I think remi mean to swap onComplete listeners.
This is the change:
function obj.onComplete1( self ) self.onComplete = self.onComplete2 transition.to( self, { time = 1500, x = 20, transition = easing.continuousLoop, onComplete = self } ) end function obj.onComplete2( self ) self.onComplete = self.onComplete1 transition.to( self, {time = 3000, y = 40, transition = easing.continuousLoop, onComplete = self } ) end self.onComplete = self.onComplete1 balloon:onComplete() -- This now runs in a loop on its own
If I misunderstood the code, apologies, but the swapping onComplete call is a standard construct and that is how I usually implement it so it is self-contained and self-triggering.
local frameLoop = function(event) balloon.x = math.sin(event.time/100)\*20 balloon.y = math.sin(event.time/200)\*40 end -- frameLoop() Runtime:addEventlistener("enterframe", frameLoop)
Now doesn’t that look twice as elegant?
Bugger… I see now that remi’s solution is to run 2 transitions, not swapping. However if I read it right the balloon won’t move after the first call to each transition?
I’m a little confused about the goal here. Is this balloon affected by gravity or is it just a display object with no body?
@thomas6 - That is elegant, but it lacks that organic feel of random wind gusts I think he is going for.
That said, it is a super solution for fixed sinusoidal motions.
Regardless, every answer thus far as been interesting and awesome.
I think if you combined that with a random time element it would be perfect.
May contain typos:
local frameLoop = function(event) balloon.dx = math.sin(event.time/100)\*20 balloon.dy = math.sin(event.time/200)\*40 end Runtime:addEventlistener("enterframe", frameLoop) balloon.x0 = balloon.x balloon.y0 = balloon.y local mRand = math.random local move move = function() balloon.x = balloon.x0 + balloon.dx balloon.y = balloon.y0 + balloon.dy timer.performWithDelay( mRand(1,150), move ) end move()
This may be too jerky, but I hope you see where I’m going with this.
True, but the random wind gusts are not what was shown in the example!
Anyway, if you want random gusts, it’s just a matter of tweaking the code to add one or two more frequencies, and add these together.
Cool. Hopefully none of my responses seemed critical to anyone. I’m just interested in the discussion.
Firstly you have to define how the balloon need to move.
If you want to have your own special way you can use enterframe event but I think transition will be enought
local function onComplete1( obj ) transition.to( obj, { time=1500, x=20, transition=easing.continuousLoop,onComplete=onComplete1} ) end local function onComplete2( obj ) transition.to( baloon, {time=3000, y=40, transition=easing.continuousLoop,onComplete=onComplete2} ) end onComplete1(baloon) local function listener( event ) onComplete2(baloon) end timer.performWithDelay( 1500, listener )
Does it answer to your problem?
If I see well, the balloon draw a kind of 8
Use sine waves.
Create an enterframe event function. In this function do this:
balloon.x = math.sin(event.time/xSpeed)*xAmplitude
balloon.y = math.sin(event.time/ySpeed)*yAmplitude
Higher xSpeed and ySpeed values mean slower movement.
Higher xAmplitude and yAmplitude values mean bigger movement.
Can I suggest a small change to remi’s solution? As it stands, that won’t work because of a visibility issue. Also, I think remi mean to swap onComplete listeners.
This is the change:
function obj.onComplete1( self ) self.onComplete = self.onComplete2 transition.to( self, { time = 1500, x = 20, transition = easing.continuousLoop, onComplete = self } ) end function obj.onComplete2( self ) self.onComplete = self.onComplete1 transition.to( self, {time = 3000, y = 40, transition = easing.continuousLoop, onComplete = self } ) end self.onComplete = self.onComplete1 balloon:onComplete() -- This now runs in a loop on its own
If I misunderstood the code, apologies, but the swapping onComplete call is a standard construct and that is how I usually implement it so it is self-contained and self-triggering.
local frameLoop = function(event) balloon.x = math.sin(event.time/100)\*20 balloon.y = math.sin(event.time/200)\*40 end -- frameLoop() Runtime:addEventlistener("enterframe", frameLoop)
Now doesn’t that look twice as elegant?
Bugger… I see now that remi’s solution is to run 2 transitions, not swapping. However if I read it right the balloon won’t move after the first call to each transition?
I’m a little confused about the goal here. Is this balloon affected by gravity or is it just a display object with no body?
@thomas6 - That is elegant, but it lacks that organic feel of random wind gusts I think he is going for.
That said, it is a super solution for fixed sinusoidal motions.
Regardless, every answer thus far as been interesting and awesome.
I think if you combined that with a random time element it would be perfect.
May contain typos:
local frameLoop = function(event) balloon.dx = math.sin(event.time/100)\*20 balloon.dy = math.sin(event.time/200)\*40 end Runtime:addEventlistener("enterframe", frameLoop) balloon.x0 = balloon.x balloon.y0 = balloon.y local mRand = math.random local move move = function() balloon.x = balloon.x0 + balloon.dx balloon.y = balloon.y0 + balloon.dy timer.performWithDelay( mRand(1,150), move ) end move()
This may be too jerky, but I hope you see where I’m going with this.
True, but the random wind gusts are not what was shown in the example!
Anyway, if you want random gusts, it’s just a matter of tweaking the code to add one or two more frequencies, and add these together.