I’m working on my first project, which is an app with three different menus. Each has several option buttons and each button needs to return a number from 1-10. I’m borrowing heavily from SoundsPlus to create my option buttons. I’ve got the three screens with their respective buttons but I don’t know how to get the buttons to return a number. The idea in my head is; The first screen is drawn, you make a choice, the corresponding number is stored, the second screen is drawn, you make your choice, its number is added to the running total, etc. The grand total is displayed at the end.
Without knowing more of what you want to do, here is a simple example of rotating thru three screens and adding a value to the total based on pressing one of three buttons. Hope this gives you some idea… If you have more questions, just ask.
function setup()
total=0
next=false
state=screen1
end
function draw()
background(40, 40, 50)
state()
str=string.format("Total = %d",total)
text(str,WIDTH/2,HEIGHT-200)
end
function screen1()
text("Screen 1",WIDTH/2,HEIGHT-100)
buttons()
if next then
state=screen2
next=false
end
end
function screen2()
text("Screen 2",WIDTH/2,HEIGHT-100)
buttons()
if next then
state=screen3
next=false
end
end
function screen3()
text("Screen 3",WIDTH/2,HEIGHT-100)
buttons()
if next then
state=screen1
next=false
end
end
function touched(t)
if t.state==BEGAN then
if t.x>150 and t.x<250 then -- button 1 pressed
if t.y>375 and t.y<425 then
total = total + value1
next=true
end
end
if t.x>150 and t.x<250 then -- button 2 pressed
if t.y>275 and t.y<325 then
total = total + value2
next=true
end
end
if t.x>150 and t.x<250 then -- button 3 pressed
if t.y>175 and t.y<225 then
total = total + value3
next=true
end
end
end
end
function buttons()
rectMode(CENTER)
fill(255)
rect(200,400,100,50)
rect(200,300,100,50)
rect(200,200,100,50)
fill(255,0,0)
text("value 1",200,400)
text("value 2",200,300)
text("value 3",200,200)
value1=1
value2=2
value3=3
end
My approach to this would be to use a Finite State Machine, have a look at http://codeatuts.blogspot.com.au/2012/07/tutorial-5-finite-state-machines.html
Isn’t there a way to use the array which contains my choices to get the value of the choice? For example, choices = {[1] = “first option”, [2] = “second option” etc. Otherwise, I’d have to write an if,then with 10 cases for the first menu, 8 for the second, and 3 for the third.
SoundButton = class(Button)
SoundName = class()
--soundName = SoundName()
function SoundButton:init(displayName)
Button.init(self, displayName)
self.soundName = loadstring( "return "..displayName )()
self.color = color(87, 129, 231, 255)
self.action = function () selection() toggleMenu()
end
end
function SoundButton:draw()
-- Codea does not automatically call this method
pushStyle()
fill(131, 126, 123, 255)
font("ArialRoundedMTBold")
fontSize(22)
--use longest statement for size
local w,h = textSize(" Physiologic_Monitoring_and_Diagnostic ")
w = w + 20
h = h + 30
roundRect(self.pos.x - w/2,
self.pos.y - h/2,
w,h,30)
self.size = vec2(w,h)
textMode(CENTER)
fill(54, 65, 96, 255) --shadow color
text(self.displayName,self.pos.x+2,self.pos.y-2)
fill(229, 121, 45, 255) --text color
text(self.displayName,self.pos.x,self.pos.y)
popStyle()
end
function selection()
if soundName == ("Patient_Related_and_Other")then Total =
Total + 2 elseif
("Computer_and_Related")then Total = Total + 3 elseif
("Laboratory_Accessories")then Total = Total + 4 elseif
("Analytical_Laboratory")then Total = Total + 5 elseif
("Physiologic_Monitoring_and_Diagnostic")then Total = Total + 6
elseif
("Surgery_and_Intensive_Care_Monitoring")then Total = Total + 7
elseif
("Physical_Therapy")then Total = Total + 8 elseif
("Surgery_and_Intensive_Care")then Total = Total + 9 elseif
("Life_Support")then Total = Total + 10 elseif
--risks
("No_Significant_Risk")then Total = Total + 1 elseif
("Inappropriate_Therapy_or_Diagnosis_minor")then Total = Total + 2
elseif
("Inappropriate_Therapy_or_Diagnosis_major")then Total = Total + 3
elseif
("Patient_or_Operator_Injury")then Total = Total + 4 elseif
("Patient_Death")then Total = Total + 5 elseif
--PMs
("Minimal")then Total = Total + 1 elseif
("Average")then Total = Total + 3 elseif
("Extensive")then Total = Total + 5
end
end
Alright, I’m close. In this section, I’m not getting ‘soundName’ in the proper format. It’s not comparing to the statements in the if/then statements. I think I’m getting a reference value, not the " " value. When I select anything from the first menu, my running total becomes a 3. When I select anything from the second, a 3 is added to the original 3, and ditto for the last menu. I always end up with a total of 9.
Thanks a lot for the help.