Advertisement
Hendrix000007

ButtonsBirds class and playGame file

Sep 19th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.91 KB | None | 0 0
  1. --ButtonsBirds.lua
  2. Bird = {}
  3. Bird.pathForBehaviours = "Assets/Birds/Behaviour/"
  4. Bird.group =  display.newGroup( )
  5. Bird.group.x = 20
  6. Bird.group.y = display.contentHeight
  7. --group = group
  8.  
  9. local imgW, imgH = 48, 48
  10. local behaviourCounter = 0
  11. --BIRDS TABLE
  12. Bird.buttons = {}
  13. --BEHAVIOURS TABLE
  14. Bird.buttons.behaviours = {
  15. [1] = {
  16.       name                 =  "behaviour",
  17.       sound                =  "behaviour.mp3" or nil,
  18.       imageObj             =  display.newImageRect( Bird.pathForBehaviours .. "behaviour.png", imgW, imgH ),
  19.       sql                  =  "SELECT * FROM  birds WHERE PATTERN = 'Spettete'",               -- WICH FUNCTION TO LISTEN TO
  20. },
  21. [2] = {
  22.       name                 =  "birdHouse",
  23.       sound                =  "birdHouse.mp3" or nil,
  24.       imageObj             =  display.newImageRect( Bird.pathForBehaviours .. "birdHouse.png", imgW, imgH ),
  25.       sql                  =  "SELECT * FROM  birds WHERE PATTERN = 'Stripete'",               -- WICH FUNCTION TO LISTEN TO
  26. },
  27. [3] = {
  28.       name                 =  "catchingInsectsInFlight",
  29.       sound                =  "catchingInsectsInFlight.mp3" or nil,
  30.       imageObj             =  display.newImageRect( Bird.pathForBehaviours .. "catchingInsectsInFlight.png", imgW, imgH ),
  31.       listnerFunctionName  =  "playButton",               -- WICH FUNCTION TO LISTEN TO
  32. },
  33. [4] = {
  34.       name                 =  "climbingTree",
  35.       sound                =  "climbingTree.mp3" or nil,
  36.       imageObj             =  display.newImageRect( Bird.pathForBehaviours .. "climbingTree.png", imgW, imgH ),
  37.       listnerFunctionName  =  "playButton",               -- WICH FUNCTION TO LISTEN TO
  38. },
  39. }
  40.  
  41. function Bird:test(event)
  42.        if ( event.phase == "began" ) then
  43.         print( "Touch started")
  44.         print(self.id)
  45.     --    sql = "SELECT * FROM  birds WHERE PATTERN = 'self.id'"
  46.  --   sql  =  "SELECT * FROM  birds WHERE PATTERN = 'Stripete'"
  47.     elseif ( event.phase == "ended" ) then
  48.         print( "Touch ended" )
  49.     end
  50.     updateSql()
  51.    return true
  52. end
  53.  
  54.  
  55.  
  56. function Bird:initButtons()
  57.    for k,v in ipairs (Bird.buttons.behaviours) do
  58.       behaviourCounter = behaviourCounter + 1
  59.       local img = v["name"] .. k
  60.       img = v["imageObj"]
  61.       img.x = (imgW + 5) * behaviourCounter --.. behaviourCounter = v["imageObj"]
  62.       img.anchorX = 0.5
  63.       img.anchorY = 0.15
  64.  
  65.       img:addEventListener( "touch", img )
  66.       img.id = v["name"]
  67.       sql = v["sql"]
  68.       img.touch = Bird.test
  69.       Bird.group:insert(img)
  70.       print(v["name"])
  71.    end
  72.    print( "Bird.group has "..Bird.group.numChildren.." children")
  73.    return true
  74. end
  75.  
  76. function Bird:playButton(self) -- FUNCTION FOR THE BUTTONS
  77.       print("My name is " .. self.name)
  78.    return true
  79. end
  80.  
  81. function playButton(self) -- FUNCTION FOR THE BUTTONS
  82.       print("im a button")
  83.    return true
  84. end
  85. function Bird:setupButtons()
  86.    for i = 1, 4 do
  87.       behaviourCounter = behaviourCounter + 1
  88.     --  Bird.buttons.behaviours.imageObj[i].x = imgW * behaviourCounter
  89.     print(Bird.buttons.behaviours[3][2])
  90.    end
  91.    return true
  92. end
  93.  
  94.  
  95. Bird:initButtons()
  96. --Bird:setupButtons()
  97. return Bird
  98. ----------------------
  99. ----------------------
  100. --playGame.lua file
  101.  
  102. display.setStatusBar(display.HiddenStatusBar)
  103.  
  104. require( "sqlite3" )
  105. birds = require("Classes.ButtonsBirds")
  106. --LOCALS FWD
  107. local composer       = require "composer"
  108. local scene            = composer.newScene()
  109. local path             = system.pathForFile( "Birds.sqlite", system.ResourceDirectory )
  110. local imagePath        = "Images/Birds/"
  111. local text, t, counter, bgItem, bg, colorConverter
  112. local rowBg = {}
  113. local rowName = {}
  114. local rowLatin = {}
  115. local rowImage = {}
  116. local playButton = {}
  117. removeGroup = {}
  118. updateSql = {}
  119.  
  120. local _W, _H         = display.contentWidth, display.contentHeight
  121. local group          = {}
  122. local containerGroup = display.newGroup( )
  123. local dashboardGroup = display.newGroup( )
  124.  
  125. --GLOBALS FWD
  126. _G.msgFont          = "Cheboygan"
  127. _G.db               = sqlite3.open( path )
  128.  
  129. -- FUNCTIONS
  130. local function onSystemEvent( event )
  131.     if ( event.type == "applicationExit" ) then
  132.         db:close()
  133.     end
  134. end
  135.  
  136. function colorConverter(r, g, b)
  137.    local colorR, colorG, colorB
  138.       colorR = r/256; colorG = g/256; colorB = b/256
  139.    return colorR, colorG, colorB
  140. end
  141.  
  142. function removeGroup()
  143.    for i = 1, #group do
  144.       print(#group)
  145.    end
  146.  
  147.    return true
  148. end
  149.  
  150.  
  151. -- "scene:create()"
  152. function scene:create( event )
  153.    local sceneGroup = self.view
  154.    Bird.group = Bird.group
  155. end
  156.  
  157. -- "scene:show()"
  158. function scene:show( event )
  159.    local sceneGroup = self.view
  160.    local phase = event.phase
  161.    if ( phase == "will" ) then         
  162.    elseif ( phase == "did" ) then
  163.  
  164. --   sql = "SELECT * FROM  birds WHERE PATTERN = 'Spettete'"
  165.    sql = "SELECT * FROM  birds"
  166.  
  167. function updateSql()
  168.    counter = 0
  169.    if #group > 1 then removeGroup() return true end
  170. --  sql = "SELECT * FROM  birds LIMIT 10"
  171.    
  172.    for row in db:nrows( sql ) do
  173.     counter = counter + 1
  174.       group[counter] = display.newGroup()
  175.       group[counter].x = 5
  176.       group[counter].y = 50 * counter
  177.  
  178.       rowBg[counter] = display.newRoundedRect( 0, 0, _W-10, 50, 5 )
  179.       rowBg[counter].anchorX = 0
  180.       rowBg[counter]:setFillColor( colorConverter(2, 121, 22))
  181.  
  182.     rowName[counter] = display.newText( row.NAME, 5, 5, native.systemFontBold, 24 )
  183.     rowName[counter].anchorX = 0; rowName[counter].anchorY = 1
  184.     rowName[counter]:setFillColor( colorConverter(255, 228, 0) )
  185.  
  186.       rowLatin[counter] = display.newText( row.LATIN, 5, rowName[counter].height-18, native.systemFontBold, 14 )
  187.       rowLatin[counter].anchorX = 0; rowName[counter].anchorY = 1
  188.       rowLatin[counter]:setFillColor( 1,1,1 )
  189.  
  190.     rowImage[counter] = display.newImageRect(tostring(imagePath .. row.NAME) .. ".jpg", 45,45 )
  191.       rowImage[counter].anchorX = 1
  192.       rowImage[counter].x = rowBg[counter].width - 10
  193.  
  194.       sceneGroup:insert(group[counter])
  195.       group[counter]:insert(rowBg[counter])
  196.       group[counter]:insert(rowName[counter])
  197.       group[counter]:insert(rowLatin[counter])
  198.       group[counter]:insert(rowImage[counter])
  199.    end
  200.    return true
  201. end
  202.  
  203.  --     Runtime:addEventListener( "enterFrame", updateSql )
  204.     Runtime:addEventListener( "system", onSystemEvent )
  205.    end
  206.       sceneGroup:insert(dashboardGroup)
  207.       dashboardGroup:insert(birds.group)
  208.       print( "dashboardGroup has "..birds.group.numChildren.." children")
  209.  
  210.  
  211. end
  212.  
  213. -- "scene:hide()"
  214. function scene:hide( event )
  215.    local sceneGroup = self.view
  216.    local phase = event.phase
  217.    if ( phase == "will" ) then 
  218.    elseif ( phase == "did" ) then
  219.    end
  220. end
  221.  
  222. function scene:destroy( event )
  223.     local sceneGroup = self.view
  224. --  Runtime:removeEventListener( "system", onSystemEvent )
  225. end
  226.  
  227. -- Listener setup
  228. scene:addEventListener( "create", scene )
  229. scene:addEventListener( "show", scene )
  230. scene:addEventListener( "hide", scene )
  231. scene:addEventListener( "destroy", scene )
  232.  
  233. return scene
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement