请教cocos2dx-lua继承问题

wb197 2014-12-08 06:49:10
function GameScene:ctor()
print("GameScene:ctor called......")
self._visibleSize = cc.Director:getInstance():getVisibleSize()
self._origin = cc.Director:getInstance():getVisibleOrigin()
self._schedulerID = nil
self._rootNode = nil

print("width:%d, height:%d", self._visibleSize.width, self._visibleSize.height)
self._animation = nil
end

程序运行时能够得到值并打印输出

function GameScene:onLoginPush(pSender, touch_type)
print(string.format("width:%d, height:%d", self._visibleSize.width, self._visibleSize.height))
end

程序运行时就会报
[LUA-print] ----------------------------------------
[LUA-print] LUA ERROR: [string "GameScene.lua"]:90: attempt to index field '_visibleSize' (a nil value)

错误呢?



...全文
267 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wb197 2014-12-10
  • 打赏
  • 举报
回复
哦,原来调用setAnimation也要用scene:setAnimation才行,初步明白冒号的作用了。
wb197 2014-12-09
  • 打赏
  • 举报
回复
问题还是没有完全解决,如果有了self指针,那么self._animation为何还是nil value呢?
wb197 2014-12-09
  • 打赏
  • 举报
回复
问题解决了,还要完整的写一个内联function才行?不过这解决的方式有点不太优雅,如果都要这样来写回调函数,那很难将代码格式化规范化啊
笨木头 2014-12-09
  • 打赏
  • 举报
回复
引用 4 楼 wb197 的回复:
问题还是没有完全解决,如果有了self指针,那么self._animation为何还是nil value呢?
你赋值是这样的:GameScene._animation 和你创建出来的scene没有关系,所以你的scene自然就没有这个值了。 你return的是scene,这个不能混乱~ 你多看看Lua的语法(关于元表、元方法、继承方面的),然后多试试,可能就不会这么模糊了
笨木头 2014-12-08
  • 打赏
  • 举报
回复
你这样调用这句:button:addTouchEventListener(function(pSender, touch_type) scene:onLoginPush(pSender, touch_type) end) 应该就没问题了~ (注意要scene:onLoginPush,是冒号不是点号,不然是不会有self参数的~)
wb197 2014-12-08
  • 打赏
  • 举报
回复
明明在ctor中定义了成员变量,在按键响应中要处理却得到nil value,这是何解?以下附完整代码,求熟手赐教…… require "Cocos2d" require "Cocos2dConstants" -- 在GameScene中保留动画节点不行,在GameScene中持有会丢失,在使用时会变成nil,故此使用全局变量来持有 gLoginAnimation = nil --gUserName = nil --gPassWord = nil local GameScene = class("GameScene",function() return cc.Scene:create() end) function GameScene.extend(target) local t = tolua.getpeer(target) if not t then t = {} tolua.setpeer(target, t) end setmetatable(t, GameScene) return target end function GameScene.create() local scene = GameScene.new() local layer = GameScene.extend(cc.Layer:create()) local rootNode = ccs.SceneReader:getInstance():createNodeWithSceneFile("publish/Login_ls1.json") scene.rootNode = rootNode layer:addChild(rootNode) scene:addChild(layer) local animNode = rootNode:getChildByTag(10006) local comRender = animNode:getComponent("CCArmature") local armature = comRender:getNode() local animation = armature:getAnimation() animation:pause() GameScene._animation = animation -- GameScene:setAnimation(animation) gLoginAnimation = animation local UINode = rootNode:getChildByTag(10012) assert(UINode) local uiReader = UINode:getComponent("UIFrames") assert(uiReader) local uiLayer = uiReader:getNode() assert(uiLayer) local button = uiLayer:getChildByName("login_Button") assert(button) button:addTouchEventListener(scene.onLoginPush) local textFieldUserName = uiLayer:getChildByName("name_TextField") assert(textFieldUserName) local textFieldPassWord = uiLayer:getChildByName("password_TextField") assert(textFieldPassWord) gPassWord = textFieldPassWord gUserName = textFieldUserName return scene end function GameScene:ctor() print("GameScene:ctor called......") self._visibleSize = cc.Director:getInstance():getVisibleSize() self._origin = cc.Director:getInstance():getVisibleOrigin() self._schedulerID = nil self._rootNode = nil print("width:%d, height:%d", self._visibleSize.width, self._visibleSize.height) self._animation = nil end function GameScene:setAnimation(pAnim) self._animation = pAnim end function GameScene:onLoginPush(pSender, touch_type) -- if touch_type == ccui.TouchEventType.ended then -- self._animation:play() -- gLoginAnimation:resume() -- end print(string.format("width:%d, height:%d", self._visibleSize.width, self._visibleSize.height)) local userName = gUserName:getStringValue() local passWord = gPassWord:getStringValue() print(string.format("userName:%s", userName)) print(string.format("passWord:%s", passWord)) gLoginAnimation:resume() end return GameScene

8,325

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