根据 https://github.com/Tencent/xLua 的教程搭建的框架
Lua面向对象相关lua 面向对象,我从云风大神那边拿了过来:
https://blog.codingnow.com/cloud/LuaOO
---扑克类
Poker = class()
function Poker:ctor(id, name, num)
self.id = id or 0 --- 扑克id
self.name = name or '' --- 扑克名称
self.num = num or 0 --- 扑克的数量
end
function Poker:ctor(id, name, num, isPlay)
self.id = id or 0 --- 扑克id
self.name = name or '' --- 扑克名称
self.num = num or 0 --- 扑克的数量
self.isPlay = isPlay or true --- 是否打出去了
end
--- 判断扑克是否打出去了, true表示打出去了, false 表示没有打出去
function Poker:isPlay()
return self.isPlay
end
function Poker:setPlay(is_play)
self.isPlay = is_play
end
--- 获取扑克id , 扑克的唯一id
function Poker:getId()
return self.id
end
--- 设置扑克id
function Poker:setId(id)
self.id = id
end
---获取牌的名称
function Poker:getName()
return self.name
end
--- 设置牌的名称
function Poker:setName(name)
self.name = name
end
使用也是很简单:
记得 require(‘class.class’) class 父类, 根据你自己存放的路径来写。
然后:
require('game_thing.Poker')
local poker = Poker.new(0, 'A', 1, true)
print(poker:getName())
将会打印:
A
我们首先来实现一个简单的功能
--- 扑克精灵类,即一张扑克的UI表现
PokerSprite = class()
local rect_transform
local transform
function PokerSprite:ctor(id, name, num)
self.id = id or 0 --- 扑克id
self.name = name or '' --- 扑克名称
self.num = num or 0 --- 扑克的数量
self.image = nil
end
function PokerSprite:ctor(id, name, num, isPlay, isUp, isSelected)
self.id = id or 0 --- 扑克id
self.name = name or '' --- 扑克名称
self.num = num or 0 --- 扑克的数量
self.isPlay = isPlay or true --- 是否打出去了
self.isUp = isUp or false --- 扑克是否翻上来
self.isSelected = isSelected or false --- 是否被选中
self.image = nil
end
function PokerSprite:getId()
return self.id
end
function PokerSprite:setId(id)
self.id = id
end
function PokerSprite:getName()
return self.name
end
function PokerSprite:setName(name)
self.name = name
end
function PokerSprite:getNum()
return self.num
end
function PokerSprite:setNum(num)
self.num = num
end
--- 判断扑克是否打出去了, true表示打出去了, false 表示没有打出去
function PokerSprite:getIsPlay()
return self.isPlay
end
function PokerSprite:setPlay(is_play)
self.isPlay = is_play
end
--- 判断牌是否往上提,表示 想出这张牌
function PokerSprite:getIsUp()
return self.isUp
end
--- true 表示往上抽牌,想出这张牌。 false 把牌抽回来,不想出这张牌
function PokerSprite:setUp(is_up)
--如果扑克已经抽了出来,又调用了抽出来, 或者 扑克没有被抽出来,又调用了放回去 。 则什么都不做
if ((self.isUp == true and is_up == true) or (self.isUp == false and is_up == false) )
then
return nil
end
self.isUp = is_up
if (self.isUp == true)
then
transform.position = CS.UnityEngine.Vector3(transform.position.x, transform.position.y + 75, transform.position.z)
else
transform.position = CS.UnityEngine.Vector3(transform.position.x, transform.position.y - 75, transform.position.z)
end
end
--- 判断牌是否被选择了
function PokerSprite:getIsSelected()
return self.isSelected
end
--- 设置牌是否选中 true 表示选择了这张牌, false表示不选这张牌
function PokerSprite:setSelected(is_selected)
self.isSelected = is_selected
end
function PokerSprite:start(image_url)
---local button = CS.UnityEngine.GameObject.Find('Canvas/Button'):GetComponent(typeof(CS.UnityEngine.UI.Button))
---button.onClick:AddListener(function ( )
---print('click me')
---end)
self.image = CS.UnityEngine.GameObject.Find(image_url):GetComponent(typeof(CS.UnityEngine.UI.Image))
---改变Image组件的宽高,使他跟牌的原始图片的宽高一致
transform = self.image.transform
rect_transform = transform:GetComponent(typeof(CS.UnityEngine.RectTransform))
local newSizeDelta = CS.UnityEngine.Vector2(105, 150)
rect_transform.sizeDelta = newSizeDelta
--默认给一张翻的牌
ResourceManager:LoadObject('Game/Doudizhu/res/poker/down', function( obj )
self.image.sprite = obj:ToSprite()
end)
---self:changeImage()
end
function PokerSprite:changeImage()
-- body
string.format("Game/Doudizhu/res/poker/%d", self.id)
ResourceManager:LoadObject(string.format("Game/Doudizhu/res/poker/%d", self.id), function( obj )
self.image.sprite = obj:ToSprite()
end)
end
function PokerSprite:turnUp()
-- body
self:changeImage()
end
function PokerSprite:turnDown()
-- body
ResourceManager:LoadObject('Game/Doudizhu/res/poker/down', function( obj )
self.image.sprite = obj:ToSprite()
end)
end
function PokerSprite:turn(isUp)
-- body
if (isUp == true)
then
self:turnUp()
else
self:turnDown()
end
end
让我们实现一个简单的功能:
ui布局
原创文章 14获赞 1访问量 2632
关注
私信
展开阅读全文
作者:Tui_GuiGe