Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Shape:getX() returns 0 — Gideros Forum

Shape:getX() returns 0

unlyingunlying Guru
edited May 2013 in General questions
I can't really understand why it doesn't work.

If i use
Shape:hitTestPoint(event.x,event.y)==true

it works.

But when i try Shape:getX() it returns 0.

Why so?

And is there any simple method to draw line in real time? I mean set start point on mouse down and then redraw it every time on MouseMove?

And how often MouseMove happens? Because if i use lineTo(event.x,event.y) onMouseMove and move mouse fast enough i have not straight line, but dashed.

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Hello @unlying ;)

    most probably your shape is at 0 0 coordinate, thats why getX returns 0.
    Although shape is as 0;0 coordinate, you can draw using moveTo/lineTo in any place, shape will still be at 0;0

    And mouse move I'd guess it is updated as often as onEnterFrame is.

    Hmm, I don't get how you can have dashed line, if you are drawing from the last point to current. Can you show the code ? :)

  • hello @ar2rsawseen

    My shape has 20,20,30,30 coordinates, for example. But it still returns 0.

    I attached screenshot. Every shape from screen returns 0 on getX() or getY(). I can't understand why.

    code for dashed line

    function game:onMouseMove(event)
    line[linecur]:lineTo(event.x,event.y)
    line[linecur]:endPath()
    self:addChild(line[linecur])
    end

    But it is not what i really want. I want straight line that redraws every time.
    I thought it should works something like that

    line[linecur]:clear()
    line[linecur]:lineTo(event.x,event.y)
    line[linecur]:endPath()
    self:addChild(line[linecur])
    line[linecur]:moveTo(startx,starty)

    but it doesn't. Something wrong with my logic:(
    image.PNG
    335 x 531 - 9K
  • @unlying

    you should have to use shape api like this
     
    local shape = Shape.new()
    shape:setFillStyle(Shape.SOLID, 0x00ff00, 1)
    shape:beginPath()
    shape:moveTo(0,0) -- put it always
    shape:lineTo(300, 0)
    shape:lineTo(300, 300)
    shape:lineTo(0, 300)
     
    shape:closePath()
    shape:endPath()
    stage:addChild(shape)
     
    shape:setPosition(100,200) --now set the position once shape is ready just like setting position of sprite
    print(shape:getX())
    :)

    Likes: unlying

    +1 -1 (+1 / -0 )Share on Facebook
  • @hgvyas123 thanks, it works.
    But? Is it really ok???
  • Not for me:) if something on 20,20 why it still returns 0?

    About line. It works for me now.

    If anybody will need this too:

    function game:onMouseMove(event)
    if oldy~=event.y then
    line[linecur]:clear()
    line[linecur] = Shape.new()
    line[linecur]:setLineStyle(3)
    line[linecur]:setFillStyle(Shape.SOLID, color)
    line[linecur]:beginPath()
    line[linecur]:moveTo(startx,starty)

    end

    oldx=event.x
    oldy=event.y

    if draw==1 then
    line[linecur]:lineTo(event.x,event.y)
    line[linecur]:endPath()
    self:addChild(line[linecur])
    end

    end
  • there must be some mistake in ur code make sure you draw your shape from 0,0 and then give it position

    :)
  • As i said before, your method works. But it is not clear for me why shape:getX() returns 0 if it is on 20,20 without setPosition.
  • I still don't get it. There isn't any explain why it works so. Just "The origin of all sprites are at (0, 0) by default". Even if this sprite doesn't has any dot at 0,0.

    Btw, thanks for your shapes snippets. May be i'll use it someday.

    Likes: hgvyas123

    +1 -1 (+1 / -0 )Share on Facebook
  • @Unlying, I thought that the previous replies would have got you a solution to your question.

    Try this
    local shape = Shape.new()
    stage:addChild(shape)
     
    print(shape:getX())
    shape:setPosition(100,100)
     
    print(shape:getX())
    Irrespective of what and how you draw on the shape, the positioning of the object set for the x co-ordinate can be seen here. Hope this gives you a clearer (pardon the pun, you do not see anything on the screen as we are drawing nothing) picture of how it works.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • hgvyas123hgvyas123 Guru
    edited May 2013
    I think i had found a bug here is the example code
    local shape = Shape.new()
    shape:setFillStyle(Shape.SOLID, 0x000000, 0.5)
    shape:beginPath()
    shape:moveTo(100,100)
    shape:lineTo(300, 0)
    shape:lineTo(300, 300)
    shape:lineTo(0, 300)
     
    shape:closePath()
    shape:endPath()
    stage:addChild(shape)
     
     
    print(shape:getPosition())
    print(shape:getBounds(stage))
     
    local shape = Shape.new()
    shape:setFillStyle(Shape.SOLID, 0x00ff00, 0.5)
    shape:beginPath()
    shape:moveTo(100,100)
    shape:lineTo(300, 100)
    shape:lineTo(300, 300)
    shape:lineTo(100, 300)
     
    shape:closePath()
    shape:endPath()
    stage:addChild(shape)
     
     
    print(shape:getPosition())
    print(shape:getBounds(stage))
    output
    ------------------
    0 0
    0 0 300 300
    0 0
    100 100 200 200------------why 100,100
  • unlyingunlying Guru
    edited May 2013
    @OZApps ... It is not about how it works. It is about why it works so.
  • @OZApps ... It is not about how it works. It is about why it works so.
    yeah i am also with you changing my side :D

    :)

  • ar2rsawseenar2rsawseen Maintainer
    @unlying I don't know why but it seems logical to me.

    You have a Shape object which has some coordinates, and then you draw relative to these coordinates.

    For example if you set shape coordinated to 50, 50, then when you use 0, 0 when drawing shape (local points), they will be 50, 50 in global coordinates, 50, 50, would be 100, 100, etc.

  • ar2rsawseenar2rsawseen Maintainer
    If you are asking why, the answer is simple, this way you can draw a shape with any anchor point you want ;)
  • hgvyas123hgvyas123 Guru
    edited May 2013
    but why shape:getBounds returns different value for both the shapes

    :)
  • ar2rsawseenar2rsawseen Maintainer
    this is probably how getBounds method work, it simply wraps the element by all its contents and returns positions and dimensions relative to the stage

    Likes: hgvyas123

    +1 -1 (+1 / -0 )Share on Facebook
  • but doesn't it should have to be the same

    x1,y1,width,height = shape:getBounds(stage)

    and

    x2,y2 = shape:getPosition()

    here x1,y1 is supposed to be same as x2,y2 doesn't it?

    :)
  • ar2rsawseenar2rsawseen Maintainer
    Same thing happens with Bitmaps and anchorpoints.
    As I said, getBounds discards all those Gideros wrappers, and just checks raw contents of objects, as the second shape is drawn at 100, 100, thus it returns 100, 100 ;)

    Likes: hgvyas123

    +1 -1 (+1 / -0 )Share on Facebook
  • That's what i'm trying to say.
    getX() and getBounds() should works same. Probably(ar2sawseen like this word:) ), it's named unification.

    Even if it shouldn't - it should be documented.
  • ar2rsawseenar2rsawseen Maintainer
    :D
    well on the other hand, if they do the same, why then both of them exist, it would be enough to have only one. So there must be a different purpose between them :)
  • @unlying, @hgvyas123 I am a bit lost on your question. Your question started with the positioning of the shape object and the value returned by getX()

    Now the discussion is about getbounds. I understand that the bounds should still be the size from 0,0 as the shape has not been moved. However I am not 100% sure but this could also have something to do with texture size whereby the first shape since it intersects at x:0 and y:0 has the bounds starting from 0,0 where as in the second shape, it starts from 100,100. This behaviour can only be explained by @Atilim as it is internal to Gideros and we can only speculate.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • @ozapps doesn't this is normal with Gideros forum we are kinda famous for taking any discussion to anywhere :D

    :)

    Likes: ar2rsawseen

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.