cocos_lua 继承

local father = class(“father”,cc.Node)
function father:ctor( )
self.age = 49
self.name = “father”
end
function father:getName( )
return self.name
end
function father:ageNum()
return self.age
end
function father:say( )
print(“i am your father”,self.name,self.age)
end

return father

local father = require(“app.views.father”)
local son = class(“son”, father)
function son:ctor( )
print(“son:ctor”)
son.super.ctor(self.super)
self:getName()

end
function son:getName( )
– self.super:name()
print(self.name,self.age)
self.name = “son”
self.age = 4
print(self.name,self.age)
self:say()
end
return son


local father = require(“app.views.father”)
local gril = class(“gril”, father)
function gril:ctor( )
print(“gril:ctor”)
gril.super.ctor(self.super)
self:getName()

end
function gril:getName( )
– self.super:name()
print(self.name,self.age)
self.name = “gril”
self.age = 8
print(self.name,self.age)
self:say()
end
return gril


local son = import(“.son”)
local gril = import(“.gril”)

local _son = son:create()
:addTo(self)
    local _gril = gril:create()
:addTo(self)

—————————————–输出——————————————-
[LUA-print] son:ctor
[LUA-print] father 49
[LUA-print] son 4

[LUA-print] i am your father son 4

[LUA-print] gril:ctor
[LUA-print] father 49
[LUA-print] gril 8
[LUA-print] i am your father gril 8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用 Lua 实现 scrollView 的无限滚动的示例代码: ```lua local scrollView = ccui.ScrollView:create() scrollView:setDirection(ccui.ScrollViewDir.horizontal) scrollView:setContentSize(cc.size(600, 200)) scrollView:setInnerContainerSize(cc.size(1200, 200)) self:addChild(scrollView) -- 添加节点 for i = 1, 5 do local node = cc.LayerColor:create(cc.c4b(0, 255, 0, 255), 200, 200) node:setPosition((i - 1) * 200, 0) scrollView:addChild(node) end -- 添加节点的复制 local firstNode = scrollView:getChildren()[1] local lastNode = scrollView:getChildren()[5] local firstNodeCopy = firstNode:clone() firstNodeCopy:setPosition(lastNode:getPositionX() + 200, 0) scrollView:addChild(firstNodeCopy) local lastNodeCopy = lastNode:clone() lastNodeCopy:setPosition(firstNode:getPositionX() - 200, 0) scrollView:addChild(lastNodeCopy) -- scrollView 的回调函数 scrollView:addEventListener(function(sender, eventType) if eventType == ccui.ScrollviewEventType.containerMoved then local position = scrollView:getInnerContainerPosition() if position.x < -200 then local lastNode = scrollView:getChildren()[6] scrollView:setInnerContainerPosition(cc.p(lastNode:getPositionX() + 200, 0)) elseif position.x > 1000 then local firstNode = scrollView:getChildren()[2] scrollView:setInnerContainerPosition(cc.p(firstNode:getPositionX() - 200, 0)) end end end) ``` 这段代码实现的效果和前面提供的 C++ 代码一样,只不过是使用 Lua 语言编的。需要注意的是,Lua 中的数组下标从 1 开始,而不是从 0 开始,所以在获取节点时需要将下标加 1。 另外,需要注意的是,如果在 C++ 中使用了 Lua,那么在使用 Lua 中的一些时,需要用 ccui 前缀来代替在 C++ 中使用的 ui 前缀。例如,ccui.ScrollView:create() 代替 ui::ScrollView::create()。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值