Making objects touchable and untouchable

Hi,

I am trying to make a soundboard like app. I have 6 objects onscreen and when one is pressed,the others fade out and the pressed object zooms in and plays a sound. When the sound is ended, the object zooms back in place and the other objects fade back in.

Here is my code so far:

[lua]display.setStatusBar( display.HiddenStatusBar )
local w,h = display.contentWidth, display.contentHeight
local background = display.newImageRect(“bg.png”, h, w)

playAuto = audio.loadSound(“Car_Horn.wav”)
autoTime = audio.getDuration(playAuto)
print("Auto ", autoTime, “ms”)
playHubi = audio.loadSound(“Helicopter.wav”)
hubiTime = audio.getDuration(playHubi)
print("Hubi ", hubiTime, “ms”)
playZug = audio.loadSound(“Train_whistle.wav”)
zugTime = audio.getDuration(playZug)
print("Zug ", zugTime, “ms”)
playBoot = audio.loadSound(“Boat.wav”)
bootTime = audio.getDuration(playBoot)
print("Boot ", bootTime, “ms”)
playFire = audio.loadSound(“Fire_truck.wav”)
fireTime = audio.getDuration(playFire)
print("Fire ", fireTime, “ms”)
playBike = audio.loadSound(“Motorbike.wav”)
bikeTime = audio.getDuration(playBike)
print("Bike ", bikeTime, “ms”)

local auto = display.newImageRect(“auto.png”, h/3.4133, w/2.56)
auto.x = w/4.38857
auto.y = h/5.85142
local hubi = display.newImageRect(“heli.png”, h/3.4133, w/3.7832)
hubi.x = w/1.32413
hubi.y = h/5.85142
local zug = display.newImageRect(“zug.png”, h/3.4133, w/2.7826)
zug.x = w/4.38857
zug.y = h/2.048
local boot= display.newImageRect(“boot.png”, h/3.4133, w/3.072)
boot.x = w/1.32413
boot.y = h/2.048
local fire = display.newImageRect(“feuerwehr.png”, h/3.4133, w/2.772)
fire.x = w/4.38857
fire.y = h/1.24121
local bike = display.newImageRect(“motorrad.png”, h/3.4133, w/3.4909)
bike.x = w/1.32413
bike.y = h/1.24121
local group = display.newGroup()

