Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Beginner question - self:addChild — Gideros Forum

Beginner question - self:addChild

bvarhegyibvarhegyi Member
edited October 2014 in General questions
In the Gideros game development book there is an example on page 50.

It looks like this.

Button = Core.class(Sprite)

function Button:init()
self:addChild(sprite)
end

local bitmap=Bitmap.new(Texture.new("images/balls.png", true))
local btn = Button.new(bitmap)
stage:addChild(btn)
btn:setPosition(100,100)

However this results in an error message:
main.lua:4: bad argument #1 to 'addChild' (Sprite expected, got nil)
stack traceback:
main.lua:4: in function 'init'
[string "property.lua"]:52: in function '__new'
[string "property.lua"]:59: in function 'new'
main.lua:8: in main chunk

Why?
If I leave out the that self:addChild, then it compiles but nothing is rendered.

Comments

  • What I do not understand apart from error message:
    - Why do I need the addChild, even when Button is derived from a Sprite class?
  • @bvarhegyi the real code is (copied from the book)
    function Button:init(sprite)
        self:addChild(sprite)
    end
    So it is some other sprite that you pass to Button constructor and add it to button as a child
  • My bad!! Thx a lot!
Sign In or Register to comment.