Simple sum and subtraction math game

Hi corona community I’ve problem about my math game and I can’t figure it out how to fix the problem.

When I start for the first level, it will display randomly between sum or subtraction question then when I choose the right answer then it will jump to the second level.

The problem is overlapping object when the first level is displayed between sum or subtraction function then when change to the second level with the same function. Ex; level 1 = sum then level 2 = sum, it will create overlapping object.

Conversely, if the first level is sum then the second level is subtraction then the problem can be solved. I don’t know how to make my each level will be disappeared when the first level and second level display the same math function.

This is my code:
[lua]math.randomseed(os.time())
local mRand = math.random
local w = display.contentWidth*0.5
local h = display.contentHeight*0.5

local right
local frame = 0
local level = 1
local nextScene = false

– sum
local sumGroup = display.newGroup()
local choiceSum = {}
local cSum = {}
local sum1, sum2
local sum, equalSum, answerSum
local numSum1, numSum2, numSum3, numSum4
– subtraction
local subGroup = display.newGroup()
local choiceSub = {}
local cSub = {}
local sub1, sub2
local sub, equalSub, answerSub
local numSub1, numSub2, numSub3, numSub4

local checkRight = function()
level = level + 1
if level == 2 then
if sum then
sumGroup.isVisible = false
elseif sub then
subGroup.isVisible = false
end
elseif level == 3 then
if sum then
sumGroup.isVisible = false
elseif sub then
subGroup.isVisible = false
end
end
nextScene = true
end

local touchMe = function(event)
local t = event.target
local phase = event.phase
if phase == “ended” then
if right == tonumber(t.text) then
print(t.text … " right")
checkRight()
else
print(t.text … " wrong")
end
end
return true
end

local shuffle = function(t)
local iterations = #t
local j
for i = iterations, 2, -1 do
j = mRand(i)
t[i], t[j] = t[j], t[i]
end
end

local randomSum = function()
numSum1 = mRand(1, 9)
numSum2 = mRand(1, 9)
numSum3 = mRand(16, 20)
numSum4 = mRand(11, 15)

sum1.text = numSum1
sum2.text = numSum2

cSum[1] = numSum1 + numSum2
cSum[2] = numSum3 + 8
cSum[3] = numSum4 + 8

right = cSum[1]
shuffle(cSum)

choiceSum[1].text = cSum[1]
choiceSum[2].text = cSum[2]
choiceSum[3].text = cSum[3]
end

local randomSubtraction = function()
numSub1 = mRand(1, 9)
numSub2 = mRand(1, 9)
numSub3 = mRand(16, 20)
numSub4 = mRand(11, 15)

if numSub1 < numSub2 then
numSub1 = numSub2 + 3
end

sub1.text = numSub1
sub2.text = numSub2

cSub[1] = numSub1 - numSub2
cSub[2] = numSub3 - 6
cSub[3] = numSub4 - 9

right = cSub[1]
shuffle(cSub)

choiceSub[1].text = cSub[1]
choiceSub[2].text = cSub[2]
choiceSub[3].text = cSub[3]
end

local sumGame = function()
sum1 = display.newText("?", w - 150, h - 150, nil, 50)
sum2 = display.newText("?", w, h - 150, nil, 50)
sum = display.newText("+", w - 75, h - 150, nil, 50)
equalSum = display.newText("=", w + 75, h - 150, nil, 50)
answerSum = display.newText("?", w + 150, h - 150, nil, 50)

choiceSum[1] = display.newText("?", w - 100, h, nil, 50)
choiceSum[2] = display.newText("?", w, h, nil, 50)
choiceSum[3] = display.newText("?", w + 100, h , nil, 50)

sumGroup:insert(sum1)
sumGroup:insert(sum2)
sumGroup:insert(sum)
sumGroup:insert(equalSum)
sumGroup:insert(answerSum)
sumGroup:insert(choiceSum[1])
sumGroup:insert(choiceSum[2])
sumGroup:insert(choiceSum[3])

sumGroup.isVisible = true

randomSum()
end

local subtractionGame = function()
sub1 = display.newText("?", w - 150, h - 150, nil, 50)
sub2 = display.newText("?", w, h - 150, nil, 50)
sub = display.newText("-", w - 75, h - 150, nil, 50)
equalSub = display.newText("=", w + 75, h - 150, nil, 50)
answerSub = display.newText("?", w + 150, h - 150, nil, 50)

choiceSub[1] = display.newText("?", w - 100, h, nil, 50)
choiceSub[2] = display.newText("?", w, h, nil, 50)
choiceSub[3] = display.newText("?", w + 100, h , nil, 50)

subGroup:insert(sub1)
subGroup:insert(sub2)
subGroup:insert(sub)
subGroup:insert(equalSub)
subGroup:insert(answerSub)
subGroup:insert(choiceSub[1])
subGroup:insert(choiceSub[2])
subGroup:insert(choiceSub[3])

subGroup.isVisible = true

randomSubtraction()
end

local sumListener = function()
choiceSum[1]:addEventListener(“touch”, touchMe)
choiceSum[2]:addEventListener(“touch”, touchMe)
choiceSum[3]:addEventListener(“touch”, touchMe)
end

