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]