//How am i able to create a scoring number here? the situation is the function questiongen() is use to add 2 numbers and the function //called checkAnswer() is use to check if the answer is correct or wrong. my goal here is to create a scoring number in which if the answer is correct //the scoretxt = 0 will be added + 1 and if the answer is wrong it will stay just the same. function questionGen() local questionVar1 = display.newImage("ygSquare.png", 150, 500); local var1 = math.random(1,9) local var1Display =display.newText(var1, 200, 500, native.systemFont, 200) questionVar1.width = 200 questionVar1.height = 200 questionVar1.x = 350 questionVar1.y = 500 var1Display.x = 350 var1Display.y = 500 var1Display:setTextColor("#000000") local questionVar2 = display.newImage("blueSquare.png", 150, 500); local var2 = math.random(1,9) local var2Display = display.newText(var2, 200, 500, native.systemFont, 200) questionVar2.width = 200 questionVar2.height = 200 questionVar2.x = 700 questionVar2.y = 500 var2Display.x = 700 var2Display.y = 500 var2Display:setTextColor("#000000") local operator = " + " local operatorDisplay = display.newText(operator, 400, 500, native.systemFont, 200) operatorDisplay:setTextColor("#000000") local varAnswer = var1 + var2 return varAnswer end function checkAnswer(event) if(#theAnswer == questionGen()) then instructionsText.text = "Correct!"; instructionsText:toFront() generateBalls() for i=1, allBallsGroup.numChildren do display.remove(allBallsGroup[1]) end else instructionsText.text = "Incorrect!"; instructionsText:toFront() generateBalls() for i=1, allBallsGroup.numChildren do display.remove(allBallsGroup[1]) end end end function addscore() local score = addscoreText.text countDownText.text = score if(#theAnswer == questionGen()) then score = score + 1 else score = score ~+ 1 end end
I would make score local to the module, not local to the function.
Remove lines 66 and 67 they are not needed.
after line 65 (score = score + 1) insert this line
addscoreText.text = score
the code about countDownText.text = score is a little confusing. I cannot see all the code, so I am not sure why you are adding score to countDownText. I can guess, but it is not relevant to your question as I read it.
I guess maybe the countDownText is how many questions were asked or maybe failed(I am just guessing). I would try to keep score and countDown separate. I apologize if I have guessed wrong here, but without more code I can only guess in an effort to help.
good luck
bob
Line 63 supposed to be
addscoreText.text= score
and yes sir bob i already added that line but the score is still 0 and won’t add up… and yes the code is not complete i removed other functions that is not related on my scoring…
here is my complete code
local composer = require( "composer" ) local scene = composer.newScene() local widget = require "widget" composer.removeScene( "Subtraction",true) composer.removeScene( "Multiplication",true) composer.removeScene( "Division",true) composer.removeHidden( "operations",true) local function onBackBtnRelease() composer.gotoScene( "operations", "fade", 0 ) composer.removeScene( "Addition") --composer.removeScene( "Subtraction",true) --composer.removeScene( "Multiplication",true) --composer.removeScene( "Division",true) return true -- indicates successful touch end function scene:create( event ) local sceneGroup = self.view display.setStatusBar(display.HiddenStatusBar); local physics = require("physics"); physics.start(true) physics.setGravity(0,50); --physics.setDrawMode( "hybrid" ) local gameGround = display.newImage("Ground.png",550,1760) local gameBackground = display.newImage("addition.png",550,950) local theAnswer= ""; local theAnswerText; local isNewGame = true local scoreText local allBallsGroup local chosenBalls = {} local instructionsText local countDownText local addScoreText local numBallsToGenerate local allBalls = {} local generateBallTimer local clockTimer local gameTime = 40 local gameScore = 0 local generateQuestionTimer function setup() math.randomseed(os.time()); setupGameAssets() newGame() local scoreText = "SCORE: " local scoreDisplay = display.newText(scoreText, 350, 300, native.systemFont, 100) scoreDisplay:setTextColor("#000000") end function newGame() isNewGame = true chosenBalls = {} allBalls = {} theAnswer = "" theAnswerText.text = "" instructionsText.text = "" countDownText.text = gameTime; addScoreText.text = gameScore; createTensBalls(2) generateBalls() startTimer() isNewGame = false createBall(varAnswer) end function setupGameBoundaries() local groundLine = display.newRect(500, 1600,display.contentWidth, 2) local lineLeft = display.newRect(-50,2000,2, display.contentHeight) local lineRight = display.newRect(display.contentWidth-1,2000,-50,display.contentHeight) physics.addBody(groundLine, 'static',{bounce=0,friction=0}) physics.addBody(lineLeft, 'static',{bounce=0,friction=0}) physics.addBody(lineRight,'static',{bounce=0,friction=0}) groundLine.isVisible = false lineLeft.isVisible = false lineRight.isVisible = false end function setupTextFields() countDownText = display.newText(gameTime,950,100,native.systemFontBold,150) countDownText:setTextColor("#000000") addScoreText = display.newText(gameScore,500,100,native.systemFontBold,150) countDownText:setTextColor("#000000") theAnswerText = display.newText("",900,500,native.systemFontBold,200) theAnswerText:setTextColor("#000000"); instructionsText = display.newText("",550,750,native.systemFontBold,100) instructionsText:setTextColor("#000000") end function setupGameAssets() setupGameBoundaries() setupTextFields() allBallsGroup = display.newGroup(); allQuestionGroup = display.newGroup(); end function generateBalls() timer.performWithDelay(0) if(isNewGame == true) then numBallsToGenerate = 1 else numBallsToGenerate = 4 createTensBalls(1) end generateBallTimer = timer.performWithDelay( 50, createBall, numBallsToGenerate) generateQuestionTimer = timer.performWithDelay( 40000, questionGen) end function startTimer() clockTimer = timer.performWithDelay(1000,doCountdown,gameTime) end function doCountdown() local currentTime = countDownText.text currentTime = currentTime -1 countDownText.text = currentTime if(currentTime == 0) then for i = 1, allBallsGroup.numChildren do display.remove(allBallsGroup[1]) end generateBalls() countDownText.text = gameTime startTimer() resetAnswer() end end function createBall() local var numberArray = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "11", "12", "13", "14", "15", "16", "17", "18"}; local tens = {questionGen(varAnswer)} local ballType = math.random(8); local ballSize = math.random(2) local letterIndex local letter if(createTens == true) then letterIndex = math.random(#tens) letter = tens[letterIndex]; else letterIndex = math.random(#numberArray); letter = numberArray[letterIndex]; letterIndex = math.random(#tens) letter = tens[letterIndex]; end local ballGroup = display.newGroup(); local ball local ballRadius if(ballType == 1) then ball = display.newImage("whiteBall.png"); elseif(ballType == 2) then ball = display.newImage("brownBall.png"); elseif(ballType == 3) then ball = display.newImage("pinkBall.png"); elseif(ballType == 4) then ball = display.newImage("whiteBall.png"); elseif(ballType == 5) then ball = display.newImage("yellowBall.png"); elseif(ballType == 6) then ball = display.newImage("whiteBall.png"); elseif(ballType == 7) then ball = display.newImage("greenBall.png"); else ball = display.newImage("redBall.png"); end if(ballSize == 1)then ball.width = 200; ball.height = 200; ballRadius = 30; else ball.width = 200; ball.height = 200; ballRadius = 30; end local letterText = display.newText( letter, 0,0, native.systemFontBold, 100 ); letterText:setTextColor(0,0, 0); letterText.x = ball.x; letterText.y = ball.y; ballGroup:insert(ball); ballGroup:insert(letterText); ballGroup.x = math.random(ballRadius,display.contentWidth-ballRadius\*3); ballGroup.y= 40; physics.addBody(ballGroup, 'dynamic',{friction = 0,bounce = 0,radius = ballRadius\*3}); ballGroup.name = "ball"; ballGroup.letter = letter; ballGroup:addEventListener('touch',checkAnswer, addScore); table.insert(allBalls,ballGroup) allBallsGroup:insert(ballGroup) end function checkAnswer(event) if(#theAnswer == questionGen()) then instructionsText.text = "Correct!"; instructionsText:toFront() generateBalls() for i=1, allBallsGroup.numChildren do display.remove(allBallsGroup[1]) end else instructionsText.text = "Incorrect!"; instructionsText:toFront() generateBalls() for i=1, allBallsGroup.numChildren do display.remove(allBallsGroup[1]) end end end function addScore() local score = addscoreText.text addscoreText.text = score if(#theAnswer == questionGen()) then score = score + 1 addScoreText.text = gameScore end end function resetAnswer() instructionsText.text = ""; theAnswer= '' theAnswerText.text = "" chosenBalls = {} end function createTensBalls(number) for i=1, number do createBall(true) end end function readTextFile() local path = system.pathForFile( "wordlist.txt", system.ResourceDirectory) local file = io.open( path, "r" ) for line in file:lines() do line = string.sub(line, 1, #line - 1); table.insert(wordTable,line) end io.close( file ) file = nil end --local bgMusic = audio.loadStream('sound.mp3') --audio.play(bgMusic) function questionGen() local questionVar1 = display.newImage("ygSquare.png", 150, 500); local var1 = math.random(1,9) local var1Display =display.newText(var1, 200, 500, native.systemFont, 200) questionVar1.width = 200 questionVar1.height = 200 questionVar1.x = 350 questionVar1.y = 500 var1Display.x = 350 var1Display.y = 500 var1Display:setTextColor("#000000") local questionVar2 = display.newImage("blueSquare.png", 150, 500); local var2 = math.random(1,9) local var2Display = display.newText(var2, 200, 500, native.systemFont, 200) questionVar2.width = 200 questionVar2.height = 200 questionVar2.x = 700 questionVar2.y = 500 var2Display.x = 700 var2Display.y = 500 var2Display:setTextColor("#000000") local operator = " + " local operatorDisplay = display.newText(operator, 400, 500, native.systemFont, 200) operatorDisplay:setTextColor("#000000") local varAnswer = var1 + var2 return varAnswer end setup() backBtn = widget.newButton{ label="Back", fontSize = 100, labelColor = { default={000000}, over={128} }, default="back.png", width=200, height=50, onRelease = onBackBtnRelease -- event listener function } backBtn.x = 250 backBtn.y = 1700 end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene
You need to put that addscoreText.text= score below where you’ve added 1 to the score – if it’s really at line 63 then you’re pulling score out, then putting it right back in, unchanged.
Jay
I would make score local to the module, not local to the function.
Remove lines 66 and 67 they are not needed.
after line 65 (score = score + 1) insert this line
addscoreText.text = score
the code about countDownText.text = score is a little confusing. I cannot see all the code, so I am not sure why you are adding score to countDownText. I can guess, but it is not relevant to your question as I read it.
I guess maybe the countDownText is how many questions were asked or maybe failed(I am just guessing). I would try to keep score and countDown separate. I apologize if I have guessed wrong here, but without more code I can only guess in an effort to help.
good luck
bob
Line 63 supposed to be
addscoreText.text= score
and yes sir bob i already added that line but the score is still 0 and won’t add up… and yes the code is not complete i removed other functions that is not related on my scoring…
here is my complete code
local composer = require( "composer" ) local scene = composer.newScene() local widget = require "widget" composer.removeScene( "Subtraction",true) composer.removeScene( "Multiplication",true) composer.removeScene( "Division",true) composer.removeHidden( "operations",true) local function onBackBtnRelease() composer.gotoScene( "operations", "fade", 0 ) composer.removeScene( "Addition") --composer.removeScene( "Subtraction",true) --composer.removeScene( "Multiplication",true) --composer.removeScene( "Division",true) return true -- indicates successful touch end function scene:create( event ) local sceneGroup = self.view display.setStatusBar(display.HiddenStatusBar); local physics = require("physics"); physics.start(true) physics.setGravity(0,50); --physics.setDrawMode( "hybrid" ) local gameGround = display.newImage("Ground.png",550,1760) local gameBackground = display.newImage("addition.png",550,950) local theAnswer= ""; local theAnswerText; local isNewGame = true local scoreText local allBallsGroup local chosenBalls = {} local instructionsText local countDownText local addScoreText local numBallsToGenerate local allBalls = {} local generateBallTimer local clockTimer local gameTime = 40 local gameScore = 0 local generateQuestionTimer function setup() math.randomseed(os.time()); setupGameAssets() newGame() local scoreText = "SCORE: " local scoreDisplay = display.newText(scoreText, 350, 300, native.systemFont, 100) scoreDisplay:setTextColor("#000000") end function newGame() isNewGame = true chosenBalls = {} allBalls = {} theAnswer = "" theAnswerText.text = "" instructionsText.text = "" countDownText.text = gameTime; addScoreText.text = gameScore; createTensBalls(2) generateBalls() startTimer() isNewGame = false createBall(varAnswer) end function setupGameBoundaries() local groundLine = display.newRect(500, 1600,display.contentWidth, 2) local lineLeft = display.newRect(-50,2000,2, display.contentHeight) local lineRight = display.newRect(display.contentWidth-1,2000,-50,display.contentHeight) physics.addBody(groundLine, 'static',{bounce=0,friction=0}) physics.addBody(lineLeft, 'static',{bounce=0,friction=0}) physics.addBody(lineRight,'static',{bounce=0,friction=0}) groundLine.isVisible = false lineLeft.isVisible = false lineRight.isVisible = false end function setupTextFields() countDownText = display.newText(gameTime,950,100,native.systemFontBold,150) countDownText:setTextColor("#000000") addScoreText = display.newText(gameScore,500,100,native.systemFontBold,150) countDownText:setTextColor("#000000") theAnswerText = display.newText("",900,500,native.systemFontBold,200) theAnswerText:setTextColor("#000000"); instructionsText = display.newText("",550,750,native.systemFontBold,100) instructionsText:setTextColor("#000000") end function setupGameAssets() setupGameBoundaries() setupTextFields() allBallsGroup = display.newGroup(); allQuestionGroup = display.newGroup(); end function generateBalls() timer.performWithDelay(0) if(isNewGame == true) then numBallsToGenerate = 1 else numBallsToGenerate = 4 createTensBalls(1) end generateBallTimer = timer.performWithDelay( 50, createBall, numBallsToGenerate) generateQuestionTimer = timer.performWithDelay( 40000, questionGen) end function startTimer() clockTimer = timer.performWithDelay(1000,doCountdown,gameTime) end function doCountdown() local currentTime = countDownText.text currentTime = currentTime -1 countDownText.text = currentTime if(currentTime == 0) then for i = 1, allBallsGroup.numChildren do display.remove(allBallsGroup[1]) end generateBalls() countDownText.text = gameTime startTimer() resetAnswer() end end function createBall() local var numberArray = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "11", "12", "13", "14", "15", "16", "17", "18"}; local tens = {questionGen(varAnswer)} local ballType = math.random(8); local ballSize = math.random(2) local letterIndex local letter if(createTens == true) then letterIndex = math.random(#tens) letter = tens[letterIndex]; else letterIndex = math.random(#numberArray); letter = numberArray[letterIndex]; letterIndex = math.random(#tens) letter = tens[letterIndex]; end local ballGroup = display.newGroup(); local ball local ballRadius if(ballType == 1) then ball = display.newImage("whiteBall.png"); elseif(ballType == 2) then ball = display.newImage("brownBall.png"); elseif(ballType == 3) then ball = display.newImage("pinkBall.png"); elseif(ballType == 4) then ball = display.newImage("whiteBall.png"); elseif(ballType == 5) then ball = display.newImage("yellowBall.png"); elseif(ballType == 6) then ball = display.newImage("whiteBall.png"); elseif(ballType == 7) then ball = display.newImage("greenBall.png"); else ball = display.newImage("redBall.png"); end if(ballSize == 1)then ball.width = 200; ball.height = 200; ballRadius = 30; else ball.width = 200; ball.height = 200; ballRadius = 30; end local letterText = display.newText( letter, 0,0, native.systemFontBold, 100 ); letterText:setTextColor(0,0, 0); letterText.x = ball.x; letterText.y = ball.y; ballGroup:insert(ball); ballGroup:insert(letterText); ballGroup.x = math.random(ballRadius,display.contentWidth-ballRadius\*3); ballGroup.y= 40; physics.addBody(ballGroup, 'dynamic',{friction = 0,bounce = 0,radius = ballRadius\*3}); ballGroup.name = "ball"; ballGroup.letter = letter; ballGroup:addEventListener('touch',checkAnswer, addScore); table.insert(allBalls,ballGroup) allBallsGroup:insert(ballGroup) end function checkAnswer(event) if(#theAnswer == questionGen()) then instructionsText.text = "Correct!"; instructionsText:toFront() generateBalls() for i=1, allBallsGroup.numChildren do display.remove(allBallsGroup[1]) end else instructionsText.text = "Incorrect!"; instructionsText:toFront() generateBalls() for i=1, allBallsGroup.numChildren do display.remove(allBallsGroup[1]) end end end function addScore() local score = addscoreText.text addscoreText.text = score if(#theAnswer == questionGen()) then score = score + 1 addScoreText.text = gameScore end end function resetAnswer() instructionsText.text = ""; theAnswer= '' theAnswerText.text = "" chosenBalls = {} end function createTensBalls(number) for i=1, number do createBall(true) end end function readTextFile() local path = system.pathForFile( "wordlist.txt", system.ResourceDirectory) local file = io.open( path, "r" ) for line in file:lines() do line = string.sub(line, 1, #line - 1); table.insert(wordTable,line) end io.close( file ) file = nil end --local bgMusic = audio.loadStream('sound.mp3') --audio.play(bgMusic) function questionGen() local questionVar1 = display.newImage("ygSquare.png", 150, 500); local var1 = math.random(1,9) local var1Display =display.newText(var1, 200, 500, native.systemFont, 200) questionVar1.width = 200 questionVar1.height = 200 questionVar1.x = 350 questionVar1.y = 500 var1Display.x = 350 var1Display.y = 500 var1Display:setTextColor("#000000") local questionVar2 = display.newImage("blueSquare.png", 150, 500); local var2 = math.random(1,9) local var2Display = display.newText(var2, 200, 500, native.systemFont, 200) questionVar2.width = 200 questionVar2.height = 200 questionVar2.x = 700 questionVar2.y = 500 var2Display.x = 700 var2Display.y = 500 var2Display:setTextColor("#000000") local operator = " + " local operatorDisplay = display.newText(operator, 400, 500, native.systemFont, 200) operatorDisplay:setTextColor("#000000") local varAnswer = var1 + var2 return varAnswer end setup() backBtn = widget.newButton{ label="Back", fontSize = 100, labelColor = { default={000000}, over={128} }, default="back.png", width=200, height=50, onRelease = onBackBtnRelease -- event listener function } backBtn.x = 250 backBtn.y = 1700 end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene
You need to put that addscoreText.text= score below where you’ve added 1 to the score – if it’s really at line 63 then you’re pulling score out, then putting it right back in, unchanged.
Jay