Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Quick questions about Gideros&Lua - Page 14 — Gideros Forum

Quick questions about Gideros&Lua

18910111214»

Comments

  • rrraptorrrraptor Member
    edited November 2019
    How to properly use second parameter to Core.class()?
    I have a hierarchy like: Sprite <- foo <- bar <- abc
    I did this:<pre class="CodeBlock">
     foo = Core.class(Sprite)
     
    function foo:init(param)
    	assert(param ~= nil, "Overide me")
    	self.param = param
    	print("Output from foo", self.param)
    end
     
    bar = Core.class(foo)
     
    function bar:init(param)
    	print("Output from bar", self.param)
    end
     
    function bar:ok() print("bar ok") end
     
    abc = Core.class(bar, function(a) return a end)
     
    function bar:init(param)
    	print("Output from abc", self.param)
    end
     
    local a = abc.new("yo")
    a:ok()
    Ouput:
    Output from foo yo
    Output from abc yo
    bar ok
    Why there is no "Output from bar"?

  • hgy29hgy29 Maintainer
    I see two definitions for bar:init() but none for abc:init() ...

    Likes: oleg

    +1 -1 (+1 / -0 )Share on Facebook
  • Omg, Im so stupid...
    Anyways, any info about 2d args Core.class([base, args]) parameter?
  • hgy29hgy29 Maintainer
    the second arg should be a function that will returns the args to pass to the base class when constructing it

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • rrraptorrrraptor Member
    edited November 2019
    How TextField's "h" parameter in layout properties works?
    I tried different values (big, small, negative), but seems like its competently ignored. Correct me if Im wrong.
  • hgy29hgy29 Maintainer
    It is only used if you want to center vertically or bottom align your text

    Likes: rrraptor

    +1 -1 (+1 / -0 )Share on Facebook
  • rrraptorrrraptor Member
    edited November 2019
    So it shoud not work with TLF_CENTER? And if so, I need to center it by myself?

    Code:
    Label = Core.class(TextField, function(t) return nil,t.text,"" end)
     
    function Label:init(params)
    	self:setLayout{
    		flags = FontBase.TLF_CENTER|FontBase.TLF_REF_TOP,
    		w = params.w, h = params.h or 0
    	}
    	self.ax = params.ax or 0
    	self.ay = params.ay or 0
    	self.offsetX = params.offsetX or 0
    	self.offsetY = params.offsetY or 0
    	self:setPosition(self.offsetX, self.offsetY)
    end
     
    local w, h = 70, 200
    local scale = 2
     
    local l = Label.new({text="Sample multiline text", w = w, h = h})
    l:setTextColor(0xffffff)
    l:setScale(scale)
    l:setPosition(100, 100)
    stage:addChild(l)
     
    local px = Pixel.new(0xff0000, 0.3, w*scale, h*scale)
    px:setPosition(l:getPosition())
    stage:addChild(px)


    Edit: turns out that TLF_CENTER is horizontal center, and I shoud use TLF_VCENTER also, silly me :smile:
    flags = FontBase.TLF_REF_TOP|FontBase.TLF_VCENTER|FontBase.TLF_CENTER
    label.png
    779 x 543 - 28K
  • rrraptorrrraptor Member
    edited November 2019
    Is it posible to inherit meta methods like __tostring() ?

    Lets say I have
    GlobalID = 0
     
    foo = Core.class()
    function foo:init()
    	GlobalID += 1
    	self.id = GlobalID
    end
    function foo:__tostring()
    	return string.format("Object [%i]", self.id)
    end
     
    bar = Core.class(foo)
    function bar:init() end
     
    local a = foo.new()
    print(a) -- > give us "Object [1]"
    local b = bar.new()
    print(b) -- > give us "table: xxxxxxxx"
    But I want to print ID for bar object too.

    I have one solution, but I dont like it, maybe ther is more elegant way of doing this?
    GlobalID = 0
     
    foo = Core.class()
    function foo:init()
    	GlobalID += 1
    	self.id = GlobalID
    	self:gen()
    end
    function foo:gen()
    	local t = getmetatable(self)
    	t.__tostring = function(v) return string.format("Object [%i]", v.id) end
    	setmetatable(self, t)
    end
     
    bar = Core.class(foo)
    function bar:init()
    	self:gen()
    end
     
    local a = foo.new()
    print(a) -- > "Object [1]"
    local b = bar.new()
    print(b) -- > "Object [2]"
Sign In or Register to comment.