Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Change X/Y location easier — Gideros Forum

Change X/Y location easier

amaximovamaximov Member
edited March 2013 in General questions
Comign from Corona I was used to changing a display objects (Bitmap, Textures is that what they are called here?) locations simply by changing their ".x" and ".y" properties. Is there a way to do that with Gideros instead of "setX()". I have devised a way using EnterFrame listeners but I do not think it is very performance efficient.

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Sorry, I did not really understand what is the problem with setters?
    How this :
    objet.x = 100
    is much different from this:
    object:setX(100)
    And yes setting x property and then rechecking inside onEnterFrame and use a setter there will not be very efficient, but that how the Corona must be doing it internally.
  • zvardinzvardin Member
    Accepted Answer
    http://www.giderosmobile.com/forum/discussion/comment/20571

    Is a layer made by @OZApps that should accomplish that if you really want, but as there are minor syntactical differences when switching between any languages etc it may be good to learn this new way as well. I'm not sure how his tool does it, but I'm guessing you could also use metatables to perform a function when the value is read and a different function when it is set to accomplish that.
  • bowerandybowerandy Guru
    Accepted Answer
    @amaximov, I don't see why using setX(), setY() etc should be inefficient when used inside an ENTER_FRAME handler. After all that's the way the Gideros GTween module works and that is one of the main ways of doing tweening animations in Gideros.

    best regards
  • Thanks for the replies guys! I just really liked .x and .y properties because they are faster to code :). I guess I will just use setX and setY since it may improve performance compared to Corona's implementation,

    Likes: hgvyas123

    +1 -1 (+1 / -0 )Share on Facebook
  • hgvyas123hgvyas123 Guru
    edited March 2013
    1jetly

    "new word invented can be used when any person says 110% true things" :D

    :)
  • @ar2rsawseen

    from the movie (bollywood)

    :D
  • john26john26 Maintainer
    I suspect Corona literally stores the x-component under .x and queries Lua for this every enterFrame. Gideros stores the location of everything in the C++ code which makes drawing faster. This may explain the speed differences...

    One thing I'd like is the ability to access sprite children using indexing e.g.

    menu:getChildAt(i):addEventListener(Event.MOUSE_DOWN,gotolevel,menu:getChildAt(i))

    could be written

    menu[i]:addEventListener(Event.MOUSE_DOWN,gotolevel,menu[i])

    Much more readable!
  • atilimatilim Maintainer
    edited March 2013 Accepted Answer
    And as a side note, properties (like .x and .y) are usually implemented through __newindex meta-method and they are slow compared to :setX() and :setY().

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • MauMauMauMau Member
    edited November 2013
    +1

    I found this thread because I was *exactly* looking for the same:

    - Using .x / .y instead of the inconvenient :setX() / setY() methods.

    - Easily accessing a sprite's children using MySprite[2] instead of MySprite:getChildAt(2)

    While porting a big amount of code from Corona to Gideros, I found that using .x, .y, Sprite[n] is much cleaner, easier to maintain code. The Gideros ported code looks pretty much "spaghetti like" compared to the original.

    With Corona, I used to do things like this:

    MySprite[6].isVisible = showMe > 0 and true or false

    It's short, easy to read any convenient. Is there any way / workaround to extend Gideros Sprite and Bitmap so we can use .x, .y, .isVisible etc. instead of the clumsy setter / getter functions?

    You need those setters / getters literally hundreds of times, so shorter notations makes a big difference (and it really looks cleaner) in my own opinion.
  • ar2rsawseenar2rsawseen Maintainer
    edited November 2013
    Funny thing that usually Corona's code is compared as spaghetti due to the lack of OOP approach :)

    well usually if someone asked to use MySprite[2] instead of MySprite:getChildAt(2), I'd say you are using it wrong.

    But since you are in process of porting, thats completely understandable.

    you can achieve MySprite[2] through __index metamethod and .x / .y through __newindex

    Here are more on this topic:
    http://lua-users.org/wiki/MetamethodsTutorial
    http://lua-users.org/wiki/MetatableEvents

    So basically you could try something like this (but be warned, that it is a slower process than setter methods):
    function transform(object)
    	--to get sprite[2]
    	object.__oldindex = getmetatable(object).__index
     
    	getmetatable(object).__index = function (t, k)
    		print("test get", t, k)
    		if type(k) == "number" then
    			return object:getChildAt(k)
    		end
    		if object.__oldindex[k] then
    			return object.__oldindex[k]
    		end
    		return nil
    	end
     
    	print("__newindex", getmetatable(Sprite).__newindex)
     
    	getmetatable(object).__newindex = function (t, k, v)
    		print("test set", t, k, v)
    		if type(k) == "number" then
    			return object:addChildAt(v, k)
    		else
    			if not pcall(object.set, object, k, v) then
    				rawset(t,k,v)
    			end
    		end
    	end
    end
    And use it like this:
     
    transform(stage)
     
    local sprite = Sprite.new()
    transform(sprite)
     
    stage[1] = sprite
    local child = Bitmap.new(Texture.new("testbg.png"))
    sprite[1] = child
     
    sprite.x = 100
    sprite.y = 100
  • Interesting stuff ar2rsawseen. Don't worry though I released my first Gideros game about a month ago and have come to appreciate Gideros OOP approach to Lua :)
Sign In or Register to comment.