-- -- AS 91373 Programming assessment -- -- [Benjamim Saddler] -- -- [give program overview] -- ============================================================= -- main.lua -- ============================================================= ---------------------------------------------------------------------- -- 1. Requires ---------------------------------------------------------------------- -- require the widget library local widget = require ("widget") ---------------------------------------------------------------------- -- 2. Constants ---------------------------------------------------------------------- -- create constants to make thing more efficient later on local centerX = display.contentCenterX local centerY = display.contentCenterX local fullw = display.contentWidth local fullh = display.contentHeight ---------------------------------------------------------------------- -- 2. Variables ---------------------------------------------------------------------- local faceChoice = {} -- create a table to hold user choice -- this needs to be scoped for the whole file so that all functions can access it -- faceChoice[1] = face choice -- faceChoice[2] = amount local hairChoice = {} local shirtChoice = {} local pantsChoice = {} local shoeChoice = {} ---------------------------------------------------------------------- -- 3. Function ---------------------------------------------------------------------- -- createfacePicker Function local function createFacePicker(group, faceTable) -- Generic Function to Create picker -- Receives: -- group -- display group to insert the elements into -- faceTable -- a table holding the names of faces to select -- Returns: nil -- display text for picker that shows the current selection local myText = display.newText(group, faceTable[1], centerX, centerY, native.systemFont, 36 ) -- initialize a variable called counter and set it to 1 local counter = 1 -- make faceChoices[1] equal the first element of the table that was passed over faceChoice[1] = faceTable[1] -- generic function to handle button presses local function handleButtonEvent (event) -- check if the id of the event is Left or Right -- if Left then subtract 1 from the counter and update -- the text to show the new value -- if Right then add 1 from the counter and update -- the text to show the new value if event.target.id == "Left" then -- update the counter value counter = counter - 1 myText.text = faceTable[counter] elseif event.target.id == "Right" then counter = counter + 1 -- update the counter value myText.text = faceTable[counter] end -- update the value stored in the main table faceChoice[1] = faceTable[counter] end -- display the two buttons, an arrow on the left to scroll text left -- and an arrow on the right to scroll text right -- place the arrows in the correct place on the screen and insert them -- into the appropriate group local leftButton = widget.newButton( { left = 0, top = 0, id = "Left", label = "Left", radius = 30, shape = "circle", fillColor = { default={0,1,0,1}, over={0,1,0,0.8} }, onRelease = handleButtonEvent }) leftButton.y = centerY leftButton.x = 100 group:insert(leftButton) local rightButton = widget.newButton( { left = 0, top = 0, id = "Right", label = "Right", radius = 30, shape = "circle", fillColor = { default={0,1,0,1}, over={0,1,0,0.8} }, onRelease = handleButtonEvent }) rightButton.y = centerY rightButton.x = fullw - 100 group:insert(rightButton) end --------------------------------------------------------------------------------------------- local function createHairPicker(group2, hairTable) -- Generic Function to Create picker -- Receives: -- group -- display group to insert the elements into -- faceTable -- a table holding the names of faces to select -- Returns: nil -- display text for picker that shows the current selection local myText2 = display.newText(group2, hairTable[1], centerX, centerY - 100, native.systemFont, 36 ) -- initialize a variable called counter and set it to 1 local counter2 = 1 -- make hairChoices[1] equal the first element of the table that was passed over hairChoice[1] = hairTable[1] -- generic function to handle button presses local function handleButtonEvent2 (event2) -- check if the id of the event is Left or Right -- if Left then subtract 1 from the counter and update -- the text to show the new value -- if Right then add 1 from the counter and update Right -- the text to show the new value if event2.target.id == "Left2" then -- update the counter value counter2 = counter2 - 1 myText2.text = hairTable[counter2] elseif event2.target.id == "Right2" then counter2 = counter2 + 1 -- update the counter value myText2.text = hairTable[counter2] end -- update the value stored in the main table hairChoice[1] = hairTable[counter2] end -- display the two buttons, an arrow on the left to scroll text left -- and an arrow on the right to scroll text right -- place the arrows in the correct place on the screen and insert them -- into the appropriate group local leftButton2 = widget.newButton( { left = 0, top = 0, id = "Left2", label = "Left", radius = 30, shape = "circle", fillColor = { default={0,1,0,1}, over={0,1,0,0.8} }, onRelease = handleButtonEvent2 }) leftButton2.y = centerY - 100 leftButton2.x = 100 group2:insert(leftButton2) local rightButton2 = widget.newButton( { left = 0, top = 0, id = "Right2", label = "Right", radius = 30, shape = "circle", fillColor = { default={0,1,0,1}, over={0,1,0,0.8} }, onRelease = handleButtonEvent2 }) rightButton2.y = centerY - 100 rightButton2.x = fullw - 100 group2:insert(rightButton2) end ------------------------------------------------------------------------------------------------- local function createShirtPicker(group3, shirtTable) -- Generic Function to Create picker -- Receives: -- group -- display group to insert the elements into -- faceTable -- a table holding the names of faces to select -- Returns: nil -- display text for picker that shows the current selection local myText3 = display.newText(group3, shirtTable[1], centerX, centerY + 100, native.systemFont, 36 ) -- initialize a variable called counter and set it to 1 local counter3 = 1 -- make faceChoices[1] equal the first element of the table that was passed over shirtChoice[1] = shirtTable[1] -- generic function to handle button presses local function handleButtonEvent3 (event3) -- check if the id of the event is Left or Right -- if Left then subtract 1 from the counter and update -- the text to show the new value -- if Right then add 1 from the counter and update -- the text to show the new value if event3.target.id == "Left3" then -- update the counter value counter3 = counter3 - 1 myText3.text = shirtTable[counter3] elseif event3.target.id == "Right3" then counter3 = counter3 + 1 -- update the counter value myText3.text = shirtTable[counter3] end -- update the value stored in the main table shirtChoice[1] = shirtTable[counter3] end -- display the two buttons, an arrow on the left to scroll text left -- and an arrow on the right to scroll text right -- place the arrows in the correct place on the screen and insert them -- into the appropriate group local leftButton3 = widget.newButton( { left = 0, top = 0, id = "Left3", label = "Left", radius = 30, shape = "circle", fillColor = { default={0,1,0,1}, over={0,1,0,0.8} }, onRelease = handleButtonEvent3 }) leftButton3.y = centerY + 100 leftButton3.x = 100 group3:insert(leftButton3) local rightButton3 = widget.newButton( { left = 0, top = 0, id = "Right3", label = "Right", radius = 30, shape = "circle", fillColor = { default={0,1,0,1}, over={0,1,0,0.8} }, onRelease = handleButtonEvent3 }) rightButton3.y = centerY + 100 rightButton3.x = fullw - 100 group3:insert(rightButton3) end -------------------------------------------------------------------------------------------------- local function createPantsPicker(group4, pantsTable) -- Generic Function to Create picker -- Receives: -- group -- display group to insert the elements into -- faceTable -- a table holding the names of faces to select -- Returns: nil -- display text for picker that shows the current selection local myText4 = display.newText(group4, pantsTable[1], centerX, centerY + 200, native.systemFont, 36 ) -- initialize a variable called counter and set it to 1 local counter4 = 1 -- make faceChoices[1] equal the first element of the table that was passed over pantsChoice[1] = pantsTable[1] -- generic function to handle button presses local function handleButtonEvent4 (event4) -- check if the id of the event is Left or Right -- if Left then subtract 1 from the counter and update -- the text to show the new value -- if Right then add 1 from the counter and update -- the text to show the new value if event4.target.id == "Left4" then -- update the counter value counter4 = counter4 - 1 myText4.text = pantsTable[counter4] elseif event4.target.id == "Right4" then counter4 = counter4 + 1 -- update the counter value myText4.text = pantsTable[counter4] end -- update the value stored in the main table pantsChoice[1] = pantsTable[counter4] end -- display the two buttons, an arrow on the left to scroll text left -- and an arrow on the right to scroll text right -- place the arrows in the correct place on the screen and insert them -- into the appropriate group local leftButton4 = widget.newButton( { left = 0, top = 0, id = "Left4", label = "Left", radius = 30, shape = "circle", fillColor = { default={0,1,0,1}, over={0,1,0,0.8} }, onRelease = handleButtonEvent4 }) leftButton4.y = centerY + 200 leftButton4.x = 100 group4:insert(leftButton4) local rightButton4 = widget.newButton( { left = 0, top = 0, id = "Right4", label = "Right", radius = 30, shape = "circle", fillColor = { default={0,1,0,1}, over={0,1,0,0.8} }, onRelease = handleButtonEvent4 }) rightButton4.y = centerY + 200 rightButton4.x = fullw - 100 group4:insert(rightButton4) end --------------------------------------------------------------------------------------------------- local function createShoePicker(group5, shoeTable) -- Generic Function to Create picker -- Receives: -- group -- display group to insert the elements into -- faceTable -- a table holding the names of faces to select -- Returns: nil -- display text for picker that shows the current selection local myText5 = display.newText(group5, shoeTable[1], centerX, centerY + 300, native.systemFont, 36 ) -- initialize a variable called counter and set it to 1 local counter5 = 1 -- make faceChoices[1] equal the first element of the table that was passed over shoeChoice[1] = shoeTable[1] -- generic function to handle button presses local function handleButtonEvent5 (event5) -- check if the id of the event is Left or Right -- if Left then subtract 1 from the counter and update -- the text to show the new value -- if Right then add 1 from the counter and update -- the text to show the new value if event5.target.id == "Left5" then -- update the counter value counter5 = counter5 - 1 myText5.text = shoeTable[counter5] elseif event5.target.id == "Right5" then counter5 = counter5 + 1 -- update the counter value myText5.text = shoeTable[counter5] end -- update the value stored in the main table shoeChoice[1] = shoeTable[counter5] end -- display the two buttons, an arrow on the left to scroll text left -- and an arrow on the right to scroll text right -- place the arrows in the correct place on the screen and insert them -- into the appropriate group local leftButton5 = widget.newButton( { left = 0, top = 0, id = "Left5", label = "Left", radius = 30, shape = "circle", fillColor = { default={0,1,0,1}, over={0,1,0,0.8} }, onRelease = handleButtonEvent5 }) leftButton5.y = centerY + 300 leftButton5.x = 100 group5:insert(leftButton5) local rightButton5 = widget.newButton( { left = 0, top = 0, id = "Right5", label = "Right", radius = 30, shape = "circle", fillColor = { default={0,1,0,1}, over={0,1,0,0.8} }, onRelease = handleButtonEvent5 }) rightButton5.y = centerY + 300 rightButton5.x = fullw - 100 group5:insert(rightButton5) end ----------------------------------------------------------------------------------------------------- -- function createTextBox local function createTextBox( group ) -- Create text field -- Receives: -- group - group to insert the text field into -- Returns: -- textField - a reference to the textfield so we can access its contents later -- display title "How many?" to prompt the user to enter an amount of face items local title = display.newText(group, "Enter character strength" , centerX, centerY + 460, native.systemFont, 36 ) -- display a text field on the screen -- store it in a varible called textField -- place it in the correct location -- insert it into the correct group local textField = native.newTextField( 0, 0, 200, 50 ) textField.x = centerX textField.y = title.y + 50 group:insert(textField) -- return the textField return textField end local function displayface(face) -- draws the users face choice and dislplays their bill -- receives: storeValue - table containing all the values generated in storekeys -- returns: nil -- create a new display group to hold the info about the face local group = display.newGroup() local body = display.newImage(group, "body.png",0,0) body.x = centerX body.y = centerY + 140 -- display title "Your face" and display it on the screen -- place it on the screen and insert it into the group local title = display.newText(group, "Your character", centerX, 75, native.systemFont, 36 ) -- display the image of the correct face on the screen (faceChoice[1]) -- place it on the screen and insert it into the group local face = display.newImage(group, faceChoice[1]..".png",0,0) face.x = centerX face.y = centerY - 100 end local function displayhair(hair) local group = display.newGroup() local hair = display.newImage(group, hairChoice[1]..".png",0,0) hair.x = centerX hair.y = centerY - 170 end local function displayshirt(shirt) local group = display.newGroup() local shirt = display.newImage(group, shirtChoice[1]..".png",0,0) shirt.x = centerX shirt.y = centerY + 45 end local function displaypants(pants) local group = display.newGroup() local pants = display.newImage(group, pantsChoice[1]..".png",0,0) pants.x = centerX pants.y = centerY + 230 end local function displayshoe(shoe) local group = display.newGroup() local shoe = display.newImage(group, shoeChoice[1]..".png",0,0) shoe.x = centerX - 45 shoe.y = centerY + 368 end local function selectButton(selectionGroup, textField) local function handleButtonEvent(event) if textField.text == "" then local alert = native.showAlert( "Error", "No value entered" ) elseif tonumber(textField.text) == nil then local alert = native.showAlert( "Error", "Must be a number" ) elseif tonumber(textField.text) \> 100 then local alert = native.showAlert( "Error", "Can't be more that 100") elseif tonumber(textField.text) \< 1 then local alert = native.showAlert( "Error", "Can't be less that 1") else display.remove(selectionGroup) displayface(faceChoice) displayhair(hairChoice) displaypants(pantsChoice) displayshirt(shirtChoice) displayshoe(shoeChoice) end end local doneButton = widget.newButton( { left = 0, top = 0, id = "done", label = "Finished", radius = 20, shape = "roundedRect", fillColor = { default={0,1,1,1}, over={0,1,1,0.8} }, onRelease = handleButtonEvent }) -- place the button on the screen and insert it ino the right group doneButton.y = fullh - 50 doneButton.x = centerX selectionGroup:insert(doneButton) end --function createSelection local function createSelection() -- creates the first selection group -- receives: nil -- returns: nil -- create a new group local group = display.newGroup() -- create a table with all the face elements in it -- "chicken", "hamburger", "pizza", "rice", "salad" local faceTable = {"Ecstatic", "Happy", "Scared", "Smug", "Surprised"} -- call createfacePicker and pass over the display group and faceTable createFacePicker(group, faceTable) -- call createTextBox and pass over the display group, store the returned value in textField local textField = createTextBox(group ) -- call selectButton and pass over the display group and textField selectButton(group, textField) end -- call the function createSelection createSelection() --function createSelection local function createSelection2() -- creates the first selection group -- receives: nil -- returns: nil -- create a new group local group2 = display.newGroup() -- create a table with all the face elements in it -- "chicken", "hamburger", "pizza", "rice", "salad" local hairTable = {"Hat", "Headphone", "Mowhawk", "Straight"} -- call createfacePicker and pass over the display group and faceTable createHairPicker(group2, hairTable) -- call createTextBox and pass over the display group, store the returned value in textField local textField = createTextBox(group2 ) -- call selectButton and pass over the display group and textField selectButton(group2, textField) end -- call the function createSelection createSelection2() --function createSelection local function createSelection3() -- creates the first selection group -- receives: nil -- returns: nil -- create a new group local group3 = display.newGroup() -- create a table with all the face elements in it -- "chicken", "hamburger", "pizza", "rice", "salad" local shirtTable = {"jacket", "singlet", "tshirt"} -- call createfacePicker and pass over the display group and faceTable createShirtPicker(group3, shirtTable) -- call createTextBox and pass over the display group, store the returned value in textField local textField = createTextBox(group3 ) -- call selectButton and pass over the display group and textField selectButton(group3, textField) end -- call the function createSelection createSelection3() --function createSelection local function createSelection4() -- creates the first selection group -- receives: nil -- returns: nil -- create a new group local group4 = display.newGroup() -- create a table with all the face elements in it -- "chicken", "hamburger", "pizza", "rice", "salad" local pantsTable = {"cargo", "jeans", "threequarter"} -- call createfacePicker and pass over the display group and faceTable createPantsPicker(group4, pantsTable) -- call createTextBox and pass over the display group, store the returned value in textField local textField = createTextBox(group4 ) -- call selectButton and pass over the display group and textField selectButton(group4, textField) end -- call the function createSelection createSelection4() --function createSelection local function createSelection5() -- creates the first selection group -- receives: nil -- returns: nil -- create a new group local group5 = display.newGroup() -- create a table with all the face elements in it -- "chicken", "hamburger", "pizza", "rice", "salad" local shoeTable = {"converse", "jandles", "loafers"} -- call createfacePicker and pass over the display group and faceTable createShoePicker(group5, shoeTable) -- call createTextBox and pass over the display group, store the returned value in textField local textField = createTextBox(group5 ) -- call selectButton and pass over the display group and textField selectButton(group5, textField) end -- call the function createSelection createSelection5()
After entering a strength it says that there is no number in there I am confused!