Hey if i have an imageRect that i defined and i want that when the screen is being touched it will change the image to a different one how can i change the image?
You can use “widget.newButton”. Are you open to using the widget library in your code?
in the button you can set defaultFile and overFile. Default file will show when unpressed but overFile will show when pressed.
https://docs.coronalabs.com/api/library/widget/newButton.html
I’m guessing your pretty new? So here’s an example.
local widget = require( "widget" ) local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local button1 = widget.newButton( { x = display.contentCenterX y = display.contentCenterY width = 100, height = 100, defaultFile = "buttonDefault.png", overFile = "buttonOver.png", onEvent = handleButtonEvent } )
–InfiSnyp
you can change the fill image of a rect, circle, or imageRect by assigning a new fill (many styles and options):
https://coronalabs.com/blog/2013/11/07/tutorial-repeating-fills-in-graphics-2-0/
https://docs.coronalabs.com/daily/api/type/ShapeObject/fill.html
We have a tutorial for that:
https://coronalabs.com/blog/2013/11/26/tutorial-techniques-for-swapping-images/
That gives you several techniques for swapping images.
Rob
I mean that if i have an imageRect with an image and i want that when i press the screen or do something else the picture of that imageRect will change to a different picture how can i do that?
Did you read the tutorial that I posted?
If you want an instant change simply nil out the image and load a new one in its place. If you want it animated the use the fadeIn, fadeout transitions.
This will achieve want you want at a basic level
local function changeImage()
myImg:removeSelf()
myImg = nil
myImg = display.newImageRect(displayGroup, <path to image>, width, height)
return myImg
end
If you want it to change on touch then call changeImage() from your touch handler
roaminggamer’s suggestion is much simpler though, and reduces the chance of errors creeping in when removing+recreating an object.
--initially creating the object local theImage = display.newImageRect( "myFirstImage.png", 300, 300 ) local function changeTheImage() --change the newImageRect to use mySecondImage.png as its image theImage.fill = { type = "image", filename = "mySecondImage.png" } end if somethingHappened then --call the function whenever you need changeTheImage() end
If you need to change the image multiple times, just pass an argument into the function:
local function changeTheImage(imageName) theImage.fill = { type = "image", filename = imageName } end if somethingHappened then changeTheImage("mySecondImage.png") else changeTheImage("myFirstImage.png") end
You can use “widget.newButton”. Are you open to using the widget library in your code?
in the button you can set defaultFile and overFile. Default file will show when unpressed but overFile will show when pressed.
https://docs.coronalabs.com/api/library/widget/newButton.html
I’m guessing your pretty new? So here’s an example.
local widget = require( "widget" ) local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local button1 = widget.newButton( { x = display.contentCenterX y = display.contentCenterY width = 100, height = 100, defaultFile = "buttonDefault.png", overFile = "buttonOver.png", onEvent = handleButtonEvent } )
–InfiSnyp
you can change the fill image of a rect, circle, or imageRect by assigning a new fill (many styles and options):
https://coronalabs.com/blog/2013/11/07/tutorial-repeating-fills-in-graphics-2-0/
https://docs.coronalabs.com/daily/api/type/ShapeObject/fill.html
We have a tutorial for that:
https://coronalabs.com/blog/2013/11/26/tutorial-techniques-for-swapping-images/
That gives you several techniques for swapping images.
Rob
I mean that if i have an imageRect with an image and i want that when i press the screen or do something else the picture of that imageRect will change to a different picture how can i do that?
Did you read the tutorial that I posted?
If you want an instant change simply nil out the image and load a new one in its place. If you want it animated the use the fadeIn, fadeout transitions.
This will achieve want you want at a basic level
local function changeImage()
myImg:removeSelf()
myImg = nil
myImg = display.newImageRect(displayGroup, <path to image>, width, height)
return myImg
end
If you want it to change on touch then call changeImage() from your touch handler
roaminggamer’s suggestion is much simpler though, and reduces the chance of errors creeping in when removing+recreating an object.
--initially creating the object local theImage = display.newImageRect( "myFirstImage.png", 300, 300 ) local function changeTheImage() --change the newImageRect to use mySecondImage.png as its image theImage.fill = { type = "image", filename = "mySecondImage.png" } end if somethingHappened then --call the function whenever you need changeTheImage() end
If you need to change the image multiple times, just pass an argument into the function:
local function changeTheImage(imageName) theImage.fill = { type = "image", filename = imageName } end if somethingHappened then changeTheImage("mySecondImage.png") else changeTheImage("myFirstImage.png") end