10000 从demoZA1_AnimateFire和demoZA2_AnimateFire2中抽离candle模块 · pythonzz/LuaViewSDK@020f527 · GitHub
[go: up one dir, main page]

Skip to content

Commit 020f527

Browse files
committed
从demoZA1_AnimateFire和demoZA2_AnimateFire2中抽离candle模块
1 parent d7fb0a9 commit 020f527

File tree

7 files changed

+197
-280
lines changed

7 files changed

+197
-280
lines changed
File renamed without changes.
File renamed without changes.

IOS/Demo/lua/candle/init.lua

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
local candle = {
2+
bodyImage = "candle/lazhu.png",
3+
fireStartImage = "candle/color1.png",
4+
fireEndImage = "candle/color2.png",
5+
fireCount = 20
6+
}
7+
8+
local r = 40
9+
10+
function candle.new(x, y, count)
11+
local instance = {}
12+
13+
instance.view = View()
14+
instance.view.frame(x, y, 90, 180)
15+
16+
instance.body = Image()
17+
instance.body.image(self.bodyImage)
18+
instance.body.adjustSize()
19+
instance.body.frame(13, 70, 64, 104)
20+
instance.view.addView(instance.body)
21+
22+
instance.fires = {}
23+
if count == nil then
24+
count = self.fireCount
25+
end
26+
for i = 1, count do
27+
instance.fires[i] = self.createFire(45, 63)
28+
instance.view.addView(instance.fires[i].bg)
29+
end
30+
31+
function instance.start()
32+
if self.timer ~= nil or self.fires[1].animating then
33+
return
34+
end
35+
36+
local index = 1
37+
self.timer = Timer(
38+
function()
39+
if index < #instance.fires then
40+
self.fires[index].animating = true
41+
self.fires[index].showfires()
42+
index = index + 1
43+
else
44+
self.timer.cancel()
45+
self.timer = nil
46+
end
47+
end
48+
)
49+
50+
self.timer.start(0.1, true)
51+
end
52+
53+
function instance.stop()
54+
if self.timer then
55+
self.timer.stop()
56+
self.timer = nil
57+
end
58+
59+
for i, v in ipairs(self.fires) do
60+
v.animating = false
61+
end
62+
end
63+
64+
function instance.move(dx, dy)
65+
local x, y = self.view.center()
66+
self.view.center(x + dx, y + dy)
67+
end
68+
69+
return instance
70+
end
71+
72+
function candle.createFire(x, y)
73+
local fire = {}
74+
fire.animating = false
75+
76+
fire.imageView1 = Image()
77+
fire.imageView2 = Image()
78+
fire.imageView1.image(self.fireStartImage)
79+
fire.imageView2.image(self.fireEndImage)
80+
fire.imageView1.frame(0,0,r*2,r*2)
81+
fire.imageView2.frame(0,0,r*2,r*2)
82+
83+
fire.bg = View()
84+
fire.bg.frame(0,0,r*2,r*2)
85+
fire.bg.addView(fire.imageView1)
86+
fire.bg.addView(fire.imageView2)
87+
88+
function fire.resetViews()
89+
self.bg.scale(1, 1)
90+
self.bg.size( r*2, r*2)
91+
self.bg.alpha( 0.5)
92+
93+
local x0 = math:random(x, x + r*0.1)
94+
local y0 = math:random(y, y + r*0.3)
95+
96+
self.bg.center(x0,y0)
97+
self.x = x0
98+
self.y = y0
99+
100+
self.imageView1.alpha( 1)
101+
self.imageView2.alpha( 0)
102+
end
103+
104+
function fire.randomPositionDelta()
105+
local len = 30
106+
local dx = math:random(-len,len)
107+
local maxDy = math:sqrt( (len*len*2 - dx*dx) )*2
108+
local dy = math:random( -maxDy, 0 )
109+
110+
return dx, dy
111+
end
112+
113+
function fire.showfires()
114+
self.resetViews()
115+
116+
local x, y = self.bg.center()
117+
local dx, dy = self.randomPositionDelta()
118+
local time = math:random(15,20)/10.0
119+
Animate(time,
120+
function ()
121+
self.bg.scale( 0.2, 0.4 )
122+
self.bg.center(x + dx, y + dy)
123+
self.imageView1.alpha(0)
124+
self.imageView2.alpha(1)
125+
self.bg.alpha(0)
126+
end,
127+
function ()
128+
if self.animating then
129+
self.showfires()
130+
end
131+
end
132+
)
133+
end
134+
135+
fire.bg.alpha(0)
136+
137+
return fire
138+
end
139+
140+
return candle
File renamed without changes.

