2013-02-05 44 views
0

我一直在修改內存匹配遊戲中的代碼以使用場景,聲音和2D表。添加場景一直是困難的部分。我已經設置從我的2D表中隨機選擇項目。將它們加載到一個臨時表中,隨機洗牌(),然後boardSet();加載他們的聲音並將它們放置在屏幕上。基本上在比賽結束後,我希望場景重新加載或返回菜單重新開始。反覆選擇隨機數據{}元素。更改電暈物體的場景留在屏幕上

我已經嘗試返回到菜單或去重複的場景,但我似乎無法正確卸載由我的二維數組創建的對象,因爲我無法正確地將每個項目添加到顯示組中。我嘗試了所有我能想到的。現在我的遊戲循環被設置爲再次添加遊戲btn,我想在找到一場比賽後重新開始遊戲。

這個鏈接是一個正確的方向的開始,我相信與處理表和組的信息。我仍然不知道如何通過我的2D表循環來將所有內容包含在一個組中,然後正確卸載它。

http://developer.coronalabs.com/content/application-programming-guide-graphics-and-drawing#Variable_References

--------------------------------------------------------------------------------- 
-- 
-- level1.lua 
-- 
--------------------------------------------------------------------------------- 

local storyboard = require("storyboard") 
local scene = storyboard.newScene() 
local widget = require "widget" 

--------------------------------------------------------------------------------- 
-- BEGINNING OF YOUR IMPLEMENTATION 
--------------------------------------------------------------------------------- 

local image, text1, text2, text3, memTimer 
-- forward declarations and other locals 
local againBtn 

-- 'onRelease' event listener for playBtn 
local function onPlayBtnRelease() 

    -- go to level1.lua scene 

    storyboard.gotoScene("menu", "fade", 500) 

    return true -- indicates successful touch 
end 

-- Preload the sound file (needed for Android) 
-- 
local playBeep = function(testSound) 
      media.playEventSound(testSound) 
end 
---Preload sounds 

ki = audio.loadSound("ki-nawaneyoo.mp3") 
u = audio.loadSound("u.mp3") 
mayuhoo = audio.loadSound("mayuhoo.mp3") 

--End Sound Test 

--Set Global width and height variables 
_W = display.contentWidth; 
_H = display.contentHeight; 

---Count number of correct matches 
local matchCount = 0; 

--Hide status bar 
display.setStatusBar(display.HiddenStatusBar); 

--Declare a totalButtons variable to track number of buttons on screen 
local totalButtons = 0 

--Declare variable to track button select 
local secondSelect = 0 
local checkForMatch = false 

--Declare Data Table 
local data = {} 
--Load objects into data 
data[1] = {} 
data[1].title = "Naka" 
data[1].subtitle = "a" 
data[1].image = "a.png" 
data[1].image1 = "ear.png" 
data[1].sound = "naka-ear.caf" 
data[1].sound1 = "naka-ear.mp3" 

data[2] = {} 
data[2].title = "Aapuhu" 
data[2].subtitle = "aa" 
data[2].image = "aa.png" 
data[2].image1 = "eyebrow.png" 
data[2].sound = "aapuhu-eyebrow.caf" 
data[2].sound1 = "aapuhu-eyebrow.mp3" 

data[3] = {} 
data[3].title = "Ego" 
data[3].subtitle = "Ego" 
data[3].image = "e.png" 
data[3].image1 = "tongue.png" 
data[3].sound = "ego-tongue.caf" 
data[3].sound1 = "ego-tongue.mp3" 

data[4] = {} 
data[4].title = "Kamoo" 
data[4].subtitle = "kamoo" 
data[4].image = "ee.png" 
data[4].image1 = "chin.png" 
data[4].sound = "kamoo-chin.caf" 
data[4].sound1 = "kamoo-chin.mp3" 

data[5] = {} 
data[5].title = "Kowpa" 
data[5].subtitle = "ego" 
data[5].image = "leg.png" 
data[5].image1 = "leg.png" 
data[5].sound = "kowpa-leg.caf" 
data[5].sound1 = "kowpa-leg.mp3" 

data[6] = {} 
data[6].title = "Mae" 
data[6].subtitle = "kwa" 
data[6].image = "hand.png" 
data[6].image1 = "hand.png" 
data[6].sound = "mae-hand.caf" 
data[6].sound1 = "mae-hand.mp3" 

data[7] = {} 
data[7].title = "Nodo" 
data[7].subtitle = "kwe" 
data[7].image = "kwe.png" 
data[7].image1 = "throat.png" 
data[7].sound = "nodo-throat.caf" 
data[7].sound1 = "nodo-throat.mp3" 

