cocos2dx 3.0 之 lua 创建类

时间:2021-09-17 09:30:38

利用3.0里面封装好的模板类来写,不罗嗦,直接上例子(消除星星小游戏中 星星类)

require "extern"

StarSprite = class("StarSprite",function(fileName) return cc.Sprite:create(fileName)

end
)

StarSprite.__index = StarSprite

StarSprite.type = 0

--around color
StarSprite.UP = "Empty"    -- 定义成员变量 在外部可以调用
StarSprite.DOWN = "Empty"
StarSprite.LEFT = "Empty"
StarSprite.RIGHT = "Empty"

--self color
StarSprite.m_selfcolor = "Empty"

--pos in 10*10
StarSprite.m_Matrix_x = -1
StarSprite.m_Matrix_y = -1

--tag
StarSprite.m_tag = -1

--Be already Searched 1 找到了 2 没有找到
StarSprite.m_BeSearched = 2

function StarSprite:createByType(typenum,_type)  -- 创建实例对象 
local buf
buf = "star_type_"..typenum..".png" 
local star = StarSprite.new(buf)
--star:setScale(0.4)
star:Init(typenum,_type)

return star
end

function StarSprite:Init(typenum,_type) -- 初始化
self.type = _type
if typenum == 1 then
self.m_selfcolor = "green"
elseif typenum == 2 then
self.m_selfcolor = "blue"
elseif typenum == 3 then
self.m_selfcolor = "red"
elseif typenum == 4 then
self.m_selfcolor = "purple"
elseif typenum == 5 then
self.m_selfcolor = "yellow"
else
self.m_selfcolor = "Empty"
end
self:setScale(0.4)
self.m_BeSearched = 2

end

function StarSprite:SetAroundColor(upcolor,downcolor,leftcolor,rightcolor) 
self.UP = upcolor
self.DOWN = downcolor
self.LEFT = leftcolor
self.RIGHT = rightcolor
end

function StarSprite:SetMatrixPos(centerx,centery) -- 定义成员方法 在外部可以调用
self.m_Matrix_x = centerx
self.m_Matrix_y = centery
end
function StarSprite:GetMartrixPosX()
return self.m_Matrix_x
end
function StarSprite:GetMartrixPosY()
return self.m_Matrix_y
end

function StarSprite:SetStarTag(tag)
self.m_tag = tag
end

function StarSprite:SetStarScale(scalex,scaley)
self:setScaleX(scalex)
self:setScaleY(scaley)
end