0
votes

Hey I am working on an App using cocos2d-x. To give the app further functionalities I am currently working with Lua. This is my code which successfully generates a new scene:

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)

end

xpcall(main, G__TRACKBACK)

Now I'd like to add a CCEditBox which requires a CCScale9Sprite. As soon as I open the Lua file my App crashes and I get the following error message:

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

Why is it not possible to use CCScale9Sprite in combination with Lua?

1

1 Answers

1
votes

The error message tells you that Lua doesn't know an object named CCScale9Sprite.

CCScale9Sprite is not part of cocos2d, and from what I know it's not in cocos2d-x either. So it can't be registered as a known object in cocos2d-x's Lua implementation. You'll have to register CCScale9Sprite (binding it to Lua) using cocos2d-x's tolua++ binding mechanism before you can use it on the Lua side. Same goes for any custom classes you add.