Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How can i change the width of a shape? — Gideros Forum

How can i change the width of a shape?

Ozkar619Ozkar619 Member
edited July 2012 in General questions
Hello.
I'm having a little issue with the shape object.
I'm triying to make a lifebar, so the fillbar (a shape sprite) changes it's width depending on the life of the player.
It's very simple, but there is no setWidth property for sprite or shape.
How can I do this with gideros?

Thanks in advance!
Tagged:

Comments

  • hnimhnim Member
    i think setScaleX or setScaleY is another solution.
  • Ozkar619Ozkar619 Member
    edited July 2012
    Yeah, I've tried that, but i think it's not working because I haven't gave an actual size to the shape/sprite itself. Maybe it is scaling it, but you can't scale a 0 value.
    How do I give an initial size to the shape?
    sorry for the inconvenience, and thanks in advance!
  • bowerandybowerandy Guru
    edited July 2012
    When you create a Shape you first have to draw into it to give it a size. After that you can scale it. Try this:
    -- Create a unit square shape
    local bar=Shape.new()
    bar:setFillStyle(Shape.SOLID, 0xff0000)
    bar:beginPath()
    bar:moveTo(0,0)
    bar:lineTo(1,0)
    bar:lineTo(1,1)
    bar:lineTo(0,1)
    bar:lineTo(0,0)
    bar:endPath()
     
    -- Add it to the stage
    stage:addChild(bar)
     
    -- You can define some useful helper methods in Sprite to set width and height
    -- directly
    function Sprite:setWidth(newWidth)
    	local scale=self:getScaleX()
    	local width=self:getWidth()
    	local newScale=scale/width*newWidth
    	self:setScaleX(newScale)
    end
     
    function Sprite:setHeight(newHeight)
    	local scale=self:getScaleY()
    	local width=self:getHeight()
    	local newScale=scale/width*newHeight
    	self:setScaleY(newScale)
    end
     
    -- Now use these to control your bar
    bar:setHeight(10)
    bar:setWidth(lifeForce)
    HTH

    best regards

    Likes: Teranth, atilim

    +1 -1 (+2 / -0 )Share on Facebook
  • @bowerandy nice example, yeah that is exactly the type solution i would use as well. Just create a slice shape, and then extend it as needed with setScaleX. I like your added functions for the sprite class. might just have to borrow those :)
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
  • petecpetec Member
    @bowerandy That example has helped me see how I can add functions to the sprite class, which I had never really got before. Really useful for a learner like me and should help me make my code more efficient. Thanks :)

    Likes: chipster123

    +1 -1 (+1 / -0 )Share on Facebook
  • atilimatilim Maintainer
    edited July 2012
    Thank you @bowerandy :)

    As a small addition to your method, I would prefer using untransformed bounds as:
    function Sprite:setWidth(newWidth)
    	local x,y,width,height=self:getBounds(self)
    	local newScale=newWidth/width
    	self:setScaleX(newScale)
    end
     
    function Sprite:setHeight(newHeight)
    	local x,y,width,height=self:getBounds(self)
    	local newScale=newHeight/height
    	self:setScaleY(newScale)
    end
    Because getWidth()/getHeight() can return 0 if scaleX/scaleY is already 0.

    Likes: Ozkar619

    +1 -1 (+1 / -0 )Share on Facebook
  • @atilim, thanks for that; I've updated my library to include your fix.
  • Ozkar619Ozkar619 Member
    edited September 2012
    @bowerandy @atilim Thanks a lot! It really helped :) I also updated my lib.
Sign In or Register to comment.