Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Change color from shape in runtime — Gideros Forum

Change color from shape in runtime

Hello,
I want to change the color of a shape runtime.
But i can't get it to work... :((

See -- Added line

[From the Drag Me Sample]
--
local function onMouseDown(self, event)

if self:hitTestPoint(event.x, event.y) then
self.isFocus = true

self.x0 = event.x
self.y0 = event.y

-- Added line
self:setFillStyle(Shape.SOLID, 0xffff00, 1)
--
event:stopPropagation()
end
end
--

Can someone please help? O:-)

Comments

  • atilimatilim Maintainer
    Hi DRS,

    You have two options:
    1. Create a white shape and change it's color by using Sprite:setColorTransform(r, g, b, a)
    2. Clear the shape by using Shape:clear() and then reconstruct it again from the beginning with the new color.
  • atilimatilim Maintainer
    Also, self:setFillStyle(Shape.SOLID, 0xffff00, 1) only effects the new drawing, not the previously drawn ones.
  • Hi atilim,
    Thanx for the quick reply!
    How can i implement option 1 within the function onMouseDown(self, event)?

    Sorry, a newbie..
  • atilimatilim Maintainer
    you're welcome :)

    For example you create a white rectange shape like:
    shape = Shape.new()
    shape:setFillStyle(Shape.SOLID, 0xffffff)
    shape:beginPath()
    shape:moveTo(0, 0)
    shape:lineTo(100, 0)
    shape:lineTo(100, 50)
    shape:lineTo(0, 50)
    shape:closePath()
    shape:endPath()
    and then you can make it red onMouseDown as:
    shape:setColorTransform(1, 0, 0)
    or green later:
    shape:setColorTransform(0, 1, 0)
    or a random color:
    shape:setColorTransform(math.random(), math.random(), math.random())
    Hope this is more clear :)

  • edited December 2011
    Ok, so i have to recreate the shape.. :\">
    Because shape is not available in onMouseDown.
  • atilimatilim Maintainer
    If you only want to change the color, not the "shape" of the shape, color transform is the easiest and the fastest way. Otherwise, you need to recreate the shape.
  • atilimatilim Maintainer
    I see. In the Drag Me sample, self is the shape itself :) You need to write self:setColorTransform(....)
  • edited December 2011
    Thanx!

    Final question (for the day >:) )
    Can i also change the color of the line? (setLineStyle)
  • atilimatilim Maintainer
    yes, you can. But, if you want to change the color of line and fill independently with setColorTransform, then you need to create 2 shapes one with line, and one with fill.
  • I understand, thanx for al the help =D>
Sign In or Register to comment.