How to use CCArray in Lua?

How to use CCArray in Lua?
0.0 0



I want to use CCArray to pass array of objects between lua and c**.
Unfortunately, when I add objects to CCArray in Lua, they are unusable afterwards.
Simple test:
local arr = cocos2d.CCArray:array
for i = 1, 10 do
local test1 = ogs.OGSScrollTableEntry:newEntry — <- this is just a CCNode with few additional methods
local spriteTest1 = cocos2d.CCSprite:spriteWithFile
local testObj = arr:objectAtIndex
cocos2d.CCLuaLog.x) — <- attempt to call method ‘getPosition’

I’ve tried to just pass the array to c** and deal with it there, but I get the same error.
After I cast the object from array to OGSScollTableEntry I cannot use any of the CCNode methods.

What’s wrong?

How can I pass an array of objects from Lua to c++?

My current workaround is to create CCArray when instantiating object and adding entries one by one from Lua. It works this way.



I think you should use CCArray:create() instead CCArray:array(). I think array just doesn’t exist when you try to call any member


While you get object from CCArray, what function objectAtIndex returned is CCObject, you should cast it to the very type.

local testObj = tolua.cast(arr:objectAtIndex(1), “CCNode”) — cast it to CCNode, or other type you want
local x, y = testObj:getPosition() — getPosition returned two number - x and y
CCLuaLog(" pos:“…x…”,"…y)