IOS/Demo/lua/demoZA1_AnimateFire.lua

Lines changed: 3 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,3 @@
1-
2-
scrW, scrH = System.screenSize();
3-
4-
window.frame(0, 0, scrW, scrH);
5-
window.backgroundColor(0xffffff,1);
6-
window.enabled(true);
7-
8-
9-
10-
local percent = 1;
11-
local r = 40*percent;
12-
13-
local bodyX0 = scrW/2;
14-
local bodyY0 = scrH/2;
15-
16-
lazhuBody = Image();
17-
lazhuBody.frame( bodyX0 - 64*0.45, bodyY0 + r*0.5, 64, 104);
18-
lazhuBody.image("lazhu.png");
19-
20-
21-
-------------------------------
22-
function fireCreater()
23-
local fire = {};
24-
fire.times = 0;
25-
26-
fire.imageView1 = Image();
27-
fire.imageView2 = Image();
28-
fire.imageView1.image("color1.png");
29-
fire.imageView2.image("color2.png");
30-
fire.imageView1.frame(0,0,r*2,r*2);
31-
fire.imageView2.frame(0,0,r*2,r*2);
32-
33-
fire.bg = View();
34-
fire.bg.frame(0,0,r*2,r*2);
35-
fire.bg.addView(fire.imageView1);
36-
fire.bg.addView(fire.imageView2);
37-
38-
function fire.initX0Y0()
39-
self.bg.scale(1, 1);
40-
self.bg.size( r*2, r*2);
41-
self.bg.alpha( 0.5);
42-
43-
local x0 = math:random(bodyX0, bodyX0 + r*0.1);
44-
local y0 = math:random(bodyY0, bodyY0 + r*0.3);
45-
46-
self.bg.center(x0,y0);
47-
self.x = x0;
48-
self.y = y0;
49-
50-
self.imageView1.alpha( 1);
51-
self.imageView2.alpha( 0);
52-
end
53-
54-
function fire.move()
55-
self.bg.center( self.x, self.y );
56-
self.bg.scale( 0.2, 0.4 );
57-
self.imageView1.alpha(0);
58-
self.imageView2.alpha(1);
59-
self.bg.alpha(0);
60-
end
61-
62-
function fire.nextXYAndColor()
63-
local len = 30*percent;
64-
local dx = math:random(-len,len);
65-
local maxDy = math:sqrt( (len*len*2 - dx*dx) )*2;
66-
local dy = math:random( -maxDy, 0 );
67-
local x,y = self.bg.center();
68-
self.x = x+dx;
69-
self.y = y+dy;
70-
end
71-
72-
function fire.showfires()
73-
self.initX0Y0();
74-
self.nextXYAndColor();
75-
76-
local time = math:random(15,20)/10.0;
77-
Animate(time,
78-
function ()
79-
self.move();
80-
end
81-
,
82-
function ()
83-
self.showfires();
84-
end
85-
);
86-
end
87-
88-
return fire;
89-
end
90-
-------------------------------------
91-
fireArr = {};
92-
93-
index = 1;
94-
fireTimer = Timer(
95-
function()
96-
if (index<20 ) then
97-
index = index+1;
98-
fireArr[index] = fireCreater();
99-
fireArr[index].showfires();
100-
else
101-
fireTimer.cancel();
102-
end
103-
end
104-
);
105-
106-
fireTimer.start(0.1, true);
107-
108-
1+
candle = require("candle.init")
2+
instance = candle.new(100, 100, 200, 300)
3+
instance.start()

0 commit comments

Comments
 (0)
0