谈谈我喜欢的编码方式(lua代码)

以前喜欢这样用:

	--上栏
	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);

一个变量不用定义,代码简洁,不多余。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值