Hey I need help trying to change scene using parallax. I got a level select menu, but when I click on a level to go to or back button, the scene would not change. In the code what am I missing so the scene changing can work below?
[code]module(…, package.seeall)
require “ui”
require “movieclip”
local ui = require(“ui”)
local parallax = require( “parallax” )
function new()
local localGroup = display.newGroup()
local myScene = parallax.newScene(
{
width = 960,
height = 320,
top = 0,
left = 0,
infinite = true
} )
– add the near layer
local nearLayer = myScene:newLayer(
{
image = “bk.png”,
width = 480,
height = 320,
top = 0,
left = 0
} )
nearLayer.alpha = 0.7
– add the far layer
myScene:newLayer(
{
image = “bk.png”,
width = 480,
height = 320,
top = 0,
left = 0,
speed = 0.2,
repeated = true
} )
backgroundMusic = audio.loadStream(“My Song.m4a”)
backgroundMusicChannel = audio.play( backgroundMusic, { channel=20, loops=-1, fadein=10000 } ) – play the background music on channel 1, loop infinitely, and fadein over 5 seconds
– play the laser on any available channel
local function onback (event)
if event.phase == “release” then
director:changeScene (“menu”, “moveFromLeft”)
end
end
local function checkForFile ()
currentLevel = loadFile (“currentLevel.txt”)
if currentLevel == “empty” then
currentLevel = 1
saveFile(“currentLevel.txt”, currentLevel)
end
end
checkForFile()
back = ui.newButton{
default = “arrow1.png”,
over = “arrow3.png”,
x = 30,
y = 30,
onEvent = onback,
}
localGroup:insert(back)
myScene:insertObj( back)
local text = display.newText(“Level Select”, 170, 10,“Times New Roman”, 30)
localGroup:insert(text)
myScene:insertObj( text)
–Table for levels
local level = {}
–Table for level text
local levelText = {}
–Function for open levels
local function goLevel (event)
director:changeScene(event.target.scene)
audio.stop( backgroundMusicChannel )
end
local startX = 45
iconPos = {}
iconPos[1] = {x=70, y=80}
iconPos[2] = {x=140, y=80}
iconPos[3] = {x=210, y=80}
iconPos[4] = {x=280, y=80}
iconPos[5] = {x=350, y=80}
iconPos[6] = {x=420, y=80}
iconPos[7] = {x=70, y=140}
iconPos[8] = {x=140, y=140}
iconPos[9] = {x=210, y=140}
iconPos[10] = {x=280, y=140}
iconPos[11] = {x=350, y=140}
iconPos[12] = {x=420, y=140}
iconPos[13] = {x=70, y=200}
iconPos[14] = {x=140, y=200}
iconPos[15] = {x=210, y=200}
iconPos[16] = {x=280, y=200}
iconPos[17] = {x=350, y=200}
iconPos[18] = {x=420, y=200}
iconPos[19] = {x=70, y=260}
iconPos[20] = {x=140, y=260}
iconPos[21] = {x=210, y=260}
iconPos[22] = {x=280, y=260}
iconPos[23] = {x=350, y=260}
iconPos[24] = {x=420, y=260}
iconPos[25] = {x=540, y=80}
iconPos[26] = {x=610, y=80}
iconPos[27] = {x=680, y=80}
iconPos[28] = {x=750, y=80}
iconPos[29] = {x=820, y=80}
iconPos[30] = {x=890, y=80}
iconPos[31] = {x=540, y=140}
iconPos[32] = {x=610, y=140}
iconPos[33] = {x=680, y=140}
iconPos[34] = {x=750, y=140}
iconPos[35] = {x=820, y=140}
iconPos[36] = {x=890, y=140}
iconPos[37] = {x=540, y=200}
iconPos[38] = {x=610, y=200}
iconPos[39] = {x=680, y=200}
iconPos[40] = {x=750, y=200}
iconPos[41] = {x=820, y=200}
iconPos[42] = {x=890, y=200}
iconPos[43] = {x=540, y=260}
iconPos[44] = {x=610, y=260}
iconPos[45] = {x=680, y=260}
iconPos[46] = {x=750, y=260}
iconPos[47] = {x=820, y=260}
iconPos[48] = {x=890, y=260}
iconPos2 = {}
iconPos2[1] = {x=70, y=80}
iconPos2[2] = {x=140, y=80}
iconPos2[3] = {x=210, y=80}
iconPos2[4] = {x=280, y=80}
iconPos2[5] = {x=350, y=80}
iconPos2[6] = {x=420, y=80}
iconPos2[7] = {x=70, y=140}
iconPos2[8] = {x=140, y=140}
iconPos2[9] = {x=210, y=140}
iconPos2[10] = {x=280, y=140}
iconPos2[11] = {x=350, y=140}
iconPos2[12] = {x=420, y=140}
iconPos2[13] = {x=70, y=200}
iconPos2[14] = {x=140, y=200}
iconPos2[15] = {x=210, y=200}
iconPos2[16] = {x=280, y=200}
iconPos2[17] = {x=350, y=200}
iconPos2[18] = {x=420, y=200}
iconPos2[19] = {x=70, y=260}
iconPos2[20] = {x=140, y=260}
iconPos2[21] = {x=210, y=260}
iconPos2[22] = {x=280, y=260}
iconPos2[23] = {x=350, y=260}
iconPos2[24] = {x=420, y=260}
iconPos2[25] = {x=540, y=80}
iconPos2[26] = {x=610, y=80}
iconPos2[27] = {x=680, y=80}
iconPos2[28] = {x=750, y=80}
iconPos2[29] = {x=820, y=80}
iconPos2[30] = {x=890, y=80}
iconPos2[31] = {x=540, y=140}
iconPos2[32] = {x=610, y=140}
iconPos2[33] = {x=680, y=140}
iconPos2[34] = {x=750, y=140}
iconPos2[35] = {x=820, y=140}
iconPos2[36] = {x=890, y=140}
iconPos2[37] = {x=540, y=200}
iconPos2[38] = {x=610, y=200}
iconPos2[39] = {x=680, y=200}
iconPos2[40] = {x=750, y=200}
iconPos2[41] = {x=820, y=200}
iconPos2[42] = {x=890, y=200}
iconPos2[43] = {x=540, y=260}
iconPos2[44] = {x=610, y=260}
iconPos2[45] = {x=680, y=260}
iconPos2[46] = {x=750, y=260}
iconPos2[47] = {x=820, y=260}
iconPos2[48] = {x=890, y=260}
–Load level icons accordingly
local function setupLevels()
for i = 1, 48 do
if tonumber(currentLevel) >= i then
level[i] = display.newImageRect(“moon.png”, 50, 50)
level[i].x, level[i].y = startX*i, 50
level[i].x, level[i].y = iconPos[i].x, iconPos[i].y
localGroup:insert( level[i] )
myScene:insertObj( level[i] )
level[i].scene = “level”…i…""
level[i]:addEventListener(“tap”, goLevel)
levelText[i] = display.newText(""…i…"", startX*i-2, 45, “Arial”, 20)
levelText[i]:setTextColor(0,0,0)
levelText[i].x, levelText[i].y = iconPos2[i].x, iconPos2[i].y
localGroup:insert( levelText[i] )
myScene:insertObj( levelText[i] )
elseif tonumber(currentLevel) <= i then
level[i] = display.newImageRect(“moonl.png”, 50, 50)
level[i].x, level[i].y = startX*i, 50
level[i].x, level[i].y = iconPos[i].x, iconPos[i].y
localGroup:insert( level[i] )
myScene:insertObj( level[i] )
levelText[i] = display.newText(""…i…"", startX*i-2, 45, “Arial”, 20)
levelText[i]:setTextColor(255,255,255)
levelText[i].x, levelText[i].y = iconPos2[i].x, iconPos2[i].y
localGroup:insert( levelText[i] )
myScene:insertObj( levelText[i] )
end
end
end
setupLevels()
local function onTouch( event )
local phase = event.phase
if phase == “began” then
– set scene to ‘focused’
display.getCurrentStage():setFocus( myScene, event.id )
– store location as previous
myScene.xPrev = event.x
elseif phase == “moved” then
– move scene as the event moves
myScene:move( myScene.xPrev - event.x, 0 )
– store location as previous
myScene.xPrev = event.x
elseif phase == “ended” or phase == “cancelled” then
– un-focus scene
display.getCurrentStage():setFocus( myScene, nil )
end
return true
end
– Events
myScene:addEventListener( “touch”, onTouch )
return localGroup
end [/code]
I would like some help how to change scene because when I click on back button or level it would not change. Please help thanks
[import]uid: 17058 topic_id: 21280 reply_id: 321280[/import]