以前喜欢这样用:
--上栏
local topBar = UIImageBox.new("speed_shanglan");
topBar:align(display.LEFT_TOP, 0, display.height);
self:addChild(topBar);
--下栏
local bottomBar = UIImageBox.new("speed_xialan")
bottomBar:align(display.BOTTOM_LEFT, 0, 0);
self:addChild(bottomBar);
--历史纪录
local best = UIImageBox.new("speed_best")
best:align(display.BOTTOM_RIGHT, display.width - 20, display.height - 40);
self:addChild(best);
--历史纪录得分
local bestScore = UIBMFontLabel.new({
font = "white.fnt",
text = tostring(PlayerMgr.historicRecord) .. "kg"
});
bestScore:align(display.BOTTOM_RIGHT, display.width - 20, display.height - 80);
self:addChild(bestScore);
--标题
local title = UIImageBox.new("speed_biaoti")
title:align(display.CENTER, display.cx, display.cy + display.height / 4)
self:addChild(title);
--开始按钮
local startBut = UIButton.new("speed_start", "speed_start", {
onClick = function()
OpenScene(MainScene, nil, "fade", 0.6, display.COLOR_WHITE);
end});
startBut:align(display.CENTER, display.cx, display.cy - 180);
self:addChild(startBut);
感觉这样做很整洁,但唯一让我头痛的是,每一个控件都要定义一个变量名,为什么非得要定义一个变量来处理呢,处理完了这个变量也没用了,而每一次又要定义不同名字,然后我就这样做:
--上栏
local uiTemp = UIImageBox.new("speed_shanglan");
uiTemp:align(display.LEFT_TOP, 0, display.height);
self:addChild(uiTemp);
--下栏
uiTemp = UIImageBox.new("speed_xialan")
uiTemp:align(display.BOTTOM_LEFT, 0, 0);
self:addChild(uiTemp);
--历史纪录
uiTemp = UIImageBox.new("speed_best")
uiTemp:align(display.BOTTOM_RIGHT, display.width - 20, display.height - 40);
self:addChild(uiTemp);
--历史纪录得分
uiTemp = UIBMFontLabel.new({
font = "white.fnt",
text = tostring(PlayerMgr.historicRecord) .. "kg"
});
uiTemp:align(display.BOTTOM_RIGHT, display.width - 20, display.height - 80);
self:addChild(uiTemp);
--标题
uiTemp = UIImageBox.new("speed_biaoti")
uiTemp:align(display.CENTER, display.cx, display.cy + display.height / 4)
self:addChild(uiTemp);
--开始按钮
uiTemp = UIButton.new("speed_start", "speed_start", {
onClick = function()
OpenScene(MainScene, nil, "fade", 0.6, display.COLOR_WHITE);
end});
uiTemp:align(display.CENTER, display.cx, display.cy - 180);
self:addChild(uiTemp);
这样做就避免定义很多变量,好像解决了我以前遇到定义变量的问题,但总觉得这样不是最好的,一直在需求简洁代码方式的我,能不能一个都不定义,看到前辈的代码方式,于是这种代码出现了,也是我最喜欢的代码方式。
--上栏
UIImageBox.new("speed_shanglan")
:align(display.LEFT_TOP, 0, display.height)
:addTo(self);
--下栏
UIImageBox.new("speed_xialan")
:align(display.BOTTOM_LEFT, 0, 0)
:addTo(self);
--历史纪录
UIImageBox.new("speed_best")
:align(display.BOTTOM_RIGHT, display.width - 20, display.height - 40)
:addTo(self);
--历史纪录得分
UIBMFontLabel.new({
font = "white.fnt",
text = tostring(PlayerMgr.historicRecord) .. "kg"
})
:align(display.BOTTOM_RIGHT, display.width - 20, display.height - 80)
:addTo(self);
--标题
UIImageBox.new("speed_biaoti")
:align(display.CENTER, display.cx, display.cy + display.height / 4)
:addTo(self);
--开始按钮
UIButton.new("speed_start", "speed_start", {
onClick = function()
OpenScene(MainScene, nil, "fade", 0.6, display.COLOR_WHITE);
end})
:align(display.CENTER, display.cx, display.cy - 180)
:addTo(self);
一个变量不用定义,代码简洁,不多余。