local subListener = function()
choiceSub[1]:addEventListener(“touch”, touchMe)
choiceSub[2]:addEventListener(“touch”, touchMe)
choiceSub[3]:addEventListener(“touch”, touchMe)
end

local chooseQ = function()
local choose = mRand(2)
if choose == 1 then
sumGame()
sumListener()
elseif choose == 2 then
subtractionGame()
subListener()
end
end

local render = function()
if nextScene == false then
return
end

frame = frame + 1

if frame == 1 then
print("level : "…level)
if level == 1 then
chooseQ()
elseif level == 2 then
chooseQ()
end
elseif frame == 15 then
frame = 0
nextScene = false
end
print(frame)
end

local run = function()
nextScene = true
Runtime:addEventListener(“enterFrame”, render)
end

run()[/lua] [import]uid: 74725 topic_id: 13461 reply_id: 313461[/import]

You need to create all the text elements and insert into the group only once. don’t do that for each question. [import]uid: 71210 topic_id: 13461 reply_id: 49407[/import]

sorry technowand I’m still beginner and i need to know clearly what u’ve said about group things. After I’ve inserted my objects into the group then how can I fix my sum and subtraction function that will display overlapping object between level 1 and level 2?? [import]uid: 74725 topic_id: 13461 reply_id: 49409[/import]

Not an Issue
I have tried to simplify your code…I hope this is the result you are looking for
[lua]math.randomseed(os.time())
local mRand = math.random
local w = display.contentWidth*0.5
local h = display.contentHeight*0.5

local right
local render
local choice = {}
local c = {}
local level = 1
local sumGroup = display.newGroup()

local num1TXT = display.newText("?", w - 150, h - 150, nil, 50)
local num2TXT = display.newText("?", w, h - 150, nil, 50)
local Operator = display.newText("+", w - 75, h - 150, nil, 50)
local equalTXT = display.newText("=", w + 75, h - 150, nil, 50)
local answerTXT = display.newText("?", w + 150, h - 150, nil, 50)

choice[1] = display.newText("?", w - 100, h, nil, 50)
choice[2] = display.newText("?", w, h, nil, 50)
choice[3] = display.newText("?", w + 100, h , nil, 50)

sumGroup:insert(num1TXT)
sumGroup:insert(num2TXT)
sumGroup:insert(Operator)
sumGroup:insert(equalTXT)
sumGroup:insert(answerTXT)
sumGroup:insert(choice[1])
sumGroup:insert(choice[2])
sumGroup:insert(choice[3])

local touchMe = function(event)
local t = event.target
local phase = event.phase
if phase == “ended” then
if right == tonumber(t.text) then
print(t.text … " right")
else
print(t.text … " wrong")
end
level = level + 1
render()
end
end

choice[1]:addEventListener(“touch”, touchMe)
choice[2]:addEventListener(“touch”, touchMe)
choice[3]:addEventListener(“touch”, touchMe)

local shuffle = function(t)
local iterations = #t
local j
for i = iterations, 2, -1 do
j = mRand(i)
t[i], t[j] = t[j], t[i]
end
end

local chooseQ = function()
local choose = mRand(2)
num1 = mRand(1, 9)
num2 = mRand(1, 9)
num3 = mRand(16, 20)
num4 = mRand(11, 15)

num1TXT.text = num1
num2TXT.text = num2

if choose == 1 then
c[1] = num1 + num2
c[2] = num3 + 8
c[3] = num4 + 8
Operator.text =’+’
elseif choose == 2 then
c[1] = num1 - num2
c[2] = num3 - 6
c[3] = num4 - 9
Operator.text =’-’
end
right = c[1]
shuffle©
choice[1].text = c[1]
choice[2].text = c[2]
choice[3].text = c[3]
end

render = function()
print("level : "…level)
chooseQ()
end

render()[/lua] [import]uid: 71210 topic_id: 13461 reply_id: 49410[/import]

ok let me explain what was happening in your code.

when u use display.newText you always create a new display object this happens even if you assign it to same variable,.

[lua]local Group = display.newGroup()
local text = display.newText(“Hello World”, 150, 150, nil, 50)
Group:insert(text)

text = display.newText(“Hello Universe”, 250, 250, nil, 50)
Group:insert(text)[/lua]

when you are showing 1st question addition and second question as subtraction they were in different group and you were hiding the groups with some logic so it worked fine then. when same operation repeats you created another text which actually overlaps the old one.

if you want to create it that way. you have to remove the previously created one using object:removeSelf()
[lua]local Group = display.newGroup()
local text = display.newText(“Hello World”, 150, 150, nil, 50)
Group:insert(text)

text:removeSelf()
text = display.newText(“Hello Universe”, 250, 250, nil, 50)
Group:insert(text)[/lua]

hope this is clear…
if not feel free to ask me… :slight_smile:

[import]uid: 71210 topic_id: 13461 reply_id: 49412[/import]

Thanks for your explanation technowand it’s really help me a lot to understand the way to create the same variables.

I really need to learn more about the basic programming and how the logic run. What’s ur suggestion for me to start that part technowand?
How can I find u if I want to ask something? do u use MIRC??or any other chatting programs? [import]uid: 74725 topic_id: 13461 reply_id: 49420[/import]

You are always welcome… I use gtalk on renjith@technowand.com [import]uid: 71210 topic_id: 13461 reply_id: 49424[/import]