While working on a project I wanted a way to trigger two or more tweens at once in a sequence. The following function is my solution. I am sure the following has been done before but searched already to find no results.
-- Trigger two or more tweens at once in a sequence
-- Syntax:
-- tween.pair({t1, t2, t3...})
function tween.pair(tweens)
for i,t in pairs(tweens) do
tween.sequence(t)
end
end
I know using tween.sequence() is not exactly the most creative aproach, but I don’t fully understand how tweens work and it seemed the easiest way to me. I am open for better ways to do this.
I also wanted to make the syntax simpler using the arg table described in @Ignatz 's book 'Lua for beginners but got an error when I used this. If anyone can explain why or how to fix this I am open for it.
function foo(...)
-- get the number of args
local n = select('#', ...)
-- make a table out of the args
local arg = {...}
if n > 0 then
print(arg[1])
end
end
@jaj_TheDeveloper Here’s something I wrote some time ago that shows multiple tweens being started and running at the same time. There are 768 tweens, one for each square. Tap the screen to start them in motion. It’s set to run for 15 seconds.
supportedOrientations(PORTRAIT_ANY)
displayMode(FULLSCREEN)
function setup()
spriteMode(CORNER)
tab={}
img=readImage("Cargo Bot:Startup Screen")
w=img.width/24
h=img.height/32
for x=1,24 do
for y=1,32 do
table.insert(tab,img:copy((x-1)*32+1,(y-1)*32+1,32,32))
end
end
twTab={}
count=0
for x=1,24 do
for y=1,32 do
count=count+1
table.insert(twTab,
twn(15,math.random(WIDTH),math.random(HEIGHT),
(x-1)*32+1,(y-1)*32+1,count))
end
end
end
function draw()
background(0)
for a,b in pairs(twTab) do
b:draw()
end
end
function touched(t)
if t.state==BEGAN then
for a,b in pairs(twTab) do
b:start()
end
end
end
twn=class()
function twn:init(t,xs,ys,xe,ye,nbr)
self.col=color(math.random(255),math.random(255),math.random(255))
self.starts={x=xs,y=ys}
self.ends={x=xe,y=ye}
self.time=t
self.nbr=nbr
end
function twn:start()
tween(self.time,self.starts,self.ends,tween.easing.elasticInInOut)
end
function twn:draw()
fill(self.col)
sprite(tab[self.nbr],self.starts.x,self.starts.y)
end