data[8] = {} 
data[8].title = "Matogo" 
data[8].subtitle = "kwe" 
data[8].image = "kwe.png" 
data[8].image1 = "thumb.png" 
data[8].sound = "matogo-thumb.caf" 
data[8].sound1 = "matogo-thumb.mp3" 

data[9] = {} 
data[9].title = "Matzehe" 
data[9].subtitle = "kwe" 
data[9].image = "kwe.png" 
data[9].image1 = "elbow.png" 
data[9].sound = "matzehe-eblow.caf" 
data[9].sound1 = "matzehe-elbow.mp3" 

data[10] = {} 
data[10].title = "Kuku" 
data[10].subtitle = "kwe" 
data[10].image = "kwe.png" 
data[10].image1 = "foot.png" 
data[10].sound = "kuku-foot.caf" 
data[10].sound1 = "kuku-foot.mp3" 

--Declare button, buttonCover, and buttonImages table 
local tableCopy = {} 
local buttonImages = {} 


--Shuffle data table 
--local shuffleSet = function() 
--Shuffle data table 
math.randomseed (os.time()) 
local function shuffle(a) 
     local n = #a 
     local t 
     local k 
     while(n > 0) do 
       t = a[n] 
       k = math.random(n) 
       a[n] = a[k] 
       a[k] = t 
       n = n - 1 
     end 
     return a 
end 

local tableCopy = shuffle(data); 

local button = {} 
local buttonCover = {} 

--Choose six random objects from data to be used in game. 
local firstSix = {} 
for i = 1,6 do 
    firstSix[i] = tableCopy[i]; 
end 
local buttonImages = {firstSix[1],firstSix[1],firstSix[2],firstSix[2],firstSix[3],firstSix[3],firstSix[4],firstSix[4],firstSix[5],firstSix[5],firstSix[6],firstSix[6]} 
--end 


--Declare and prime a last button selected variable 
local lastButton;-- = display.newImage("1.png");  
--lastButton.myName = 1; 
-- 

--Set up simple off-white background 


--Notify player if match is found or not 
local matchText = display.newText(" ", 0, 0, native.systemFont, 65) 
matchText:setReferencePoint(display.CenterReferencePoint) 
matchText:setTextColor(255, 255, 255) 
matchText.x = _W/2 


-- 
--Set starting point for button grid 
local x = -20 

local matchesFound = 0; 

--Set up game function 
local function game(object, event) 
    if(event.phase == "began") then    
     if(checkForMatch == false and secondSelect == 0) then 
      --Flip over first button 
      buttonCover[object.number].isVisible = false; 
      audio.play(object.sound); 
      lastButton = object 
      checkForMatch = true    
     elseif(checkForMatch == true and object.number ~= lastButton.number) then 
      if(secondSelect == 0) then 
       --Flip over second button 
       buttonCover[object.number].isVisible = false; 
       secondSelect = 1; 
       --If buttons do not match, flip buttons over 
       if(lastButton.myName ~= object.myName) then 
        audio.play(object.sound); 
        timer.performWithDelay(1000,function() 
        matchText.text = "Ki Nawa'neyoo"; 
        audio.play(ki); end,1) 

        timer.performWithDelay(2500, function()      
         matchText.text = " "; 
         checkForMatch = false; 
         secondSelect = 0; 
         buttonCover[lastButton.number].isVisible = true; 
         buttonCover[object.number].isVisible = true; 
          end, 1)   

       --If buttons DO match, remove buttons 
       elseif(lastButton.myName == object.myName) then 
        matchText.text = "U " .. object.myName .. " Mayuhoo"; 



          audio.play(u) 

          timer.performWithDelay(750,function() 
          audio.play(object.sound); end, 1) 

          timer.performWithDelay(1500,function() 
          audio.play(mayuhoo); end, 1) 

          timer.performWithDelay(2400, function() 

          matchText.text = " "; 
          checkForMatch = false; 
          secondSelect = 0; 
          lastButton:removeSelf(); 
          object:removeSelf(); 
          buttonCover[lastButton.number]:removeSelf(); 
          buttonCover[object.number]:removeSelf(); 
          matchesFound = matchesFound + 1; 
          timer.performWithDelay(250, function() 
           if (matchesFound == 1) then 
          matchText.text = " Play Again? "; 
          againBtn.isVisible = true; 

          -- all display objects must be inserted into group 
          --group:insert(playBtn) 
          end 
          end, 1) 
          end, 1) 


       end    
      end   
     end 
    end 

end 
--]] 
-- Touch event listener for background image 
--[[ 
local function onSceneTouch(self, event) 
    if event.phase == "began" then 

     storyboard.gotoScene("scene2", "slideLeft", 800 ) 

     return true 
    end 
end 
--]] 



