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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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)

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

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

views:1873 update:2011/12/31 9:35:10
corona forums © 2003-2011