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]
