2013-02-05 1 views
0

나는 장면, 소리 및 2D 테이블을 사용하기 위해 메모리 매치 게임의 코드를 수정하고 있습니다. 장면 추가는 어려운 부분이었습니다. 내 2d 테이블에서 임의로 항목을 선택하도록 설정했습니다. 임시 테이블에로드 한 다음 shuffle(), boardSet(); 소리를 불러 와서 화면에 놓습니다. 기본적으로 게임이 끝나면 장면을 다시로드하거나 메뉴로 다시 돌아가서 다시 시작하기를 원합니다. 임의의 데이터 {} 요소를 반복해서 선택합니다.코로나 객체의 장면을 화면에 그대로 유지

메뉴로 돌아가려고했으나 중복 된 장면으로 이동하려고했지만 디스플레이 그룹에 각 항목을 제대로 추가 할 수 없기 때문에 제 2d 배열에서 생성 한 개체를 올바르게 언로드 할 수 없습니다. 나는 내가 생각할 수있는 모든 것을 시도했다. 지금 당장 게임 루프를 설정하여 게임을 다시 추가합니다. 하나의 게임이 발견 된 후 게임을 다시 시작하고 싶습니다.

이 링크는 올바른 방향으로 시작했습니다. 나는 테이블과 그룹을 다루는 것에 대한 정보를 믿습니다. 나는 여전히 그룹의 모든 것을 포함시키고 적절히 언로드하기 위해 2d 테이블을 순환시키는 법을 모른다.

--------------------------------------------------------------------------------- 
-- 
-- 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

은 '함정'의 일부에 대해 조금 더 오늘 배운 '여기 – doneDone

+0

왜 그들을 scene의 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 

이제 다 잘 갈 것입니다 : 객체의 제대로을 제거 할 때

관련 문제