Hi,
I’m can’t find information on how to change the color of an object, such as a ball or a platform by tapping the object. I’ve seen it in some games where the user goes to the options area.
Appreciate any help,
Thanks
Hi,
I’m can’t find information on how to change the color of an object, such as a ball or a platform by tapping the object. I’ve seen it in some games where the user goes to the options area.
Appreciate any help,
Thanks
If the object is an image, just change the image.
If the object is a polygon, rect, etc use the .fill property…
https://docs.coronalabs.com/api/type/ShapeObject/fill.html
I recommend trying out the basics and then reading on through the guide.
Thanks for that info, but what I’m wondering is what is the code to use for tapping an image to change it’s color?
Thanks again.
You want to look at the “tap” and “touch” events:
https://docs.coronalabs.com/api/event/tap/index.html
https://docs.coronalabs.com/api/event/touch/index.html
Rob
Thanks, I’ll let you know.
This guide probably will be easier to understand than pure documentation:
https://docs.coronalabs.com/guide/events/touchMultitouch/index.html
Brent
@DLuck if you want to change the colour of an object you can do this…
obj:setFillColor(1,0,0)
That will give the image a red tint or
obj:setFillColor(0,1,0)
will give a green tint
I appreciate everybody trying to help, but unfortunately lua is not as easy as I thought. I’ve gone through the Corona University vids, Brian Burton’s, Rafael Hernandez aka cheetomoskeeto, I bought Michelle Fernandez’s book, I could gone and gone, but you know what I mean.
I’m just trying to make a simple little app for fun; I’m not having that much fun right now.
The problem I see with a lot of the API Reference is that it does not explain how to incorporate my code into the function, maybe if it had completed mock example for extreme newbies.
local function tapListener( event )
– Code executed when the button is tapped
print( "Object tapped: " … tostring(event.target) ) – “event.target” is the tapped object
return true
end
local myButton = display.newRect( 100, 100, 200, 50 )
myButton:addEventListener( “tap”, tapListener ) – Add a “tap” listener to the object
I don’t even know if this is the correct code to use; I just want to tap the green button and have it change to another color.
Thanks again
The main problem is that you’re trying to use regular RGB values with your setFillColor call - they should actually be between 0 and 1 - just divide your values by 255:
The other problem I see with your request vs your code is that you’re saying you want to change the colour of an object when it is tapped, but you’re using two display objects - an image and some text. You need to get the simplest version of your requirements working and go from there.
Here is a piece of code which renders a rectangle and then changes it’s colour when the user taps it:
local rect = display.newRect( 100, 100, 100, 50 ) rect.fill = {1,1,1} local function tapRect(e) rect.fill = {0,1,0} return true end rect:addEventListener( "tap", tapRect )
Here is some code which renders some text and colours it when the user taps it:
local text = display.newText{ x=100, y=200, text="Some Text", fontSize=40 } text.fill = {1,1,1} local function tapText(e) text.fill = {0,1,0} return true end text:addEventListener( "tap", tapText )
And here is some text which loads an image and tints it when the user taps on it:
local image = display.newImageRect( "image.png", 100, 100 ) image.x, image.y = 100, 400 local function tapImage(e) image:setFillColor( 0,1,0 ) return true end image:addEventListener( "tap", tapImage )
I should also point out that it is (of course) possible to combine the above three examples by adding the display objects (rect, image and/or text) into a display group and attaching the tap event listener to the group.
You may like to use the standard button object to make your life easier?
local function myButtonPress( event ) if event.phase == "ended" then event.target:setFillColor(1,0,0 ) -- FYI, event.target is a reference to myButton end end local myButton = widget.newButton({ width = 240, height = 20, defaultFile = "btnGreen.png", label = "info", onEvent = myButtonPress })
If the object is an image, just change the image.
If the object is a polygon, rect, etc use the .fill property…
https://docs.coronalabs.com/api/type/ShapeObject/fill.html
I recommend trying out the basics and then reading on through the guide.
Thanks for that info, but what I’m wondering is what is the code to use for tapping an image to change it’s color?
Thanks again.
You want to look at the “tap” and “touch” events:
https://docs.coronalabs.com/api/event/tap/index.html
https://docs.coronalabs.com/api/event/touch/index.html
Rob
Thanks, I’ll let you know.
This guide probably will be easier to understand than pure documentation:
https://docs.coronalabs.com/guide/events/touchMultitouch/index.html
Brent
@DLuck if you want to change the colour of an object you can do this…
obj:setFillColor(1,0,0)
That will give the image a red tint or
obj:setFillColor(0,1,0)
will give a green tint
I appreciate everybody trying to help, but unfortunately lua is not as easy as I thought. I’ve gone through the Corona University vids, Brian Burton’s, Rafael Hernandez aka cheetomoskeeto, I bought Michelle Fernandez’s book, I could gone and gone, but you know what I mean.
I’m just trying to make a simple little app for fun; I’m not having that much fun right now.
The problem I see with a lot of the API Reference is that it does not explain how to incorporate my code into the function, maybe if it had completed mock example for extreme newbies.
local function tapListener( event )
– Code executed when the button is tapped
print( "Object tapped: " … tostring(event.target) ) – “event.target” is the tapped object
return true
end
local myButton = display.newRect( 100, 100, 200, 50 )
myButton:addEventListener( “tap”, tapListener ) – Add a “tap” listener to the object
I don’t even know if this is the correct code to use; I just want to tap the green button and have it change to another color.
Thanks again
The main problem is that you’re trying to use regular RGB values with your setFillColor call - they should actually be between 0 and 1 - just divide your values by 255:
The other problem I see with your request vs your code is that you’re saying you want to change the colour of an object when it is tapped, but you’re using two display objects - an image and some text. You need to get the simplest version of your requirements working and go from there.
Here is a piece of code which renders a rectangle and then changes it’s colour when the user taps it:
local rect = display.newRect( 100, 100, 100, 50 ) rect.fill = {1,1,1} local function tapRect(e) rect.fill = {0,1,0} return true end rect:addEventListener( "tap", tapRect )
Here is some code which renders some text and colours it when the user taps it:
local text = display.newText{ x=100, y=200, text="Some Text", fontSize=40 } text.fill = {1,1,1} local function tapText(e) text.fill = {0,1,0} return true end text:addEventListener( "tap", tapText )
And here is some text which loads an image and tints it when the user taps on it:
local image = display.newImageRect( "image.png", 100, 100 ) image.x, image.y = 100, 400 local function tapImage(e) image:setFillColor( 0,1,0 ) return true end image:addEventListener( "tap", tapImage )
I should also point out that it is (of course) possible to combine the above three examples by adding the display objects (rect, image and/or text) into a display group and attaching the tap event listener to the group.