首页 文章

创建CCScale9Sprite时使用Lua和Cocos2d-x的混淆

提问于
浏览
0

嘿我正在使用cocos2d-x在App上工作 . 为了给应用程序提供更多功能,我目前正在与Lua合作 . 这是我成功生成新场景的代码:

function main() 

local visibleSize = CCDirector:sharedDirector():getVisibleSize()
local origin = CCDirector:sharedDirector():getVisibleOrigin()

local cclog = function(...)
    print(string.format(...))
end


-- create layer
local function createLuaSceneLayer()
    local luaSceneLayer = CCLayer:create()

         local function menuCallbackBack()
            CCDirector:sharedDirector():popScene()
        end

        local size = CCDirector:sharedDirector():getWinSize();

        local bg = CCLayerColor:create(ccc4(255,255,255,255))
        luaSceneLayer:addChild(bg)



        local backButton = CCMenuItemImage:create("arrow_left.png", "arrow_left.png")
        backButton:setPosition(ccp(20, CCDirector:sharedDirector():getWinSize().height - 20) )
        backButton:registerScriptTapHandler(menuCallbackBack)


        local menu = CCMenu:createWithItem(backButton)
        menu:setPosition(ccp(0, 0))
        luaSceneLayer:addChild(menu,3)


        local textLabel = CCLabelTTF:create("I am a Lua generated Label", "Thonburi", 20)
        textLabel:setPosition(180 ,size.height -20)
        textLabel:setColor(ccc3(0,0,0))
        luaSceneLayer:addChild(textLabel)

        local taskLabel = CCLabelTTF:create("Enter two numbers in the EditBoxes Below", "Thonburi", 20)
        taskLabel:setPosition(160,size.height /7 * 6)
        taskLabel:setColor(ccc3(0,0,0))
        luaSceneLayer:addChild(taskLabel)

        local mySprite = CCScale9Sprite:create("green_edit.png")

        local textField1 = CCEditBox:create(CCSizeMake(200, 40), mySprite)
        textField1:setPosition(40, 40)
        textField1:setFontColor(ccBLACK);
        textField1:setText("");
        luaSceneLayer:addChild(textField1)

    return luaSceneLayer
end


-- run
local luaScene = CCScene:create()
luaScene:addChild(createLuaSceneLayer())

CCDirector:sharedDirector():pushScene(luaScene)

结束

xpcall(main, G__TRACKBACK

现在我想添加一个需要CCScale9Sprite的CCEditBox . 一旦我打开Lua文件,我的应用程序崩溃,我收到以下错误消息:

PANIC: unprotected error in call to Lua API (...3-AEC8-DC161CC38F63/SalesOrderApp.app/helloLocal.lua:47: attempt to index global 'CCScale9Sprite' (a nil value))

为什么不能将CCScale9Sprite与Lua结合使用?

1 回答

  • 1

    该错误消息告诉您Lua不知道名为CCScale9Sprite的对象 .

    CCScale9Sprite不是cocos2d的一部分,据我所知,它也不在cocos2d-x中 . 所以它不能在cocos2d-x的Lua实现中注册为已知对象 . 你必须使用cocos2d-x的tolua绑定机制注册CCScale9Sprite(将它绑定到Lua),然后才能在Lua端使用它 . 您添加的任何自定义类也是如此 .

相关问题