嘿,我正在使用 cocos2d-x 开发一个应用程序。为了给应用程序提供更多功能,我目前正在使用 Lua。这是我成功生成新场景的代码:
函数主()
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(主要,G__TRACKBACK)
现在我想添加一个需要 CCScale9Sprite 的 CCEditBox。一旦我打开 Lua 文件,我的应用程序就会崩溃,并收到以下错误消息:
PANIC:调用 Lua API 时出现未受保护的错误(...3-AEC8-DC161CC38F63/SalesOrderApp.app/helloLocal.lua:47:尝试索引全局“CCScale9Sprite”(零值))
为什么不能将 CCScale9Sprite 与 Lua 结合使用?