[Resolved] NEED HELP Making Object Appear or Disappear When the Screen is Touched

I am trying to do something that should be so simple, but it is taking me forever to figure out. I’ve searched the forums and online video tutorials, but I can’s seem to get this done. Basically, I want to make an object appear on screen whenever I touch any part of the screen. Then, when my finger is off the screen, I want the object to disappear. Below is my code for attempting to achieve this. Any help is GREATLY appreciated.
local background = display.newImage( “images/Background1.png”, 0, 0 )

local rocco1 = display.newImage(“images/Rocco1.png”)

local mouthClosed = display.newImage(“images/MouthClosed.png”)

local mouthEat = display.newImage(“images/MouthEat.png”)
mouthEat.isVisible = false
function background:touch( event )
if (event.phase == “began”) then
mouthEat.isVisible = true

if (event.phase == “ended”) then
mouthEat.isVisible = false

end

end

background:addEventListener( “touch”, background )

end

[import]uid: 138755 topic_id: 24192 reply_id: 324192[/import]

Here ya go:

[code]

local screenW = display.contentWidth
local screenH = display.contentHeight
local screenHW = screenW *.5
local screenHH = screenH *.5

local mouthEat = nil

function touchHandler(self, event)
print(“here”)
if (event.phase == “began”) then
mouthEat.isVisible = true

elseif (event.phase == “ended”) then
mouthEat.isVisible = false

end
end

local background = display.newRect(0,0,screenW,screenH)
background:setFillColor(200,200,200)
background.touch = touchHandler
background:addEventListener(“touch”, background )

mouthEat = display.newCircle(screenHW, screenHH, 50)
mouthEat:setFillColor(50,50,50)
mouthEat.isVisible = false

[/code] [import]uid: 21331 topic_id: 24192 reply_id: 97618[/import]

You are fantastic! Thanks a bunch. I’m new to this whole thing and I really appreciate the quick help. I do have one other quick question. Is it possible to set a duration for how long the object “mouthEat” will be on screen while the touch is being applied? I don’t want the user to keep their finger on the screen to allow the object to be there indefintely until the touch has ended. [import]uid: 138755 topic_id: 24192 reply_id: 97626[/import]

No longer at my computer, but sure. Instead of setting visibility to false, use transition:
transition.to( moutheat, {time=3000, alpha=0.0})
Will produce fade out effect.

From memory, so may need tweaked , but you get the idea.
If I’m understanding you correctly.

Or maybe you’re looking for a timer.performWithDelay() solution…
[import]uid: 21331 topic_id: 24192 reply_id: 97630[/import]

Yeah, it works. Except, once the object fades out it can’t reappear when the touch is applied again. [import]uid: 138755 topic_id: 24192 reply_id: 97632[/import]

Try:
Transition.to(moutheat, {time=3000, alpha=0.0, onComplete=funtion() moutheat.isVisible = false alpha=1.0 end})
[import]uid: 21331 topic_id: 24192 reply_id: 97633[/import]

Use second one, sry for iPad double tap…lol
I’m out. You should be on right track, I’ll check on ya tomorrow. [import]uid: 21331 topic_id: 24192 reply_id: 97635[/import]

Try:
Transition.to(moutheat, {time=3000, alpha=0.0, onComplete=funtion() moutheat.isVisible = false moutheat.alpha=1.0 end})

This is referred to as closure. Lua and Corona, what a sweet mix! [import]uid: 21331 topic_id: 24192 reply_id: 97634[/import]

That did it! You rock! [import]uid: 138755 topic_id: 24192 reply_id: 97639[/import]

Just a little note, please post in appropriate forum in the future - Feature Requests is for requesting new features you’d like to see, not assistance with code :wink:

Moved to New Users. [import]uid: 52491 topic_id: 24192 reply_id: 97648[/import]

Got it. Sorry about that. I thought features request was for any features a developer was trying to do, but couldn’t. I’ll make sure to post in the appropriate forum next time. [import]uid: 138755 topic_id: 24192 reply_id: 97708[/import]

I used the solution given here and tweaked it a little. My character “Rocco” eats bones. I programmed it so when you touch a particular button the bones go flying into his mouth, which is “mouthEat” in the code. Everything works great, but the final piece to this stage is that when the bones collide with the mouthEat, I want the bones and the mouthEat to disappear, and then have the screen go to the next level. My code below makes sense to me and doesn’t produce any errors. What’s wrong with it?

[lua]

module (…, package.seeall)

function new()

local level1Group = display.newGroup()

local physics = require (“physics”)
physics.start()
physics.setGravity(0,0)

local background = display.newImage( “images/Background1.png”, 0, 0 )

local rocco1 = display.newImage(“images/Rocco1.png”)

local mouthClosed = display.newImage(“images/MouthClosed.png”)

local bone = display.newImage(“images/Bone.png”)
bone.x = 65
bone.y = 25

local bone2 = display.newImage(“images/Bone.png”)
bone2.x = 65
bone2.y = 45

local number1 = display.newImage(“images/1.png”)
number1.x = 85
number1.y = 275

local number2 = display.newImage(“images/2.png”)
number2.x = 185
number2.y = 275

local number3 = display.newImage(“images/3.png”)
number3.x = 285
number3.y = 275

local number4 = display.newImage(“images/4.png”)
number4.x = 385
number4.y = 275

level1Group:insert(background)
level1Group:insert(rocco1)
level1Group:insert(mouthClosed)
level1Group:insert(number1)
level1Group:insert(number2)

level1Group:insert(number3)
level1Group:insert(number4)
level1Group:insert(bone)
level1Group:insert(bone2)

function touchHandler(self, event)
print(“here”)
if (event.phase == “began”) then

transition.to( bone, {time=1000, alpha=1.0, x=259, y=170, onComplete=function() mouthEat.isVisible = true mouthEat.alpha=1.0 end})
transition.to( bone2, {time=1000, alpha=1.0, x=259, y=170})

end

end

number1.touch = touchHandler
number1:addEventListener(“touch”, number1 )

mouthEat = display.newImage(“images/MouthEat.png”)
mouthEat.isVisible = false

physics.addBody (bone, “static”, {bounce=0, density=1.0})
physics.addBody (bone2, “static”, {bounce=0, density=1.0})
physics.addBody (mouthEat, “static”, {bounce=0, density=1.0})

bone.myName = “bone”
bone2.myName = “bone2”
mouthEat.myName = “mouthEat”
– Collision detection!
mouthEat:addEventListener(“collision”, mouthEat)

function mouthEat:collision (event)
if event.other.myName == “bone” then
mouthEat.visible = false
bone2.visible = false
bone.visible = false

director:changeScene(“level2”)

end

end

level1Group:insert(mouthEat)
level1Group:insert(bone)
level1Group:insert(bone2)

return level1Group

end[/lua] [import]uid: 138755 topic_id: 24192 reply_id: 98314[/import]

Do print statement on line 94, before the if statement, and again on 96, after the if statement.

Do both print? [import]uid: 52491 topic_id: 24192 reply_id: 98424[/import]

Yes they do. Thanks again for the info. I actually managed to solve it without using physics. However, this is something I know I will need in the future. [import]uid: 138755 topic_id: 24192 reply_id: 98450[/import]