Lua and CCScale9Sprite

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 )

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

Currently CCScale9Sprite is not package to be part of lua.

You can try to add it by either creating a new package file or by add it to an existing package file (this is easier).

It is not really easy to do but it is feasible.

Good luck

Andre

Thanks for the quick reply. How do I know what packages are part of lua?
And where can I find more information on this whole package topic?
:slight_smile:

Files

DOC.html a reference to the current binding in version Beta3 2.1.1
I wrote a program in lua that creates this file from the LuaCocos2d.cpp
build.xml
works for the same version. Please remove line 99 to 104 before using this file this code is experimental and supports multiple inheritance.
I have posted the code somewhere else in this forum if you are interested.

Generating your own bindings

Look in E:2d-x\tools\tolua*+ the *.pkg files describe the bindings to be generated by tolua**.exe
You use ‘ant’ to do this but on window the build.xml may not be up to date; the build.php is ok.
You also need tolua**.exe.
Read the documentation for tolua*+ on the web it is pretty good, it explains what is in the package files.

The file generated is E:2d-x\scripting\lua\cocos2dx_support\LuaCocos2d.cpp

Look in the forum they are more hints.

Good luck

Andre

Sorry should have zip the doc.file :frowning:

see https://github.com/dualface/quick-cocos2d-x

Includes CCScale9Sprite, CCEditBox support, and more !

local DemoScene = class("DemoScene", function()
    return display.newScene("DemoScene")
end)

function DemoScene:ctor()
    local editBoxBg = CCScale9Sprite:createWithSpriteFrameName("InputBox.png")
    local editBox = CCEditBox:create(CCSize(400, 64), editBoxBg)
    editBox:setPosition(display.cx, display.cy) -- screen center
    editBox:registerScriptEditBoxHandler(function(eventType)
        self:onEdit({name = eventType, target = editBox})
    end)
    self:addChild(editBox)
end

function DemoScene:onEdit(event)
    printf("EDITBOX %s, TEXT = %s", event.name, event.target:getText())
end

return DemoScene