--reset animations in 0ms to pos 0/0
ActiveAnimations["tunnel0"]:to(0, { VAnimationCurrentPosition = {x = 0, y = 0} })
ActiveAnimations["tunnel1"]:to(0, { VAnimationCurrentPosition = {x = 0, y = 0} })
ActiveAnimations["tunnel2"]:to(0, { VAnimationCurrentPosition = {x = 0, y = 0} })
--randomize a number between 0 and 2
local randomtunnel = math.random(0,2)
--move random tunnel animation to other side of the screen in 500ms (assuming x=2000 is outside the view)
ActiveAnimations["tunnel"..randomtunnel]:to(500, { VAnimationCurrentPosition = {x = 2000, y = 0} })
1
2
3
4
5
6
7
8
scUW = {}
scUW.tilewidth = 913 -- width of a tiles image
scUW.numtiles = math.ceil(game.WindowResolution.x / scUW.tilewidth)
scUW.tiles0 = {"tunnel0a","tunnel0b"} -- animations with image0
scUW.tiles1 = {"tunnel1a","tunnel1b"} -- animation with iamge1
scUW.tiles2 = {"tunnel2"} -- animations with image2
scUW.offset = 0
scUW.ordering = {"tunnel0a","tunnel0b","tunnel2"}
--[[ doesnt move, just for testing ]]--
scUW.setBackground = function(self)
local pos = 0
for _,t in ipairs(self.ordering) do
ActiveAnimations[t]:to(0, { VAnimationCurrentPosition =
{x = self.offset + self.tilewidth * pos, y = 0} })
pos = pos + 1
end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
scUW.ordering = {"tunnel0a","tunnel1a","tunnel2"}
1
scUW.ordering = {"tunnel0a","tunnel1a","tunnel0b"}
1
local pos = 0
for _,t in ipairs(self.active) do
local ani = getObject("Animations["..t.."]")
startAnimation(ani)
ActiveAnimations[t]:to(0, { VAnimationCurrentPosition =
{x = self.offset + self.tilewidth * pos, y = 0} })
pos = pos + 1
end
1
2
3
4
5
6
7
8
ActiveAnimations[t].AnimationCurrentPosition = {…}?
1
ActiveAnimations[t]:to(0, { VAnimationCurrentPosition =
{x = self.tilewidth * pos, y = 0} })
ActiveAnimations[t]:to(self.tick, { VAnimationCurrentPosition =
{x = self.tilewidth * (pos - 1), y = 0} })
1
2
3
4
math.randomseed( os.time() )
-- unfuck luas base-1 bullshit
function iarray(a)
local n = 0
local s = #a
if a[0] ~= nil then
n = -1
end
return function()
n = n + 1
if n <;= s then return n, a[n] end
return nil,nil
end
end
QP_createTiledBackground = function(tilewidth, tick, aBasetiles, aAlt1, aAlt2, chance2)
ret = {}
ret.scWidth = game.CurrentScene.Sprite:getSize().x
ret.numTiles = math.ceil(ret.scWidth / tilewidth)
ret.offset = 0
ret.tilewidth = tilewidth
ret.chance2 = chance2 * 100
ret.isInit = false
ret.tick = tick
ret.tiles = {}
ret.ordering = {}
local pos = 0
for _,t in iarray(aBasetiles) do
ret.tiles[pos] = t
pos = pos + 1
end
ret.numBases = pos
for _,t in iarray(aAlt1) do
ret.tiles[pos] = t
pos = pos + 1
end
ret.startAlt2 = pos
ret.numAlt1 = pos - ret.numBases
for _,t in iarray(aAlt2) do
ret.tiles[pos] = t
pos = pos + 1
end
ret.numAlt2 = pos - ret.numBases - ret.numAlt1
-- move all animation-tiles outsize the viewport
ret.initBackground = function(self)
if (self.isInit) then
return end
self.isInit = true
self.offset = 0
for _,t in iarray(self.tiles) do
local ani = getObject("Animations["..t.."]")
startAnimation(ani)
print (t)
ActiveAnimations[t]:to(0, { AnimationCurrentPosition =
{x = self.scWidth, y = 0} })
end
end
-- check if a tile is already used in the ordering, skipping it
ret.findTile = function(self, tile)
for _,t in iarray(self.ordering) do
if (t == tile) then return _ end
end
return -1
end
-- return an appropiate tile from given range or nil if none available
ret.getNextTile = function(self, start, stop)
local pos = start
while (pos = math.random(0,100)) then
self.ordering[pos] = self:getNextTile(self.startAlt2,#(self.tiles)+1)
else
self.ordering[pos] = self:getNextTile(self.numBases, self.startAlt2)
end
end
end
off = off + 1
if (self.ordering[pos] ~= nil) then
pos = pos + 1
end
if (off >; 20) then break end -- reasonable way out if to few tiles given
end
end
ret.moveBackground = function(self)
self:initBackground()
self:orderBackground()
local pos = 0
for _,t in iarray(self.ordering) do
ActiveAnimations[t]:to(0, { VAnimationCurrentPosition =
{x = self.tilewidth * pos, y = 0} })
ActiveAnimations[t]:to(self.tick, { VAnimationCurrentPosition =
{x = self.tilewidth * (pos - 1), y = 0} })
pos = pos + 1
end
end
return ret
end
scUW = QP_createTiledBackground(913, 1500, {"tunnel0a","tunnel0b"}, {"tunnel1a","tunnel1b"}, {"tunnel2"}, 0.5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
That doesnt do anything. BTW, you misinterpreted the code, i dont move it pixelwise, a tick is 1500 ms and in that time all tiles should move one tilewidth. Its just that the zero-delay-commands sometimes seem to get ignored.ActiveAnimations[t].CurrentPosition = {x=something, y=something}