Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
How to force all objects to be create with anchor point (0.5, 0.5) - Gideros Forum

How to force all objects to be create with anchor point (0.5, 0.5)

I know that I can add new methods for existing classes in init.lua.

But how do I make that all objects be created with anchor point (0.5, 0.5) ?

I thought in overwrite the Sprite:init method in order to make it call Sprite:setAnchorPosition(0.5, 0.5) but I'm not finding the script Lua that contains Sprite definition.

Note: If you have better idea on how to do it, please let me know.

Thanks.

Comments

  • hgy29hgy29 Maintainer
    edited January 16 Accepted Answer
    There isn't any lua script for it, Sprite is a native class written in C, like most of Gideros classes.
    However you can still do it by overriding 'new' method:
    Sprite.old_new=Sprite.new
    function Sprite.new(...)
     local instance=Sprite.old_new(...)
     instance:setAnchorPoint0.5,0.5)
     return instance
    end
    But it won't work as you expect because native subclasses creation is done in C++, completely by passing any lua overrides.
    Anyway setAnchorPoint in Sprite is a shorthand for setAnchorPoisition(width*ratio,height*ratio), and since width and height would have been 0 at that time, this wouldn't done anything useful...

    EDIT: There may be a way using postInit method. if postInit exists on an object, thehn it is called right after the object is contructed/initialized. So doing:
    function Sprite:postInit(...)
     self:setAnchorPoint0.5,0.5)
    end
    may well work!
    +1 -1 (+2 / -0 ) Share on Facebook
  • hgy29 said:


    There may be a way using postInit method. if postInit exists on an object, thehn it is called right after the object is contructed/initialized. So doing:

    function Sprite:postInit(...)
     self:setAnchorPoint0.5,0.5)
    end
    may well work!

    I see. Just a question: is it save to create the postInit() in Sprite? I mean, won't it overwrite and postInit() function in a derived class?

    I think probably no Sprite's derived class have postInit() but its safer to ask.

    Thank you.
  • olegoleg Member
    @plicatibu You can simply create your class with the parameters you need, just like you create a "button class", but you will need to update the anchor position - after each bitmap is added
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • hgy29hgy29 Maintainer
    IIRC postInit is used for leak tests by bhleaky.lua, Gideros don't use it internally but calls it if it exists.
  • @oleg I didn't want to do this because it would give me more work.

    I think its better changing code in a specific point that applies to all classes.

    @hgy29 Thanks for clarification. I'll add the postInit(...) to Sprite class in init.lua.

    Regards.
  • plicatibuplicatibu Member
    edited January 16
    @hgy29 It worked like a charm.

    Thanks again.

    Likes: MoKaLux

    +1 -1 (+1 / -0 ) Share on Facebook
  • Useful to know too - should be in the official docs.

    Likes: MoKaLux

    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
    +1 -1 (+1 / -0 ) Share on Facebook
Sign In or Register to comment.