Advice for Changing Level Between Game

First thing I just learned Corona for past few months and I would like to ask some questions to the expert corona’s community. I started to make this game with different files main.lua and game.lua .

My game concept was simple, just dragging the 3 objects and matching the color to the up box, if same color means RIGHT then it will delete objects of level 1 and change to second level.

My problem is I can’t change to the second level after the answer is right and I separate my dragging function different with my changing level function.

This is my example code:
main.lua
[lua][code]
local game = require(“game”)

local w = display.contentWidth*0.5
local h = display.contentHeight*0.5

local nextScene = false
local nextLevel = false
local frame = 0
local level = 1
local obj1 = nil

local function instruction()
text = display.newText(" drag the objects \n\ to match the color", w - 180 , h + 350 , nil , 40)
end


— delete level 1 and trigger next level

local function deleteLevel()
level = level + 1
if level == 2 then
scene1:removeSelf() – delete level 1
elseif level == 3 then
print("-- delete level 2 --")
end
nextScene = true – start to play level 2
end

– local function checkObj() — check objects then jump to deleteLevel function
– obj = game.deleteGroup()
– deleteLevel()
– end


— play level

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

frame = frame + 1
if frame == 5 then
if level == 1 then
scene1.isVisible = true
elseif level == 2 then
print("-- start level 2 --")
scene2.isVisible = true
end
elseif frame == 20 then
frame = 0
nextScene = false

print("<< level = “… level…” >>")
end
– print("frame = "…frame)
end


— start game

local function main()
instruction()

scene1 = display.newGroup( )
scene2 = display.newGroup( )

– object for level 1
box1 = game.rect1_level1()
box1.x = w - 180
box1.y = h + 250
box1:setFillColor(0, 255, 255)

box2 = game.rect2_level1()
box2.x = w
box2.y = box1.y
box2:setFillColor(255, 0, 255)

box3 = game.rect3_level1()
box3.x = w + 180
box3.y = box1.y
box3:setFillColor(255, 255, 0)

answerBox = game.rectAnswer_level1()
answerBox.x = w
answerBox.y = h - 250
answerBox:setFillColor(0, 255, 255)

scene1:insert(answerBox)
scene1:insert(box1)
scene1:insert(box2)
scene1:insert(box3)
scene1.isVisible = false

– object for level 2
local box1 = game.rect1_level2()
box1.x = w - 180
box1.y = h + 250
box1:setFillColor(20, 50, 150)

box2 = game.rect2_level2()
box2.x = w
box2.y = box1.y
box2:setFillColor(200, 100, 55)

box3 = game.rect3_level2()
box3.x = w + 180
box3.y = box1.y
box3:setFillColor(55, 255, 0)

answerBox = game.rectAnswer_level2()
answerBox.x = w
answerBox.y = h - 250
answerBox:setFillColor(55, 255, 0)

scene2:insert(answerBox)
scene2:insert(box1)
scene2:insert(box2)
scene2:insert(box3)
scene2.isVisible = false


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

main()
[/code][/lua]

game.lua
[lua][code]
module(…, package.seeall)

local w = display.contentWidth*0.5
local h = display.contentHeight*0.5

local right = 0
local wrong = 0


— objects for level 1

function rectAnswer_level1()

rectAnswer = display.newRect(0, 0, 100 , 100)
return rectAnswer

end

function rect1_level1()

box1 = display.newRect(0, 0, 100, 100)
box1:addEventListener(“touch”, startDrag)
return box1

end

function rect2_level1()

box2 = display.newRect(0, 0, 100, 100)
box2:addEventListener(“touch”, startDrag)
return box2

end

function rect3_level1()

box3 = display.newRect(0, 0, 100, 100)
box3:addEventListener(“touch”, startDrag)
return box3

end


— objects for level 2

function rectAnswer_level2()

rectAnswer_2 = display.newRect(0, 0, 100 , 100)
return rectAnswer_2

end

function rect1_level2()

box4 = display.newRect(0, 0, 100, 100)
box4:addEventListener(“touch”, startDrag)
return box4

end

function rect2_level2()

box5 = display.newRect(0, 0, 100, 100)
box5:addEventListener(“touch”, startDrag)
return box5

end

function rect3_level2()

box6 = display.newRect(0, 0, 100, 100)
box6:addEventListener(“touch”, startDrag)
return box6

end


— check answer right and wrong

function checkRight(obj)
right = right + 1
print("right = "…right)
end

function checkWrong()
wrong = wrong + 1
print("wrong = "…wrong)
end


— dragging function

function startDrag(event)
local phase = event.phase
local t = event.target
local area_right = ((t.x > 250) and (t.x < 350) and (t.y > 210) and (t.y < 310))

if phase == “began” then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
t.y0 = event.y - t.y

elseif t.isFocus then
if phase == “moved” then
t.x = event.x - t.x0
t.y = event.y - t.y0
elseif phase == “ended” then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
if area_right then
– object level 1
if box1 == t then – box1 = right then it will be desappeared
box1.isVisible = false
checkRight()
elseif box2 == t then
box2.x0 = t.x
box2.y0 = t.y
transition.to( box2, {time = 200, x = w , y = h + 250})
checkWrong()
elseif box3 == t then
box3.x0 = t.x
box3.y0 = t.y
transition.to( box3, {time = 200, x = w + 180 , y = h + 250})
checkWrong()
– object level 2
elseif box4 == t then
box4.x0 = t.x
box4.y0 = t.y
transition.to( box4, {time = 200, x = w - 180, y = h + 250})
checkWrong()
elseif box5 == t then
box5.x0 = t.x
box5.y0 = t.y
transition.to( box5, {time = 200, x = w , y = h + 250})
checkWrong()
elseif box6 == t then
box6.isVisible = false
checkRight()
end
end
– return to the first position
if box1 == t then
box1.x0 = t.x
box1.y0 = t.y
transition.to( box1, {time = 200, x = w - 180, y = h + 250})
elseif box2 == t then
box2.x0 = t.x
box2.y0 = t.y
transition.to( box2, {time = 200, x = w , y = h + 250})
elseif box3 == t then
box3.x0 = t.x
box3.y0 = t.y
transition.to( box3, {time = 200, x = w + 180 , y = h + 250})
elseif box4 == t then
box4.x0 = t.x
box4.y0 = t.y
transition.to( box4, {time = 200, x = w - 180, y = h + 250})
elseif box5 == t then
box5.x0 = t.x
box5.y0 = t.y
transition.to( box5, {time = 200, x = w , y = h + 250})
elseif box6 == t then
box6.x0 = t.x
box6.y0 = t.y
transition.to( box6, {time = 200, x = w + 180 , y = h + 250})
end
end
end
return true
end
[/code][/lua] [import]uid: 64917 topic_id: 10585 reply_id: 310585[/import]

help please… [import]uid: 64917 topic_id: 10585 reply_id: 38521[/import]

Hey there,

Have you considered using Director for this?

It is newbie friendly and you’re clearly more advanced than that already - it might just be EASIER to work with.

Peach :slight_smile: [import]uid: 52491 topic_id: 10585 reply_id: 38583[/import]

Thanks for the advice peach :slight_smile:
at the beginning I thought to use director too but it would create more files and repeating functions, it won’t efficiently enough.

My RENDER function is to play the level of the game, if the color of the box is correct, it will directly check the answer is right in the game.lua file and then I wanna link the function to trigger the deleteGroup() in the main.lua and then play the second level.

The second level doesn’t work after the answer of first level is right and I just wanna know, is it have the way to run the function again and again based on my code with calling function from one file to the others… [import]uid: 64917 topic_id: 10585 reply_id: 38591[/import]