group:insert(auto)
group:insert(hubi)
group:insert(zug)
group:insert(boot)
group:insert(fire)
group:insert(bike)
–auto.isTouchable = true
–hubi.isTouchable = true
local function touchAuto(event)
–if event.phase == “began” and auto.isTouchable == true then
–hubi.isTouchable = false
group:insert(auto)
–timer.performWithDelay( 1000, playAuto)
audio.play(playAuto)
transition.to(auto, { time=750, delay=200, x=(w/2), y=(h/2), xScale=2, yScale=2})
transition.to(auto, { time=750, delay=autoTime + 200, x=w/4.38857, y=h/5.85142, xScale=1, yScale=1})
transition.to(hubi, { time=350, delay=100, alpha = 0})
transition.to(hubi, { time=350, delay=autoTime + 200, alpha = 1})
transition.to(zug, { time=350, delay=100, alpha = 0})
transition.to(zug, { time=350, delay=autoTime + 200, alpha = 1})
transition.to(boot, { time=350, delay=100, alpha = 0})
transition.to(boot, { time=350, delay=autoTime + 200, alpha = 1})
transition.to(fire, { time=350, delay=100, alpha = 0})
transition.to(fire, { time=350, delay=autoTime + 200, alpha = 1})
transition.to(bike, { time=350, delay=100, alpha = 0})
transition.to(bike, { time=350, delay=autoTime + 200, alpha = 1})
–hubi.isTouchable = true
–end
end
local function touchHubi(event)
–if event.phase == “began” and hubi.isTouchable == true then
–auto.isTouchable = false
group:insert(hubi)
–timer.performWithDelay(1000, playHubi)
audio.play(playHubi)
transition.to(hubi, { time=750, delay=200, x=(w/2), y=(h/2), xScale=2, yScale=2})
transition.to(hubi, { time=750, delay=hubiTime + 200, x=w/1.32413, y=h/5.85142, xScale=1, yScale=1})
transition.to(auto, { time=350, delay=100, alpha = 0})
transition.to(auto, { time=350, delay=hubiTime + 200, alpha = 1})
transition.to(zug, { time=350, delay=100, alpha = 0})
transition.to(zug, { time=350, delay=hubiTime + 200, alpha = 1})
transition.to(boot, { time=350, delay=100, alpha = 0})
transition.to(boot, { time=350, delay=hubiTime + 200, alpha = 1})
transition.to(fire, { time=350, delay=100, alpha = 0})
transition.to(fire, { time=350, delay=hubiTime + 200, alpha = 1})
transition.to(bike, { time=350, delay=100, alpha = 0})
transition.to(bike, { time=350, delay=hubiTime + 200, alpha = 1})
–auto.isTouchable = true
–end
end
local function touchZug(event)
group:insert(zug)
–timer.performWithDelay(1000, playZug)
audio.play(playZug)
transition.to(zug, { time=750, delay=200, x=(w/2), y=(h/2), xScale=2, yScale=2})
transition.to(zug, { time=750, delay=zugTime + 200, x=w/4.38857, y=h/2.048, xScale=1, yScale=1})
transition.to(hubi, { time=350, delay=100, alpha = 0})
transition.to(hubi, { time=350, delay=zugTime + 200, alpha = 1})
transition.to(auto, { time=350, delay=100, alpha = 0})
transition.to(auto, { time=350, delay=zugTime + 200, alpha = 1})
transition.to(boot, { time=350, delay=100, alpha = 0})
transition.to(boot, { time=350, delay=zugTime + 200, alpha = 1})
transition.to(fire, { time=350, delay=100, alpha = 0})
transition.to(fire, { time=350, delay=zugTime + 200, alpha = 1})
transition.to(bike, { time=350, delay=100, alpha = 0})
transition.to(bike, { time=350, delay=zugTime + 200, alpha = 1})
end
local function touchBoot(event)
group:insert(boot)
–timer.performWithDelay(1000, playBoot)
audio.play(playBoot)
transition.to(boot, { time=750, delay=200, x=(w/2), y=(h/2), xScale=2, yScale=2})
transition.to(boot, { time=750, delay=bootTime + 200, x=w/1.32413, y=h/2.048, xScale=1, yScale=1})
transition.to(hubi, { time=350, delay=100, alpha = 0})
transition.to(hubi, { time=350, delay=bootTime + 200, alpha = 1})
transition.to(zug, { time=350, delay=100, alpha = 0})
transition.to(zug, { time=350, delay=bootTime + 200, alpha = 1})
transition.to(auto, { time=350, delay=100, alpha = 0})
transition.to(auto, { time=350, delay=bootTime + 200, alpha = 1})
transition.to(fire, { time=350, delay=100, alpha = 0})
transition.to(fire, { time=350, delay=bootTime + 200, alpha = 1})
transition.to(bike, { time=350, delay=100, alpha = 0})
transition.to(bike, { time=350, delay=bootTime + 200, alpha = 1})
end
local function touchFire(event)
group:insert(fire)
–timer.performWithDelay(1000, playFire)
audio.play(playFire)
transition.to(fire, { time=750, delay=200, x=(w/2), y=(h/2), xScale=2, yScale=2})
transition.to(fire, { time=750, delay=fireTime + 200, x=w/4.38857, y=h/1.24121, xScale=1, yScale=1})
transition.to(hubi, { time=350, delay=100, alpha = 0})
transition.to(hubi, { time=350, delay=fireTime + 200, alpha = 1})
transition.to(zug, { time=350, delay=100, alpha = 0})
transition.to(zug, { time=350, delay=fireTime + 200, alpha = 1})
transition.to(boot, { time=350, delay=100, alpha = 0})
transition.to(boot, { time=350, delay=fireTime + 200, alpha = 1})
transition.to(auto, { time=350, delay=100, alpha = 0})
transition.to(auto, { time=350, delay=fireTime + 200, alpha = 1})
transition.to(bike, { time=350, delay=100, alpha = 0})
transition.to(bike, { time=350, delay=fireTime + 200, alpha = 1})
end
local function touchBike(event)
group:insert(bike)
–timer.performWithDelay(2000, audio.play(playBike))
audio.play(playBike)
transition.to(bike, { time=750, delay=200, x=(w/2), y=(h/2), xScale=2, yScale=2})
transition.to(bike, { time=750, delay=bikeTime + 200, x=w/1.32413, y=h/1.24121, xScale=1, yScale=1})
transition.to(hubi, { time=350, delay=100, alpha = 0})
transition.to(hubi, { time=350, delay=bikeTime + 200, alpha = 1})
transition.to(zug, { time=350, delay=100, alpha = 0})
transition.to(zug, { time=350, delay=bikeTime + 200, alpha = 1})
transition.to(boot, { time=350, delay=100, alpha = 0})
transition.to(boot, { time=350, delay=bikeTime + 200, alpha = 1})
transition.to(fire, { time=350, delay=100, alpha = 0})
transition.to(fire, { time=350, delay=bikeTime + 200, alpha = 1})
transition.to(auto, { time=350, delay=100, alpha = 0})
transition.to(auto, { time=350, delay=bikeTime + 200, alpha = 1})
end
auto:addEventListener(“touch”, touchAuto)
hubi:addEventListener(“touch”, touchHubi)
zug:addEventListener(“touch”, touchZug)
boot:addEventListener(“touch”, touchBoot)
fire:addEventListener(“touch”, touchFire)
bike:addEventListener(“touch”, touchBike)[/lua]

Basicly it works (although I think there is room for improvements on my code…), the problem is that if one object is touched, the others stay touchable until
faded out completely. So if I rapidly touch every object on screen, its a big mess…

I tried around to set the objects to isTouchable = false while the transition takes place, but
that did not work.

Can you help me out here please? [import]uid: 103773 topic_id: 19281 reply_id: 319281[/import]

try to set up a variable that will change upon one touch, so if you touch one object the var will set to false, and all touch functions will check that var to see if its true, and because its false they will not fire [import]uid: 16142 topic_id: 19281 reply_id: 74371[/import]

By the way, for optimization you could use arrays and loops to access to all your objets. Similiar result could work with few lines of codes even with hundreds of objects [import]uid: 107239 topic_id: 19281 reply_id: 74383[/import]