@dave1707, this is how things stand right now.
I’ve modified the example you have generously provided by adding extra keys. I’ve also added the ability to simulate what would happen when the iPad is tilted. So now there are four keys in one direction and four keys in the other direction.
When the right side of the iPad is tilted up the red set of keys are available. When the left side is tilted up the green set are available. Gravity.x +/- is used to govern which keys are displayed. I use color to keep track of what’s going on.
You can press and release or press and hold any combination all you want and the blended sound is pleasing. The problem I’m dealing with is when I press AND hold a key WHILE tilting from left to right or from right to left the sound gets locked in and will not stop. Like pushing and pulling on an accordion’s bellows while holding a button changes the sound produced, I want to be able to accomplish this through tilting the iPad.
My goal is that when I press AND hold a red key, for instance, while in one iPad orientation the corresponding green key should replace that sound as the iPad is tilted into the new orientation. When the key is released the sound stops.
I’ve added a text statement to label each key’s touch.id and it seems clear that I’m loosing the original touch.id as I am tilting the iPad. Somehow I need to keep track of the original id so I can release the original sound when Gravity.x’s sense changes.
I hope this make sense and you can provide additional guidance.
(Side note: Is it correct to enclose the code that follows with ~~~ before and after to preserve formatting?)
Thank you very much.
supportedOrientations(PORTRAIT)
displayMode(FULLSCREEN)
function setup()
-- key locs
X1 = (WIDTH/5)
X2 = (WIDTH/5)*2
X3 = (WIDTH/5)*3
X4 = (WIDTH/5)*4
Y = 200
-- added extra keys (all sounds are 2sec in length)
keysPush={}
table.insert(keysPush,key(X1,Y,"Dropbox:TC1PushG3"))
table.insert(keysPush,key(X2,Y,"Dropbox:TC1PushC4"))
table.insert(keysPush,key(X3,Y,"Dropbox:TC1PushE4"))
table.insert(keysPush,key(X4,Y,"Dropbox:TC1PushG4"))
keysPull={}
table.insert(keysPull,key(X1,Y,"Dropbox:TC1PullB3"))
table.insert(keysPull,key(X2,Y,"Dropbox:TC1PullD4"))
table.insert(keysPull,key(X3,Y,"Dropbox:TC1PullF4"))
table.insert(keysPull,key(X4,Y,"Dropbox:TC1PullA4"))
end
function draw()
background(40, 40, 50)
fill(255, 255, 255, 255)
fontSize(25)
textMode(CENTER)
fontSize(50)
-- display keys based on iPad orientation
if Gravity.x > 0 then
text(string.format("Gravity.x = %.f: Push",Gravity.x),
WIDTH/2,HEIGHT/2-25)
for a,b in pairs(keysPush) do
fill(0, 255, 0, 255)
b:draw()
end
elseif Gravity.x < -0 then
text(string.format("Gravity.x = %.f: Pull",Gravity.x),
WIDTH/2,HEIGHT/2-25)
for a,b in pairs(keysPull) do
fill(255, 0, 0, 255)
b:draw()
end
end
end
function touched(t)
if Gravity.x > 0 then
for a,b in pairs(keysPush) do
b:touched(t)
end
elseif Gravity.x < 0 then
for a,b in pairs(keysPull) do
b:touched(t)
end
end
end
key=class()
function key:init(x,y,s)
self.x=x
self.y=y
self.rad= 35
self.k=nil -- this will hold address of sound()?
self.s=s -- sound to be played
self.id=0
end
function key:draw()
ellipseMode(RADIUS)
strokeWidth(5)
ellipse(self.x,self.y,self.rad)
stroke(255, 255, 255, 255)
textMode(CENTER)
fontSize(15)
fill(255, 255, 255, 255)
-- sound and touch id
text(self.s,self.x,self.y-self.rad-25)
text(self.id,self.x,self.y-self.rad-50)
end
function key:touched(t)
if t.state==BEGAN then
if vec2(t.x,t.y):dist(vec2(self.x,self.y)) <= self.rad then
self.id=t.id
self.k=sound(self.s,1,1,1,true)
end
elseif t.state==ENDED and t.id==self.id then
self.k:stop()
end
end