Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Help me with getNumChildren — Gideros Forum

Help me with getNumChildren

leduylinh87leduylinh87 Member
edited March 2014 in General questions
I'm start using gideros mobile for about a week now, really great engine, however with so little experiences about lua I've many problems in using Gideros. Please help me out

here is my code snippet

coins = Sprite.new()
stage:addChild(coins)

function onEnterFrame(event)
local coinNum = coins:getNumChildren()
print(coinNum)
end

result: 841

another issue

function coin:init()
self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
end

function destroyBody()
self:removeEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
self:removeFromParent()
end

result: attempt to index global 'self' (a nil value)

the result I've got seem like a random number everytime I run my project :( and sorry for my bad English, too

Comments

  • jdbcjdbc Member
    edited March 2014 Accepted Answer
    If you want to use OOP (Oriented Object Programming) try this:
    Coin = Core.class(Sprite)
     
    function Coin:init()
     ...
    end
     
    function Coin:destroyBody()
     ...
    end
     
    function Coin:onEnterFrame()
     ...
    end
     
    local coin = Coin.new()
    stage:addChild(coin)
    where coin is an sprite object and 'self' means this object inside functions.

    You will need some tutorial about lua + OOP.

    Likes: leduylinh87

    +1 -1 (+1 / -0 )Share on Facebook
  • oh, thank you so much for replying so fast, I'm really glad :D

    The second problem was solved, thank to jdbc's reply, I forgot to declare coin:destroyBody() instead of destroyBody() feeling really dump, thanks you very very much, how about the first problem? Can you help me on that one too please, it's been 4 hours tearing my hair off about that :(
  • ar2rsawseenar2rsawseen Maintainer
    @leduylinh87 here is the simple example I did, which prints out 0
    coins = Sprite.new()
    stage:addChild(coins)
     
    function onEnterFrame(event)
     local coinNum = coins:getNumChildren()
     print(coinNum)
    end
     
    stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
    So somewhere in your code most probably you are adding children, thus quantity increases ?
  • Thank you, ar2rsawseen, gideros community is really great

    I'm not really sure about the first issue, but now I've tried another way around this issue, and it work fine, now, thank you ^^
Sign In or Register to comment.