The mystery deepens…
@epicurus101, @Jmv38,@yojimbo2000 -
Actually, the problem seems to be the opposite, as you can see if you change my code to this
function setup()
a={b=0}
c={b=0} --<-- b instead of d (throughout the code)
t1=tween( 3, a, { b=200 }, tween.easing.quadInOut )
t2=tween( 3, c, { b=200 }, tween.easing.quadInOut )
tween.sequence(t1,t2)
end
function draw()
background(100)
fill(255,0,0)
ellipse(400,200+a.b,100)
fill(255,255,0)
ellipse(500,200+c.b,100)
end
When the tween targets are different, but the tweened item has the same name, it doesn’t work properly.
@Ignatz Haha, I was trying all sorts of ways to break your code, but couldn’t. Well, I think you have solved it. Pretty weird behaviour though. It seems that Codea’s tween API forked this https://github.com/kikito/tween.lua , so we can see how easing works, but it would be useful to be able to see how things like sequence and looping work. Of course I can understand why the developers wouldn’t want to open source bits of their APIs
When the 2 Tweens are created, their initial values are set at the point of creation in setup(). When tween t1 runs, the variable ‘b’ goes from 0 to 200 over a period of 3 seconds. When tween t2 runs, the variable ‘b’ doesn’t get set back to 0 because it was set to 0 in setup and that doesn’t get run again. Since ‘b’ is sitting at 200 from the ‘t1’ run, that’s the value tween ‘t2’ sees it at.
I don’t think that’s the problem, because the tweens are supposed to store the table addresses and item names. If this is done correctly, the two b’s above will be correctly recognised as belonging to different tables.
Somehow, tween is getting confused and thinking the second b is the same as the first.
I think we have enough to file a bug report.
@Simeon if you run the two code samples immediately above, you’ll see the problem with the second one.
And if you feel like decoding, @dave1707, John’s tween code, including tween sequence, appears to be here in draft form
http://codea.io/talk/discussion/1195/cocoscodea
(But I’m going to bed. It’s late in Australia!)
@Ignatz thanks for that link, I hadn’t seen that before. It’s interesting that looping used to be implemented differently, with a tween.loop
function that you call the same way as tween.sequence
. It looks like you used to be able to specify how many times it repeated too, making my tween.loopN
function above redundant. If I have time, I’ll see if I can add the old version of tween.loop
to a project
Hi all. I had the same problem with some code of mine. I wanted to change the opacity of some text on screen with a simple fade in from 0 to 100% then a delay then a fade out back to 0%.
This wasted sooooo much of my time. I should have searched for this discussion first but in a way it was good I sussed it on my own. I ended up avoiding sequences, which appears broken on the face of it. I simple nested the call backs which is confusing to read but is OK for short sequences. I like tweens in general but it has rough edges like this sometimes
My own resolved code:
tween(0.5, self, {opacity=100}, tween.easing.linear, function() tween.delay(2, function() tween(0.5, self, {opacity=0}, tween.easing.linear, function() last_callback end) end) end)
If you’re not happy with Tween callback functions, you can roll your own by taking advantage of the running
and time
variable that each tween has. When a tween has ended, running
will be >= to time
:
if self.tween and self.tweenAction and self.tween.running>=self.tween.time then
self.tween=nil
self.tweenAction(self)
end
where self.tween is the id of the tween, and self.tweenAction is a function that is part of the class
@todddixon Here’s some code to cause text to fade in for 3 seconds, show for 3 seconds, then 3 seconds to fade out. Tap screen to start sequence.
displayMode(FULLSCREEN)
function setup()
tw={tm=0}
end
function draw()
background(0)
fontSize(17)
fill(255)
text("tap scren for",WIDTH/2,HEIGHT-200)
text("3 sec fade in, 3 sec show, 3 sec fade out",WIDTH/2,HEIGHT-250)
fontSize(40)
fill(255,255,255,tw.tm)
text("Show some text to fade in fade out",WIDTH/2,HEIGHT/2)
end
function touched(t)
if t.state==BEGAN then
t1=tween(3,tw,{tm=255})
t2=tween(3,tw,{tm=255})
t3=tween(3,tw,{tm=0})
tween.sequence(t1,t2,t3)
end
end