Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
getPosition() for Shape always returns (0,0)? — Gideros Forum

getPosition() for Shape always returns (0,0)?

grotlygrotly Member
edited March 2012 in Game & application design
I tried the Shapes example in Gideros Studio and I always get (0, 0) when calling shape:getPosition(). Same for shape:getX() or shape:getY(). If I do a shape:setPosition(x, y) then the shape is shifted from the initial position by x and y pixels.

Does anyone else have this problem or am I using the wrong way? How do I get the correct position of a Shape (either top-left point or mid point)?

Comments

  • CarolineCaroline Guru
    edited March 2012
    If I need a shape that I move around, or get the position of, then I would create the shape at 0,0, and then setPosition(x,y).

    Such as a red square that I can move around:
    shape = Shape.new()
     
    shape:setFillStyle(Shape.SOLID, 0xff0000)  
     
    shape:beginPath()
    shape:moveTo(0,0)
    shape:lineTo(100,0)
    shape:lineTo(100,100)
    shape:lineTo(0,100)
    shape:lineTo(0,0)
    shape:endPath()
     
    stage:addChild(shape)
     
    --prints 0,0
    print(shape:getPosition()) 
     
    shape:setPosition(20,100)
     
    --prints 20,100 and shape appears at that position
     
    print(shape:getPosition())
  • Are you adding the shape to another sprite and positioning this one?
  • grotlygrotly Member
    edited March 2012
    MikeHart: not at all. The ultimate goal is to attach a body to the shape.
  • Thanks Caroline, that would be a good enough workaround. I was wondering why the "official" solution does not work.
  • To me that is the "official" solution.

    When you create a shape, its default position is 0,0. You can then draw lines as I have done above at any coordinate. But when you get the position of the shape itself, which is not the position of the lines, then the position is still 0,0. Not where the lines were drawn.

    Not sure that I'm making myself clear there :).
  • Ok, got it now :) Thanks!
  • Yes, even though the position is 0, 0 initially, it doesn't really matter until you add it to the stage (or another sprite).
Sign In or Register to comment.