Hello. I’m pretty new to coding, and this is a project I’m doing for school where I have to incorporate the use of classes to an app. I’ve run into some trouble and I can’t figure out how to resolve my issue. It would be great if someone was able to help me.
We got a pretty bad rundown on how classes work and how to use them, so I’m not even entirely sure if this is how I’m supposed to go about using it.
What I’m trying to do in this screen is to create a few text fields where the user puts in the answers to whatever question I come up with, and when they push a button to check their answers, will cause a Runtime event signal to be picked up by the class and see whether or not their answers are correct. It sounds super simple on paper, but I’m stuck trying to insert the Question class into the Question screen. I’m getting the error in the screen:initialize where I’m trying to call the class.
Usually, for objects such as an image, I simply create the image, type self:insert(image), local image = self.image, and i can make that image appear or disappear; however, I can’t seem to ‘self:insert’ the class.
Could someone please lend some assistance.
QUESTION SCREEN
questionScreen = {} require ("Classes.30logglobal") local fullW = display.contentWidth local fullH = display.contentHeight function questionScreen:new() local screen = display.newGroup() screen.stage = display.getCurrentStage() function screen:initialize() local Questions = require ("Classes.Questions") self:insert(Questions) self.Questions = Questions Questions:generator() local Title = display.newText { text = ("Questions are Case Sensitive"), x = fullW/2, y = fullH/20; width = 300; font = native.systemFontBold; fontSize = 30; align = "center"; alpha = 0 } self:insert(Title) self.Title = Title --Buttons local checkButton = self:getButton("Temp.png") self:insert(checkButton) self.checkButton = checkButton --These are the native fields whose answers are stored in the Questions class questionInput1 = native.newTextField( 900, 140, 270, 35 ) self:insert(questionInput1) self.questionInput1 = questionInput1 questionInput2 = native.newTextField( 900, 230, 270, 35 ) self:insert(questionInput2) self.questionInput2 = questionInput2 questionInput3 = native.newTextField( 900, 320, 270, 35 ) self:insert(questionInput3) self.questionInput3 = questionInput3 questionInput4 = native.newTextField( 900, 410, 270, 35 ) self:insert(questionInput4) self.questionInput4 = questionInput4 end function screen:getButton(image) local img = display.newImage(image) self:insert(img) function img:touch(event) screen:onButtonTouch(event) end img:addEventListener("touch", img) return img end function screen:onButtonTouch(touch) local target = touch.target local phase = touch.phase if phase == "began" then if target == self.startButton then print("Fly, you fools.") Runtime:dispatchEvent({name = "checkButtonTouched", target = screen}) end return true end end function screen:show() local transitionInTime = 1000 local Title = self.Title local questionInput1 = self.questionInput1 local questionInput2 = self.questionInput2 local questionInput3 = self.questionInput3 local questionInput4 = self.questionInput4 local checkButton = self.checkButton checkButton.x = display.contentWidth/4 checkButton.y = display.contentHeight - 150 checkButton.xScale = .3 checkButton.yScale = .3 checkButton.alpha = 0 questionInput1.x = fullW/2 questionInput2.x = fullW/2 questionInput3.x = fullW/2 questionInput4.x = fullW/2 Title.tween = transition.to(Title, {alpha = 1, transition.easingOutExpo, time = transitionTime, onComplete = function() screen:cancelTween(Title) end}) checkButton.tween = transition.to(Title, {alpha = 1, transition.easingOutExpo, time = transitionTime, onComplete = function() screen:cancelTween(checkButton) end}) questionInput1.tween = transition.to(questionInput1, {alpha = 1, transition.easingOutExpo, time = transitionTime, onComplete = function() screen:cancelTween(questionInput1) end}) questionInput2.tween = transition.to(questionInput2, {alpha = 1, transition.easingOutExpo, time = transitionTime, onComplete = function() screen:cancelTween(questionInput2) end}) questionInput3.tween = transition.to(questionInput3, {alpha = 1, transition.easingOutExpo, time = transitionTime, onComplete = function() screen:cancelTween(questionInput3) end}) questionInput4.tween = transition.to(questionInput4, {alpha = 1, transition.easingOutExpo, time = transitionTime, onComplete = function() screen:cancelTween(questionInput4) end}) end function screen:cancelTween(obj) if obj.tween ~= nil then transition.cancel(obj.tween) obj.tween = nil end end function screen:hide() end screen:initialize() screen:show() return screen end return questionScreen
QUESTION CLASS
local Questions = class() Questions.\_\_name = "Questions" function Questions:\_\_init() end function Questions:generator() Runtime:addEventListener("checkButtonTouched", function() if ( "ended" == event.phase ) then if questionScreen.questionInput1.text ~= "Yes" then local alert = native.showAlert( "You have incorrect answers" ) elseif questionScreen.questionInput2.text ~= "Yes" then local alert = native.showAlert( "You have incorrect answers" ) elseif questionScreen.questionInput3.text ~= "Yes" then local alert = native.showAlert( "You have incorrect answers" ) elseif questionScreen.questionInput4.text ~= "Yes" then local alert = native.showAlert( "You have incorrect answers" ) else local alert = native.showAlert( "Very nice" ) end end end) end return Questions