-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindowscroller.lua
More file actions
131 lines (101 loc) · 2.49 KB
/
windowscroller.lua
File metadata and controls
131 lines (101 loc) · 2.49 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
-- WindowScroller
-- Mark Carolan 2010
-- Scroll an image 'strip" within a window
-- Warning: no error checking on sprite sheet parameters
module(..., package.seeall)
sprite = require "sprite"
horiz = 1
vert = 2
function newScroller(imageFile, imageSlices, startSlice,
endSlice, winWidth, dir, imgWidth, imgHeight )
assert(imgWidth > 1)
assert(imgHeight > 1)
local t = display.newGroup()
t.seq_start = startSlice
t.seq_end = endSlice
t.seq_len = 1 + endSlice-startSlice
t.slices = imageSlices
t.window = winWidth
t.fWidth = imgWidth
t.fHeight = imgHeight
--[[ -- corona bug #1808
-- uncomment and ignore imgWidth, imgHeight params
-- when bug is fixed
do
local testFile = display.newImage(imageFile)
t.fWidth = testFile.width
t.fHeight = testFile.height
testFile.isVisible = false
testFile = nil -- shouldn't be necessary
end
--]]
print(imageFile .. " width, height = " .. t.fWidth .. ", " .. t.fHeight)
t.slice_size = t.fWidth / t.slices
print("sequence of " .. t.seq_len .. " slices of " .. t.slice_size)
t.sheet = sprite.newSpriteSheet(imageFile, t.slice_size, t.fHeight)
t.spriteSet = sprite.newSpriteSet(t.sheet, t.seq_start, t.seq_len)
for i = 1, t.slices do
local instance = sprite.newSprite(t.spriteSet)
instance.currentFrame = i
instance.x = (i-1) * t.slice_size
if instance.x >= t.window then
instance.isVisible = false
end
t:insert(instance)
end
function t:setPos(xPos, yPos)
self.x = xPos
self.y = yPos
end
function t:nextFrame()
local num_frames = self.numChildren
local last_frame = self[num_frames]
for i = num_frames, 2, -1 do
local f = self[i]
local prev = self[i-1]
f.x = prev.x
f.y = prev.y
if f.x >= self.window then
f.isVisible = false
else
f.isVisible = true
end
end
local f = self[1]
f.x = last_frame.x
f.y = last_frame.y
if f.x >= self.window then
f.isVisible = false
else
f.isVisible = true
end
end
function t:prevFrame()
local num_frames = self.numChildren
local first_frame = self[1]
for i = 1, num_frames-1 do
local f = self[i]
local nxt = self[i+1]
f.x = nxt.x
f.y = nxt.y
if f.x >= self.window then
f.isVisible = false
else
f.isVisible = true
end
end
local f = self[num_frames]
f.x = first_frame.x
f.y = first_frame.y
if f.x >= self.window then
f.isVisible = false
else
f.isVisible = true
end
end
t.xReference = winWidth/2
if dir == vert then
t:rotate(90)
end
return t
end