更改Corona对象中的场景会保留在屏幕上

时间:2013-02-05 18:01:24

标签: lua corona corona-storyboard

我一直在修改内存匹配游戏中的代码,以使用场景,声音和2d表。添加场景一直是困难的部分。我把它设置为从我的2d表中随机选择项目。将它们加载到临时表中,随机播放shuffle(),然后将boardSet();加载他们的声音并将其放在屏幕上。基本上在游戏结束后,我希望场景重新加载或返回菜单重新开始。一遍又一遍地选择随机数据{}元素。

我已经尝试返回菜单,或者去了一个重复的场景但是我似乎无法正确卸载由我的2D阵列创建的对象,因为我无法将每个项目正确地添加到显示组中。我已经尝试了我能想到的一切。现在我的游戏循环设置为再次添加游戏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

3 个答案:

答案 0 :(得分:3)

是的,这很简单。

这是一个例子(假设你已经实现了故事板)

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

现在一切都会好起来的。)

答案 1 :(得分:2)

将所有显示对象添加到组中。local myGroup = display.newGroup(); local img = display.newImage("yourimage.png"); myGroup:insert(img); 例如

如果您使用此方法,则在更改屏幕时,所有显示对象都不会刷新内存。

答案 2 :(得分:0)

使用电晕故事板时,您需要向组中添加显示对象。例如,您需要将所有显示对象添加到screenGroup中,以便在更改场景时将其删除。