SHOW:
|
|
- or go back to the newest paste.
| 1 | local storyboard = require( "storyboard" ) | |
| 2 | local scene = storyboard.newScene() | |
| 3 | ||
| 4 | ---------------------------------------------------------------------------------- | |
| 5 | -- | |
| 6 | -- NOTE: | |
| 7 | -- | |
| 8 | -- Code outside of listener functions (below) will only be executed once, | |
| 9 | -- unless storyboard.removeScene() is called. | |
| 10 | -- | |
| 11 | --------------------------------------------------------------------------------- | |
| 12 | ||
| 13 | --------------------------------------------------------------------------------- | |
| 14 | -- BEGINNING OF YOUR IMPLEMENTATION | |
| 15 | --------------------------------------------------------------------------------- | |
| 16 | ||
| 17 | local _W = display.contentWidth | |
| 18 | local _H = display.contentHeight | |
| 19 | local mRandom = math.random | |
| 20 | ||
| 21 | local score | |
| 22 | local scoreText | |
| 23 | ||
| 24 | local gameTimer | |
| 25 | local gameTimerText | |
| 26 | local gameTimerBar | |
| 27 | local gameTimerBar_bg | |
| 28 | ||
| 29 | ||
| 30 | local gemsTable = {}
| |
| 31 | local numberOfMarkedToDestroy = 0 | |
| 32 | local gemToBeDestroyed -- used as a placeholder | |
| 33 | local isGemTouchEnabled = true -- blocker for double touching gems | |
| 34 | ||
| 35 | local gameOverLayout | |
| 36 | local gameOverText1 | |
| 37 | local gameOverText2 | |
| 38 | ||
| 39 | local timers = {}
| |
| 40 | ||
| 41 | -- pre-declaration of function | |
| 42 | local onGemTouch | |
| 43 | ||
| 44 | local kinds = {"fish", "star", "turtle", "shell", }
| |
| 45 | ||
| 46 | local function newGem (i,j) | |
| 47 | ||
| 48 | local newGem | |
| 49 | local img = img | |
| 50 | - | local R = mRandom(1,4) |
| 50 | + | |
| 51 | local R = kinds[mRandom(1,4)] | |
| 52 | - | if (R == 1 ) then |
| 52 | + | |
| 53 | - | img = "fish.png" |
| 53 | + | newGem = display.newImageRect(R..".png", 40, 40) |
| 54 | - | newGem.gemType = "fish" |
| 54 | + | |
| 55 | - | elseif (R == 2 ) then |
| 55 | + | |
| 56 | - | img = "star.png" |
| 56 | + | |
| 57 | - | newGem.gemType = "star" |
| 57 | + | |
| 58 | newGem.isMarkedToDestroy = false | |
| 59 | - | elseif (R == 3 ) then |
| 59 | + | newGem.gemType = R |
| 60 | - | img = "turtle.png" |
| 60 | + | |
| 61 | - | newGem.gemType = "turtle" |
| 61 | + | |
| 62 | ||
| 63 | - | elseif (R == 4 ) then |
| 63 | + | |
| 64 | - | img = "shell.png" |
| 64 | + | |
| 65 | - | newGem.gemType = "shell" |
| 65 | + | |
| 66 | ||
| 67 | groupGameLayer:insert( newGem ) | |
| 68 | ||
| 69 | - | newGem = display.newImageRect(img, 40, 40) |
| 69 | + | |
| 70 | newGem.touch = onGemTouch | |
| 71 | newGem:addEventListener( "touch", newGem ) | |
| 72 | ||
| 73 | return newGem | |
| 74 | end | |
| 75 | ||
| 76 | ||
| 77 | ||
| 78 | local function shiftGems () -- not working yet <-- you stupid.. oh u <_< of coz it's working! :P | |
| 79 | ||
| 80 | print ("Shifting Gems")
| |
| 81 | ||
| 82 | -- first roww | |
| 83 | for i = 1, 8, 1 do | |
| 84 | if gemsTable[i][1].isMarkedToDestroy then | |
| 85 | ||
| 86 | -- current gem must go to a 'gemToBeDestroyed' variable holder to prevent memory leaks | |
| 87 | -- cannot destroy it now as gemsTable will be sorted and elements moved down | |
| 88 | gemToBeDestroyed = gemsTable[i][1] | |
| 89 | ||
| 90 | -- create a new one | |
| 91 | gemsTable[i][1] = newGem(i,1) | |
| 92 | ||
| 93 | -- destroy old gem | |
| 94 | gemToBeDestroyed:removeSelf() | |
| 95 | gemToBeDestroyed = nil | |
| 96 | end | |
| 97 | end | |
| 98 | ||
| 99 | -- rest of the rows | |
| 100 | for j = 2, 8, 1 do -- j = row number - need to do like this it needs to be checked row by row | |
| 101 | for i = 1, 8, 1 do | |
| 102 | ||
| 103 | if gemsTable[i][j].isMarkedToDestroy then --if you find and empty hole then shift down all gems in column | |
| 104 | ||
| 105 | gemToBeDestroyed = gemsTable[i][j] | |
| 106 | ||
| 107 | -- shiftin whole column down, element by element in one column | |
| 108 | for k = j, 2, -1 do -- starting from bottom - finishing at the second row | |
| 109 | ||
| 110 | -- curent markedToDestroy Gem is replaced by the one above in the gemsTable | |
| 111 | gemsTable[i][k] = gemsTable[i][k-1] | |
| 112 | gemsTable[i][k].destination_y = gemsTable[i][k].destination_y +40 | |
| 113 | transition.to( gemsTable[i][k], { time=100, y= gemsTable[i][k].destination_y} )
| |
| 114 | ||
| 115 | -- we change its j value as it has been 'moved down' in the gemsTable | |
| 116 | gemsTable[i][k].j = gemsTable[i][k].j + 1 | |
| 117 | ||
| 118 | end | |
| 119 | ||
| 120 | -- create a new gem at the first row as there is en ampty space due to gems | |
| 121 | -- that have been moved in the column | |
| 122 | gemsTable[i][1] = newGem(i,1) | |
| 123 | ||
| 124 | -- destroy the old gem (the one that was invisible and placed in gemToBeDestroyed holder) | |
| 125 | gemToBeDestroyed:removeSelf() | |
| 126 | gemToBeDestroyed = nil | |
| 127 | end | |
| 128 | end | |
| 129 | end | |
| 130 | end --shiftGems() | |
| 131 | ||
| 132 | ||
| 133 | ||
| 134 | local function markToDestroy( self ) | |
| 135 | ||
| 136 | self.isMarkedToDestroy = true | |
| 137 | numberOfMarkedToDestroy = numberOfMarkedToDestroy + 1 | |
| 138 | ||
| 139 | -- check on the left | |
| 140 | if self.i>1 then | |
| 141 | if (gemsTable[self.i-1][self.j]).isMarkedToDestroy == false then | |
| 142 | ||
| 143 | if (gemsTable[self.i-1][self.j]).gemType == self.gemType then | |
| 144 | ||
| 145 | markToDestroy( gemsTable[self.i-1][self.j] ) | |
| 146 | end | |
| 147 | end | |
| 148 | end | |
| 149 | ||
| 150 | -- check on the right | |
| 151 | if self.i<8 then | |
| 152 | if (gemsTable[self.i+1][self.j]).isMarkedToDestroy == false then | |
| 153 | ||
| 154 | if (gemsTable[self.i+1][self.j]).gemType == self.gemType then | |
| 155 | ||
| 156 | markToDestroy( gemsTable[self.i+1][self.j] ) | |
| 157 | end | |
| 158 | end | |
| 159 | end | |
| 160 | ||
| 161 | -- check above | |
| 162 | if self.j>1 then | |
| 163 | if (gemsTable[self.i][self.j-1]).isMarkedToDestroy == false then | |
| 164 | ||
| 165 | if (gemsTable[self.i][self.j-1]).gemType == self.gemType then | |
| 166 | ||
| 167 | markToDestroy( gemsTable[self.i][self.j-1] ) | |
| 168 | end | |
| 169 | end | |
| 170 | end | |
| 171 | ||
| 172 | -- check below | |
| 173 | if self.j<8 then | |
| 174 | if (gemsTable[self.i][self.j+1]).isMarkedToDestroy== false then | |
| 175 | ||
| 176 | if (gemsTable[self.i][self.j+1]).gemType == self.gemType then | |
| 177 | ||
| 178 | markToDestroy( gemsTable[self.i][self.j+1] ) | |
| 179 | end | |
| 180 | end | |
| 181 | end | |
| 182 | end | |
| 183 | ||
| 184 | ||
| 185 | ||
| 186 | local function enableGemTouch() | |
| 187 | ||
| 188 | isGemTouchEnabled = true | |
| 189 | end | |
| 190 | ||
| 191 | ||
| 192 | ||
| 193 | local function destroyGems() | |
| 194 | print ("Destroying Gems. Marked to Destroy = "..numberOfMarkedToDestroy)
| |
| 195 | ||
| 196 | ||
| 197 | for i = 1, 8, 1 do | |
| 198 | for j = 1, 8, 1 do | |
| 199 | ||
| 200 | if gemsTable[i][j].isMarkedToDestroy then | |
| 201 | ||
| 202 | isGemTouchEnabled = false | |
| 203 | transition.to( gemsTable[i][j], { time=300, alpha=0.2, xScale=2, yScale = 2, onComplete=enableGemTouch } )
| |
| 204 | ||
| 205 | -- update score | |
| 206 | score = score + 10 | |
| 207 | scoreText.text = string.format( "SCORE: %6.0f", score ) | |
| 208 | scoreText:setReferencePoint(display.TopLeftReferencePoint) | |
| 209 | scoreText.x = 60 | |
| 210 | ||
| 211 | end | |
| 212 | end | |
| 213 | end | |
| 214 | ||
| 215 | numberOfMarkedToDestroy = 0 | |
| 216 | timer.performWithDelay( 320, shiftGems ) | |
| 217 | ||
| 218 | end | |
| 219 | ||
| 220 | ||
| 221 | ||
| 222 | local function cleanUpGems() | |
| 223 | print("Cleaning Up Gems")
| |
| 224 | ||
| 225 | numberOfMarkedToDestroy = 0 | |
| 226 | ||
| 227 | for i = 1, 8, 1 do | |
| 228 | for j = 1, 8, 1 do | |
| 229 | ||
| 230 | -- show that there is not enough | |
| 231 | if gemsTable[i][j].isMarkedToDestroy then | |
| 232 | transition.to( gemsTable[i][j], { time=100, xScale=1.2, yScale = 1.2 } )
| |
| 233 | transition.to( gemsTable[i][j], { time=100, delay=100, xScale=1.0, yScale = 1.0} )
| |
| 234 | end | |
| 235 | ||
| 236 | gemsTable[i][j].isMarkedToDestroy = false | |
| 237 | ||
| 238 | ||
| 239 | end | |
| 240 | end | |
| 241 | end | |
| 242 | ||
| 243 | ||
| 244 | ||
| 245 | function onGemTouch( self, event ) -- was pre-declared | |
| 246 | ||
| 247 | if event.phase == "began" and isGemTouchEnabled then | |
| 248 | ||
| 249 | print("Gem touched i= "..self.i.." j= "..self.j)
| |
| 250 | ||
| 251 | markToDestroy(self) | |
| 252 | ||
| 253 | if numberOfMarkedToDestroy >= 3 then | |
| 254 | ||
| 255 | destroyGems() | |
| 256 | else | |
| 257 | cleanUpGems() | |
| 258 | end | |
| 259 | end | |
| 260 | ||
| 261 | return true | |
| 262 | ||
| 263 | end | |
| 264 | ||
| 265 | ||
| 266 | ||
| 267 | function onTouchGameOverScreen ( self, event ) | |
| 268 | ||
| 269 | if event.phase == "began" then | |
| 270 | ||
| 271 | storyboard.gotoScene( "menu-scene", "fade", 400 ) | |
| 272 | ||
| 273 | return true | |
| 274 | end | |
| 275 | end | |
| 276 | ||
| 277 | ||
| 278 | local function showGameOver () | |
| 279 | ||
| 280 | gameOverLayout.alpha = 0.8 | |
| 281 | gameOverText1.alpha = 1 | |
| 282 | gameOverText2.alpha = 1 | |
| 283 | ||
| 284 | ||
| 285 | end | |
| 286 | ||
| 287 | local function gameTimerUpdate () | |
| 288 | ||
| 289 | gameTimer = gameTimer - 1 | |
| 290 | ||
| 291 | if gameTimer >= 0 then gameTimerText.text = gameTimer | |
| 292 | ||
| 293 | else | |
| 294 | gameOverText2.text = string.format( "SCORE: %6.0f", score ) | |
| 295 | showGameOver() | |
| 296 | ||
| 297 | end | |
| 298 | end | |
| 299 | ||
| 300 | ||
| 301 | -- Called when the scene's view does not exist: | |
| 302 | function scene:createScene( event ) | |
| 303 | local screenGroup = self.view | |
| 304 | ||
| 305 | print( "\n1: createScene event") | |
| 306 | ||
| 307 | ----------------------------------------------------------------------------- | |
| 308 | ||
| 309 | -- CREATE display objects and add them to 'group' here. | |
| 310 | -- Example use-case: Restore 'group' from previously saved state. | |
| 311 | ||
| 312 | ----------------------------------------------------------------------------- | |
| 313 | ||
| 314 | -- reseting values | |
| 315 | score = 0 | |
| 316 | gameTimer = 30 | |
| 317 | ||
| 318 | ||
| 319 | groupGameLayer = display.newGroup() | |
| 320 | groupEndGameLayer = display.newGroup() | |
| 321 | ||
| 322 | local bgGameImage = display.newImageRect( "bg.png", 320, 480, true ) | |
| 323 | bgGameImage.x = _W * 0.5 | |
| 324 | bgGameImage.y = _H * 0.5 | |
| 325 | groupGameLayer:insert(bgGameImage) | |
| 326 | --score text | |
| 327 | ||
| 328 | scoreText = display.newText( "SCORE:" , 40, 20, native.systemFontBold, 32 ) | |
| 329 | scoreText.text = string.format( "SCORE: %6.0f", score ) | |
| 330 | scoreText:setReferencePoint(display.TopLeftReferencePoint) | |
| 331 | scoreText.x = 60 | |
| 332 | scoreText:setTextColor(0, 0, 255) | |
| 333 | ||
| 334 | ||
| 335 | gameTimerBar_bg = display.newRoundedRect( 20, 430, 280, 20, 4) | |
| 336 | gameTimerBar_bg:setFillColor( 60, 60, 60 ) | |
| 337 | gameTimerBar = display.newRoundedRect( 20, 430, 280, 20, 4) | |
| 338 | gameTimerBar:setFillColor( 0, 150, 0 ) | |
| 339 | gameTimerBar:setReferencePoint(display.TopLeftReferencePoint) | |
| 340 | ||
| 341 | ||
| 342 | gameTimerText = display.newText( gameTimer , 0, 0, native.systemFontBold, 18 ) | |
| 343 | gameTimerText:setTextColor( 255, 255, 255 ) | |
| 344 | gameTimerText:setReferencePoint(display.TopLeftReferencePoint) | |
| 345 | gameTimerText.x = _W * 0.5 - 12 | |
| 346 | gameTimerText.y = 426 | |
| 347 | ||
| 348 | groupGameLayer:insert ( scoreText ) | |
| 349 | groupGameLayer:insert ( gameTimerBar_bg ) | |
| 350 | groupGameLayer:insert ( gameTimerBar ) | |
| 351 | groupGameLayer:insert ( gameTimerText ) | |
| 352 | ||
| 353 | --gemsTable | |
| 354 | for i = 1, 8, 1 do | |
| 355 | ||
| 356 | gemsTable[i] = {}
| |
| 357 | ||
| 358 | for j = 1, 8, 1 do | |
| 359 | ||
| 360 | gemsTable[i][j] = newGem(i,j) | |
| 361 | ||
| 362 | end | |
| 363 | end | |
| 364 | ||
| 365 | ||
| 366 | gameOverLayout = display.newRect( 0, 0, 320, 480) | |
| 367 | gameOverLayout:setFillColor( 0, 0, 0 ) | |
| 368 | gameOverLayout.alpha = 0 | |
| 369 | ||
| 370 | gameOverText1 = display.newText( "GAME OVER", 0, 0, native.systemFontBold, 24 ) | |
| 371 | gameOverText1:setTextColor( 255 ) | |
| 372 | gameOverText1:setReferencePoint( display.CenterReferencePoint ) | |
| 373 | gameOverText1.x, gameOverText1.y = _W * 0.5, _H * 0.5 -150 | |
| 374 | gameOverText1.alpha = 0 | |
| 375 | ||
| 376 | gameOverText2 = display.newText( "SCORE: ", 0, 0, native.systemFontBold, 24 ) | |
| 377 | gameOverText2.text = string.format( "SCORE: %6.0f", score ) | |
| 378 | gameOverText2:setTextColor( 255 ) | |
| 379 | gameOverText2:setReferencePoint( display.CenterReferencePoint ) | |
| 380 | gameOverText2.x, gameOverText2.y = _W * 0.5, _H * 0.5 - 50 | |
| 381 | gameOverText2.alpha = 0 | |
| 382 | ||
| 383 | ||
| 384 | gameOverLayout.touch = onTouchGameOverScreen | |
| 385 | gameOverLayout:addEventListener( "touch", gameOverLayout ) | |
| 386 | ||
| 387 | ||
| 388 | groupEndGameLayer:insert ( gameOverLayout ) | |
| 389 | groupEndGameLayer:insert ( gameOverText1 ) | |
| 390 | groupEndGameLayer:insert ( gameOverText2 ) | |
| 391 | ||
| 392 | ||
| 393 | -- insterting display groups to the screen group (storyboard group) | |
| 394 | screenGroup:insert ( groupGameLayer ) | |
| 395 | screenGroup:insert ( groupEndGameLayer ) | |
| 396 | end | |
| 397 | ||
| 398 | -- Called BEFORE scene has moved onscreen: | |
| 399 | function scene:willEnterScene( event ) | |
| 400 | local screenGroup = self.view | |
| 401 | ||
| 402 | ----------------------------------------------------------------------------- | |
| 403 | ||
| 404 | -- This event requires build 2012.782 or later. | |
| 405 | ||
| 406 | ----------------------------------------------------------------------------- | |
| 407 | ||
| 408 | end | |
| 409 | ||
| 410 | -- Called immediately after scene has moved onscreen: | |
| 411 | function scene:enterScene( event ) | |
| 412 | local screenGroup = self.view | |
| 413 | ||
| 414 | ----------------------------------------------------------------------------- | |
| 415 | ||
| 416 | -- INSERT code here (e.g. start timers, load audio, start listeners, etc.) | |
| 417 | ||
| 418 | ----------------------------------------------------------------------------- | |
| 419 | ||
| 420 | -- remove previous scene's view | |
| 421 | ||
| 422 | storyboard.purgeScene( "menu-scene" ) | |
| 423 | ||
| 424 | print( "1: enterScene event" ) | |
| 425 | ||
| 426 | -- storyboard.purgeScene( "scene4" ) | |
| 427 | ||
| 428 | -- reseting values | |
| 429 | score = 0 | |
| 430 | ||
| 431 | ||
| 432 | transition.to( gameTimerBar, { time = gameTimer * 1000, width=0 } )
| |
| 433 | timers.gameTimerUpdate = timer.performWithDelay(1000, gameTimerUpdate, 0) | |
| 434 | ||
| 435 | ||
| 436 | ||
| 437 | end | |
| 438 | ||
| 439 | ||
| 440 | -- Called when scene is about to move offscreen: | |
| 441 | function scene:exitScene( event ) | |
| 442 | local screenGroup = self.view | |
| 443 | ||
| 444 | ----------------------------------------------------------------------------- | |
| 445 | ||
| 446 | -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) | |
| 447 | ||
| 448 | ----------------------------------------------------------------------------- | |
| 449 | ||
| 450 | if timers.gameTimerUpdate then | |
| 451 | timer.cancel(timers.gameTimerUpdate) | |
| 452 | timers.gameTimerUpdate = nil | |
| 453 | end | |
| 454 | ||
| 455 | end | |
| 456 | ||
| 457 | -- Called AFTER scene has finished moving offscreen: | |
| 458 | function scene:didExitScene( event ) | |
| 459 | local screenGroup = self.view | |
| 460 | ||
| 461 | ----------------------------------------------------------------------------- | |
| 462 | ||
| 463 | -- This event requires build 2012.782 or later. | |
| 464 | ||
| 465 | ----------------------------------------------------------------------------- | |
| 466 | ||
| 467 | end | |
| 468 | ||
| 469 | ||
| 470 | -- Called prior to the removal of scene's "view" (display group) | |
| 471 | function scene:destroyScene( event ) | |
| 472 | local screenGroup = self.view | |
| 473 | ||
| 474 | ----------------------------------------------------------------------------- | |
| 475 | ||
| 476 | -- INSERT code here (e.g. remove listeners, widgets, save state, etc.) | |
| 477 | ||
| 478 | ----------------------------------------------------------------------------- | |
| 479 | ||
| 480 | end | |
| 481 | ||
| 482 | -- Called if/when overlay scene is displayed via storyboard.showOverlay() | |
| 483 | function scene:overlayBegan( event ) | |
| 484 | local screenGroup = self.view | |
| 485 | local overlay_scene = event.sceneName -- overlay scene name | |
| 486 | ||
| 487 | ----------------------------------------------------------------------------- | |
| 488 | ||
| 489 | -- This event requires build 2012.797 or later. | |
| 490 | ||
| 491 | ----------------------------------------------------------------------------- | |
| 492 | ||
| 493 | end | |
| 494 | ||
| 495 | -- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay() | |
| 496 | function scene:overlayEnded( event ) | |
| 497 | local screenGroup = self.view | |
| 498 | local overlay_scene = event.sceneName -- overlay scene name | |
| 499 | ||
| 500 | ----------------------------------------------------------------------------- | |
| 501 | ||
| 502 | -- This event requires build 2012.797 or later. | |
| 503 | ||
| 504 | ----------------------------------------------------------------------------- | |
| 505 | ||
| 506 | end | |
| 507 | ||
| 508 | ||
| 509 | ||
| 510 | --------------------------------------------------------------------------------- | |
| 511 | -- END OF YOUR IMPLEMENTATION | |
| 512 | --------------------------------------------------------------------------------- | |
| 513 | ||
| 514 | -- "createScene" event is dispatched if scene's view does not exist | |
| 515 | scene:addEventListener( "createScene", scene ) | |
| 516 | ||
| 517 | -- "willEnterScene" event is dispatched before scene transition begins | |
| 518 | scene:addEventListener( "willEnterScene", scene ) | |
| 519 | ||
| 520 | -- "enterScene" event is dispatched whenever scene transition has finished | |
| 521 | scene:addEventListener( "enterScene", scene ) | |
| 522 | ||
| 523 | -- "exitScene" event is dispatched before next scene's transition begins | |
| 524 | scene:addEventListener( "exitScene", scene ) | |
| 525 | ||
| 526 | -- "didExitScene" event is dispatched after scene has finished transitioning out | |
| 527 | scene:addEventListener( "didExitScene", scene ) | |
| 528 | ||
| 529 | -- "destroyScene" event is dispatched before view is unloaded, which can be | |
| 530 | -- automatically unloaded in low memory situations, or explicitly via a call to | |
| 531 | -- storyboard.purgeScene() or storyboard.removeScene(). | |
| 532 | scene:addEventListener( "destroyScene", scene ) | |
| 533 | ||
| 534 | -- "overlayBegan" event is dispatched when an overlay scene is shown | |
| 535 | scene:addEventListener( "overlayBegan", scene ) | |
| 536 | ||
| 537 | -- "overlayEnded" event is dispatched when an overlay scene is hidden/removed | |
| 538 | scene:addEventListener( "overlayEnded", scene ) | |
| 539 | ||
| 540 | --------------------------------------------------------------------------------- | |
| 541 | ||
| 542 | return scene |