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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | 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 |
sorry for being impatient but I really need help on this.
no worries i got it
Sorry, I didn't see this until now (got up late today ;)) - glad to hear it is sorted. Would you mind posting your solution for others who it might help in the future?
Peach :)