Question about setFillColor for images

I’ve having some trouble calling setFillColor on a newImageRect display object and hopefully one of you fine people could give me some help.

The basic idea is that I’m trying to create a custom button which colors itself when a user clicks it. Here is my code.

[lua] local function newSimpleColorSheep( )

– newRect and newImageRect do not behave in the same way.
– newImageRect does not change colors on the touch event
– newRect changes color on the touch event as it should
– test by commenting and uncommenting one fo the following lines
local sheepImg = display.newImageRect( “graphics/characterSheepLeft.png”, 100, 56 )
–local sheepImg = display.newRect(10,10,100,100)

function sheepImg.ramdomizeColor()
print(“sheepImg.ramdomizeColor”)
local red = math.random (255)
local green = math.random (255)
local blue = math.random (255)
sheepImg:setFillColor(red,green,blue)
end

local function onTouch(event)
if “ended” == event.phase then
print(“onTouch”)
– does not work when sheepImg is image
sheepImg.ramdomizeColor()
end
end

sheepImg:addEventListener( “touch”, onTouch )

– initial random color works fine for both newRect and newImageRect
sheepImg.ramdomizeColor()

return sheepImg

end
local mySheep = newSimpleColorSheep()
mySheep.x = display.contentWidth/3
mySheep.y = display.contentHeight/2
localGroup:insert(mySheep) – localGroup created previously[/lua]

The code works fine when the display object is a simple rectangle created using newRect. The object appears as a colored square and when the user clicks on the square it changes color. However, when I use an image created using newImageRect, the object does not change color when touched. The touch event is thrown but then nothing happens, not even a crash.

Still somewhat of a novice in lua so perhaps I am missing something here. Also, FYI, I am using the latest build (702).

Any suggestions would be appreciated.

Thanks! [import]uid: 74786 topic_id: 18725 reply_id: 318725[/import]

duh ! removed the entry as it didn’t make sense what i said. other than makes you realize i was in a meeting which i must have been bored out of my mind.

c. [import]uid: 24 topic_id: 18725 reply_id: 71961[/import]

It did work on my grey scale .PNG, can I rely on this always being the case?

I.e.

stuff = display.newImage("greyscale\_object.png", 80, 150) stuff:setFillColor(255, 255, 0)

Works fine. Assuming multiplicative color model (Rc, Gc, Bc) * (Ri, Gi, Bi) [import]uid: 58849 topic_id: 18725 reply_id: 71972[/import]

Thanks Carlos!

I looked at Walter’s example (http://blog.anscamobile.com/2011/09/tints-and-gradients/) and he gave me the idea to try the displayObject:translate( ). However, it took some finagling.

Here’s my revised code to the above example

[lua] function sheepImg.ramdomizeColor()
local red = math.random (255)
local green = math.random (255)
local blue = math.random (255)
sheepImg:setFillColor(red,green,blue)
sheepImg:translate( 1,0 )
sheepImg:translate( -1,0 )
end[/lua]

Strangely, calling sheepImg:translate( 0,0 ) did not enable the tint change so I had to call translate twice, once to move the sheep a pixel and another time to move the sheep back. Feels like a hack no?

Thanks me7 for your response.

Yeah, the setFillColor worked fine for me in the controlled test you performed but not in my code. Maybe it has to do with the translate function or changing the tint of an object already on the stage. Hard to say.

Much appreciated! [import]uid: 74786 topic_id: 18725 reply_id: 71977[/import]