How can I spawn objects?

How can I make an object spawn using one button and have another button that makes it disappear

using this button code

local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then print( "Button was pressed and released" ) end end local button = widget.newButton( { width = 100, height = 50, defaultFile = "images/pause.png", overFile = "images/pause.png", label = "", onEvent = handleButtonEvent } ) -- Center the button resume.x = 17 resume.y = 30 -- Change the button's label text button:setLabel( "Push" ) sceneGroup:insert(button)

Simple

Add

local myImage = display.newImageRect(“image.png”, 50,50)

Inside the handleButtonEvent which you want the image to disappear

if event.phase == “ended” and myImage.alpha ~= 0 then
myImage.alpha = 0
end

Inside the handleButtonEvent which you want the image to reappear

if event.phase == “ended” and myImage.alpha ~= 1 then
myImage.alpha = 1
end

Hope this helps

Thanks a lot man, I have another question, this is probably more complicated. Could I make it that when I entered the scene, the object wasn’t already there, rather I have to spawn it in for it to be there? Also is there a way I could make the button have a limited number of spawns, i.e can only spawn in 3 objects then the button stops spawning more in?

Cheers

Remove myImage and put

local myObjs = {}

In the third button listener
if #myObjs ~= 3 then

myObjs= display.newImageRect(“image.png” ,50,50)

end

If you wanted to hide all Image the button handler should be updated

if event.phase == “ended” and myObjs[1].alpha ~= 0 then

for i =1,#myObjs do
myObjs[i].alpha=0
end

end

Make the images reappear by changing the other button handler

if event.phase == “ended” and myObjs[1].alpha ~= 1 then

for i =1,#myObjs do
myObjs[i].alpha=1
end

end

Sorry I’m not following, could you do an example using the template I provided?

Cheers

Also, when I spawn the object back in it spawns back in the same place as it despawned, how can I make it go back to the original position?

here you go

Sorry if I’m starting to bother you, but its just not working sorry, could you just put it in the template I have attached with this reply, that would be great, using the eggplant as the image.

If you scroll down about halfway there will be a big empty space where you can but the button, no worries if you don’t feel like helping.

Simple

Add

local myImage = display.newImageRect(“image.png”, 50,50)

Inside the handleButtonEvent which you want the image to disappear

if event.phase == “ended” and myImage.alpha ~= 0 then
myImage.alpha = 0
end

Inside the handleButtonEvent which you want the image to reappear

if event.phase == “ended” and myImage.alpha ~= 1 then
myImage.alpha = 1
end

Hope this helps

Thanks a lot man, I have another question, this is probably more complicated. Could I make it that when I entered the scene, the object wasn’t already there, rather I have to spawn it in for it to be there? Also is there a way I could make the button have a limited number of spawns, i.e can only spawn in 3 objects then the button stops spawning more in?

Cheers

Remove myImage and put

local myObjs = {}

In the third button listener
if #myObjs ~= 3 then

myObjs= display.newImageRect(“image.png” ,50,50)

end

If you wanted to hide all Image the button handler should be updated

if event.phase == “ended” and myObjs[1].alpha ~= 0 then

for i =1,#myObjs do
myObjs[i].alpha=0
end

end

Make the images reappear by changing the other button handler

if event.phase == “ended” and myObjs[1].alpha ~= 1 then

for i =1,#myObjs do
myObjs[i].alpha=1
end

end

Sorry I’m not following, could you do an example using the template I provided?

Cheers

Also, when I spawn the object back in it spawns back in the same place as it despawned, how can I make it go back to the original position?

here you go

Sorry if I’m starting to bother you, but its just not working sorry, could you just put it in the template I have attached with this reply, that would be great, using the eggplant as the image.

If you scroll down about halfway there will be a big empty space where you can but the button, no worries if you don’t feel like helping.