-- Called when the scene's view does not exist: 
function scene:createScene(event) 
    local screenGroup = self.view 

    local image = display.newImageRect("bg.png", display.contentWidth, display.contentHeight) 
    image:setReferencePoint(display.TopLeftReferencePoint) 
    image.x, image.y = 0, 0 

    screenGroup:insert(image) 
    local function boardSet() 
for count = 1,3 do 
    x = x + 100 * 2 
    y = 20 *2 

    for insideCount = 1,4 do 
     y = y + 90 * 2 

     --Assign each image a random location on grid 
     local temp = math.random(1,#buttonImages) 
     button[count] = display.newImageRect(buttonImages[temp].image1, 150, 150);    

     --Position the button 
     button[count].x = x; 
     button[count].y = y;   

     --Give each a button a name 
     button[count].myName = buttonImages[temp].title 
     -- Preload the sound file (needed for Android) 

     --Give each button a sounds 

     soundID = audio.loadSound(buttonImages[temp].sound1) 

     button[count].sound = soundID 
     button[count].sound1 = soundID 
     button[count].number = totalButtons 

     --Remove button from buttonImages table 
     table.remove(buttonImages, temp) 

     --Set a cover to hide the button image 
     buttonCover[totalButtons] = display.newImageRect("button.png", 150, 150); 
     buttonCover[totalButtons].x = x; buttonCover[totalButtons].y = y; 
     --screenGroup:insert(button[count].) 
     totalButtons = totalButtons + 1 

     --Attach listener event to each button 
     button[count].touch = game  
     button[count]:addEventListener("touch", button[count]) 

    end 
end 


end 
boardSet(); 
end 




-- Called immediately after scene has moved onscreen: 
function scene:enterScene(event) 
againBtn = widget.newButton{ 
          label="Play Again?", 
          fontSize = 25, 
          labelColor = { default={255}, over={128} }, 
          default="button1.png", 
          over="button-over.png", 
          width=250, height=100, 
          onRelease = onPlayBtnRelease -- event listener function 
          } 
          againBtn:setReferencePoint(display.CenterReferencePoint) 
          againBtn.x = display.contentWidth * 0.5 
          againBtn.y = display.contentHeight - 125 
          againBtn.isVisible = false 


    print("1: enterScene event") 

    -- remove previous scene's view 
end 
-- Called when scene is about to move offscreen: 
function scene:exitScene(event) 

    print("1: exitScene event") 
    if againBtn then 
     matchText.text = " " 
     againBtn:removeSelf() -- widgets must be manually removed 
     button = nil 
     buttonCover = nil 
     firstSix = nil 
     data = nil 
     tableCopy = nil 
     buttonImages = nil 
     againBtn = nil 

    end 


end 


-- Called prior to the removal of scene's "view" (display group) 
function scene:destroyScene(event) 
    local screenGroup = self.view 
    storyboard.removeScene(); 
    --storyboard.purgeScene("level1") 

    print("((destroying scene 1's view))") 
end 

--------------------------------------------------------------------------------- 
-- END OF YOUR IMPLEMENTATION 
--------------------------------------------------------------------------------- 

-- "createScene" event is dispatched if scene's view does not exist 
scene:addEventListener("createScene", scene) 

-- "enterScene" event is dispatched whenever scene transition has finished 
scene:addEventListener("enterScene", scene) 

-- "exitScene" event is dispatched before next scene's transition begins 
scene:addEventListener("exitScene", scene) 

-- "destroyScene" event is dispatched before view is unloaded, which can be 
-- automatically unloaded in low memory situations, or explicitly via a call to 
-- storyboard.purgeScene() or storyboard.removeScene(). 
scene:addEventListener("destroyScene", scene) 

--------------------------------------------------------------------------------- 

return scene 
+0

學到了一點關於今天的一些「陷阱」的去除對象的情況下正確'here – doneDone

+0

爲什麼你不能將它們添加到場景的screenGroup中? –

回答

0

當使用電暈故事板需要顯示對象添加到組。例如,您需要將所有顯示對象添加到screenGroup中,以便在更改場景時將其移除。

2

將所有顯示對象添加到組中。 local myGroup = display.newGroup(); local img = display.newImage("yourimage.png"); myGroup:insert(img); 例如

如果您遵循此方法,當您更改屏幕時,所有顯示對象將不會刷新內存。

2

是的,這很簡單。

這裏有一個例子(假設你已經實現了分鏡)

function scene:createScene(event) 
    screenGroup = self.view 
    local image = display.newImage("image.png") 
    screenGroup:insert(image) 
end 

現在,一切都會罰款:)