Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
need help understanding gideros classes... — Gideros Forum

need help understanding gideros classes...

buchobucho Member
edited April 2013 in General questions
hello,

I have problems to understand the gideros class system (I have used java so far)...

I want to inherit a class from another class that is inherited from another class...

like so:


Particl2D.lua

particle2D = Core.class(Sprite)

function particle2D:init(texRegion, ID)

self.ID = ID

self:addChild(Bitmap.new(texRegion))

end


Blob.lua

blob = Core.class(Particle2D)

function blob:init(x, y, texRegion)

particle2D.init(self, texRegion, 1)

self:setPosition(x, y)

stage:addChild(self)

end



and then in main.lua


b = blob.new(someTexRegion, 10, 10)



but I get this error message:
buddi/Particle2D.lua:47: attempt to call method 'addChild' (a nil value)
stack traceback:
buddi/Particle2D.lua:47: in function 'init'
buttons/SlotButton.lua:12: in function 'init'
[string "property.lua"]:52: in function '__new'
[string "property.lua"]:59: in function 'new'
main.lua:54: in main chunk

(where line 47 is the line with self:addChild(Bitmap.new(texRegion)) in Particle2D.lua)

however... when I create a particle2D object directly in main.lua then it works...
like so:

p = particle.new(someTexregion, 1)


somewhere here I read that init() is the constructor and I could call the super class with .init(self)...

but I think I'd need some better explanation on how classes in gideros work (and how I can create/inherite them)...


thanks for any help... :)


PS:
and how can I tag code as code in a post for better reading?

Comments

  • ScouserScouser Guru
    edited April 2013
    @bucho: Your problem would appear to be a dependencies / execution order problem.
    Try this thread, it may help.

    To tag code use
    <pre lang="lua">
    -- Your code here
    ....
    </ pre>
    Without the space
  • ar2rsawseenar2rsawseen Maintainer
    @bucho problem is that init method automatically calls parent classes init exactly with same arguments you've passed to child classes init. Thats why you get the error.
    So basically inheritance can not work good with classes with different constructor arguments (at least same amount of arguments should be the same by type etc).

    So there are different workarounds:
    1) check inside particle2D:init which arguments have been passed, so you could determine who is calling constructor and what to do
    2) in particle2D leave the init without argumetns and create antoher method like particle2D:create(texRegion, ID) which you could call from blob:init
    3) Do not inherit from particle2D but make the instance of it inside blob:init and store as a property so you could use it for anything you wanted to use it
  • buchobucho Member
    Hi,

    thanks for the quick response...

    unfortunately it still doesn't work... even when I use the same arguments for both classes... and also the code dependencies seem to be right...

    so I have given up this way for now... and created a new class for blob... it's a bit unhandy but should do for now...

    thanks!
  • @bucho, if you are still interested in solving your original problem can you zip up an example project that illustrates it and attach it to your reply. It's easy to ensure there are no typos that way.

    Best regards
  • buchobucho Member
    Hi... oh, thanks... I'll attach a zip when I'm back on Monday...
Sign In or Register to comment.