Advertisement
alestane

Consolidated functions

Jan 14th, 2012
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | None | 0 0
  1. display.setStatusBar(display.HiddenStatusBar)
  2. local storyboard = require "storyboard"
  3. local scene = storyboard.newScene()
  4. require "sprite"
  5.  
  6. ----------------------------
  7. -- Functions
  8. ----------------------------
  9.  
  10. local function buttonTouch(event)
  11.     local button = event.target
  12.     local bounds = button.contentBounds
  13.     if event.phase == "began" then
  14.         button.currentFrame = 2
  15.         display.getCurrentStage():setFocus(button, event.id)
  16.         button.isFocused = true
  17.     elseif button.isFocused then
  18.         local isWithinBounds = bounds.xMin <= event.x and bounds.xMax >= event.x and bounds.yMin <= event.y and bounds.yMax >= event.y
  19.         if event.phase == "moved" then
  20.             if isWithinBounds then
  21.                 button.currentFrame = 2
  22.             else
  23.                 button.currentFrame = 1
  24.             end
  25.         elseif event.phase == "ended" or event.phase == 'cancelled' then
  26.             if event.phase == "ended" and isWithinBounds then
  27.                 if options.audio then
  28.                     audio.play(audioChange)
  29.                 end
  30.                 button.currentFrame = 1
  31.                 storyboard.gotoScene(button.targetScene, "slideLeft", 500)
  32.             end
  33.             button.currentFrame = 1
  34.             display.getCurrentStage():setFocus(button, nil)
  35.             button.isFocused = false
  36.         end
  37.     end
  38. end
  39.  
  40. ----------------------------
  41. -- Storyboard
  42. ----------------------------
  43.  
  44. function scene:createScene(event)
  45.     local group = self.view
  46.     if options.audio then
  47.         audioBackground = audio.loadStream("audio/menu_background" .. storyboard.audioType)
  48.             audio.play(audioBackground, {fadein = 1000, loops = -1})
  49.         audioChange = audio.loadSound("audio/menu_change" .. storyboard.audioType)
  50.     end
  51.     local background = display.newImage(group, "visuals/menu_background.png", 0, 0)
  52.     local buttonSheet = sprite.newSpriteSheet("visuals/menu_buttons.png", 405, 100)
  53.     local button = {}
  54.     button.Play = sprite.newSprite(sprite.newSpriteSet(buttonSheet, 1, 2))
  55.         group:insert(button.Play)
  56.         button.Play.targetScene = "maps.level1Scene"
  57.         button.Play:addEventListener("touch", buttonTouch)
  58.         button.Play:setReferencePoint(display.TopCenterReferencePoint)
  59.         button.Play.x = display.contentWidth / 2
  60.         button.Play.y = 340
  61.     buttonOptions = sprite.newSprite(sprite.newSpriteSet(buttonSheet, 3, 2))
  62.         group:insert(button.Options)
  63.         button.Options.targetScene ="optionsScene"
  64.         button.Options:addEventListener("touch", buttonTouch)
  65.         button.Options:setReferencePoint(display.TopLeftReferencePoint)
  66.         button.Options.x = 50
  67.         button.Options.y = 490
  68.     buttonCredits = sprite.newSprite(sprite.newSpriteSet(buttonSheet, 5, 2))
  69.         group:insert(button.Credits)
  70.         buttonCredits.targetScene = "creditsScene"
  71.         buttonCredits:addEventListener("touch", buttonTouch)
  72.         buttonCredits:setReferencePoint(display.TopLeftReferencePoint)
  73.         buttonCredits.x = 505
  74.         buttonCredits.y = 490
  75. end
  76.  
  77. function scene:exitScene(event)
  78.     if options.audio then
  79.         audio.stop()
  80.         audio.dispose(audioBackground)
  81.         audio.dispose(audioChange)
  82.     end
  83.     storyboard.purgeScene("menuScene")
  84. end
  85.  
  86. scene:addEventListener("createScene", scene)
  87. scene:addEventListener("exitScene", scene)
  88. return scene
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement