2014-12-29 94 views
-2

我試圖實現一個水平樣式頁面到我的應用程序。我在有日曆頁面(類似於關卡選擇頁面)的情況下製作出現日曆樣式的應用,用戶選擇一天並將其重定向到顯示聖誕消息的頁面。級別頁面電暈SDK

我使用我在這裏找到的例子得到了一個類似的頁面, http://www.christianpeeters.com/corona-sdk/level-selection-template-for-corona-sdk/

但是,當我在我的應用程序中實現此功能時,當我選擇可用於打開的日期/級別時,一旦完成圖像不會轉換爲完成的圖像(綠色檢查)和日期/級別在它應該可用時它旁邊仍然被鎖定。

我的代碼可以在下面找到,幫助會很好。謝謝!

require "CiderDebugger"; 
-- Add a global background 
local background = display.newImage("xmasbg.jpg",160, 240,300,480) 

local function removeFlake(target) 
     target:removeSelf() 
     target = nil 
end 

local function spawnSnowFlake() 
     local flake = display.newImage("snowflake.png") 
     --group:insert(flake) 
     flake.x = math.random(display.contentWidth) 
     flake.y = -2 
     local wind = math.random(80) - 40 
     transition.to(flake,{time=math.random(3000) + 3000, y = display.contentHeight + 2, x = flake.x + wind, onComplete=removeFlake}) 
     --group:insert(flake) 
end 

local function makeSnow(event) 
     if math.random(10) == 1 then -- adjust speed here by making the random number higher or lower 
      spawnSnowFlake() 
     end 
     return true 
end 

Runtime:addEventListener("enterFrame",makeSnow) 

-- Initialize our global variables 
--local globals = require("globals") 

-- main.lua 
local storyboard = require("storyboard") 
storyboard.purgeOnScreenChange = true 
storyboard.gotoScene("menu_scene") 

menu_scene

local storyboard = require("storyboard") 

local scene = storyboard.newScene() 
local hohoho = audio.loadSound("xmas.wav") 

-- Clear previous scene 
storyboard.removeAll() 

-- local forward references should go here -- 
local function buttonHit(event) 
    audio.play(hohoho) 
    storyboard.gotoScene(event.target.goto, {effect = "slideRight"}) 

end 

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


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

--local background = display.newImage("xmasbg.jpg",160, 240,300,480) 
--group:insert(background) 
local hohoho = audio.loadSound("xmas.wav") 
--group:insert(hohoho) 
local bgMusic = audio.loadSound("bgmusic.wav") 
--group:insert(bgMusic) 
local calendarButton = display.newImage("calendarButton.png", 160, 170) 
group:insert(calendarButton) 
calendarButton:addEventListener("tap", buttonHit) 
calendarButton.goto = "calendar_scene" 

local creditsButton = display.newImage("creditsButton.png", 160, 250) 
group:insert(creditsButton) 
creditsButton:addEventListener("tap", buttonHit) 
creditsButton.goto = "credits_scene" 

local sound = display.newImage("speaker117.png", 290, 495) 
group:insert(sound) 

local backgroundMusicChannel = audio.play(bgMusic, { channel=1, loops=-1, fadein=5000 }) 

--Until Xmas Text 
local xmasText = display.newText("Until Christmas", 0, 0, native.systemFontBold, 30) 
xmasText: setTextColor(0, 0,.75) 
xmasText.x, xmasText.y = 160, 80 
group:insert(xmasText) 


--The number of days remaining 
local dayText = display.newText("", 0, 0, native.systemFontBold, 30) 
dayText:setTextColor(0, 0, .75) 
dayText.x, dayText.y = 55, 45 
group:insert(dayText) 

--The number of hours remaining 
local hourText = display.newText("", 0, 0, native.systemFontBold, 30) 
hourText:setTextColor(0, 0, .75) 
hourText.x, hourText.y = 160, 45 
group:insert(hourText) 

--The number of minutes remaining 
local minutesText = display.newText("", 0, 0, native.systemFontBold, 30) 
minutesText:setTextColor(0, 0, .75) 
minutesText.x, minutesText.y = 255, 45 
group:insert(minutesText) 

-- Create labels to indicate what the numbers mean 
local dayLabel = display.newText("Days", 0, 0, native.systemFont, 30) 
dayLabel:setTextColor(0, 0, .75) 
dayLabel.x = 60; dayLabel.y = 5 
group:insert(dayLabel) 

local hourLabel = display.newText("Hours", 0, 0, native.systemFont, 30) 
hourLabel:setTextColor(0, 0, .75) 
hourLabel.x = 160; hourLabel.y = 5 
group:insert(hourLabel) 

local minuteLabel = display.newText("Mins", 0, 0, native.systemFont, 30) 
minuteLabel:setTextColor(0, 0, .75) 
minuteLabel.x = 260; minuteLabel.y = 5 
group:insert(minuteLabel) 

--Function to update the countdown clock 
local function updateTime() 
local time = os.date("*t") 

local daysLeft = 358 - time.yday 
if (daysLeft < 10) then 
daysLeft = "0" .. daysLeft 
end 
dayText.text = daysLeft 

local hoursLeft = 23 - time.hour 
if (hoursLeft < 10) then 
hoursLeft = "0" .. hoursLeft 
end 
hourText.text = hoursLeft 

local minutesLeft = 60 - time.min 
if (minutesLeft < 10) then 
minutesLeft = "0" .. minutesLeft 
end 
minutesText.text = minutesLeft 


end 

--Update the time once immediately to display the correct time 
updateTime() 

-- Update the clock once per second 
local clockTimer = timer.performWithDelay(1000, updateTime, -1) 

--[[function calendarButton:tap(event) 
    audio.play(hohoho) 
    calendarButton:removeSelf() 
    display.newImage("pushedCalendarButton.png", 160, 170) 
end--]] 

--[[function creditsButton:tap(event) 
    storyboard.gotoScene("credits_scene") 
    audio.play(hohoho) 
    creditsButton:removeSelf() 
    --display.newImage("pushedCreditsButton.png", 160, 250) 
end]]-- 

function sound: tap(event) 
    --[[if(self.status == "playing") then 
     sound:removeSelf() 
     display.newImage("mute.png", 290, 495) 
     audio.pause() 
     self.status = "paused" 

    elseif(self.status == "paused") then 
     display.newImage("speaker117.png", 290, 495) 
     audio.play() 
     --self.status = "paused" 



    end--]] 


    sound:removeSelf() 
    display.newImage("mute.png", 290, 495) 
    audio.stop() 
end 



--[[local function removeFlake(target) 
     target:removeSelf() 
     target = nil 
end 

local function spawnSnowFlake() 
     local flake = display.newImage("snowflake.png") 
     --group:insert(flake) 
     flake.x = math.random(display.contentWidth) 
     flake.y = -2 
     local wind = math.random(80) - 40 
     transition.to(flake,{time=math.random(3000) + 3000, y = display.contentHeight + 2, x = flake.x + wind, onComplete=removeFlake}) 
     --group:insert(flake) 
end 

local function makeSnow(event) 
     if math.random(10) == 1 then -- adjust speed here by making the random number higher or lower 
      spawnSnowFlake() 
     end 
     return true 
end 



Runtime:addEventListener("enterFrame",makeSnow)--]] 
calendarButton:addEventListener("tap", calendarButton) 
--creditsButton:addEventListener("tap", creditsButton) 
sound:addEventListener("tap", sound) 

end 


-- Called BEFORE scene has moved onscreen: 
function scene:willEnterScene(event) 
    local group = self.view 

end 


-- Called immediately after scene has moved onscreen: 
function scene:enterScene(event) 
    local group = self.view 

end 


-- Called when scene is about to move offscreen: 
function scene:exitScene(event) 
    local group = self.view 

end 


-- Called AFTER scene has finished moving offscreen: 
function scene:didExitScene(event) 
    local group = self.view 

end 


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

end 


-- Called if/when overlay scene is displayed via storyboard.showOverlay() 
function scene:overlayBegan(event) 
    local group = self.view 
    local overlay_name = event.sceneName -- name of the overlay scene 

end 


-- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay() 
function scene:overlayEnded(event) 
    local group = self.view 
    local overlay_name = event.sceneName -- name of the overlay scene 

end 

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

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

-- "willEnterScene" event is dispatched before scene transition begins 
scene:addEventListener("willEnterScene", 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) 

-- "didExitScene" event is dispatched after scene has finished transitioning out 
scene:addEventListener("didExitScene", 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) 

-- "overlayBegan" event is dispatched when an overlay scene is shown 
scene:addEventListener("overlayBegan", scene) 

-- "overlayEnded" event is dispatched when an overlay scene is hidden/removed 
scene:addEventListener("overlayEnded", scene) 

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

return scene 

calendar_scene

local storyboard = require("storyboard") 
local scene = storyboard.newScene() 
local hohoho = audio.loadSound("xmas.wav") 

-- Clear previous scene 
storyboard.removeAll() 

-- local forward references should go here -- 

levels = 
{ 
1, 2, 2, 2 ,2, --1 means level is open to be played (level.png) 
2, 2, 2, 2, 2, --2 means level is locked (locked.png) 
2, 2, 2, 2, 2, --3 means level is completed (greenchecked.png) 
2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 
} 

images ={ 
    { getFile = "gift.png", types = "play"}, 
    { getFile = "lock.png", types = "locked"}, 
    { getFile = "greenchecked.png", types = "done"} 
} 

local function buttonHit(event) 
    storyboard.gotoScene (event.target.destination, {effect = "slideLeft"}) 
     audio.play(hohoho) 
    print(event.target.destination) 
     return true 
end 
--------------------------------------------------------------------------------- 
-- BEGINNING OF YOUR IMPLEMENTATION 
--------------------------------------------------------------------------------- 


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

    local levelIndex = 0 
      for i=0,4 do 
       for j=1,5 do 

        tablePlace = i*5 + j 
        levelIndex = levelIndex + 1 
        local imagesId = levels[levelIndex] 
        levelImg = display.newImageRect (images[imagesId].getFile , 50, 50) 
        levelImg.x = -22 + (j*62) 
        levelImg.y = 130+ (i*70) 
        levelImg.anchorY = .8 
        group:insert(levelImg) 

        local levelText = display.newText(tablePlace,0,0,native.systemFont,28) 
        levelText.x = -22 + (j*62) 
        levelText.y = 125+ (i*70) 
        group:insert(levelText) 

        levelImg.destination = "level0"..tostring(tablePlace) 

        if images[imagesId].types ~= "locked" then 
        levelImg:addEventListener("tap", buttonHit) 
        end 
       end 
      end 

    local heading = display.newText("Christmas Countdown", 0, 0, native.systemFontBold, 31) 
     heading: setTextColor(.75, 0, 0) 
     heading.x, heading.y = 160, 35 
     group:insert(heading) 

     local backButton = display.newImage("backButton.png", 43, 495) 
     group:insert(backButton) 
     backButton:addEventListener("tap", buttonHit) 
     backButton.destination = "menu_scene" 
end 


-- Called BEFORE scene has moved onscreen: 
function scene:willEnterScene(event) 
    local group = self.view 

end 


-- Called immediately after scene has moved onscreen: 
function scene:enterScene(event) 
    local group = self.view 

end 


-- Called when scene is about to move offscreen: 
function scene:exitScene(event) 
    local group = self.view 

end 


-- Called AFTER scene has finished moving offscreen: 
function scene:didExitScene(event) 
    local group = self.view 

end 


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

end 


-- Called if/when overlay scene is displayed via storyboard.showOverlay() 
function scene:overlayBegan(event) 
    local group = self.view 
    local overlay_name = event.sceneName -- name of the overlay scene 

end 


-- Called if/when overlay scene is hidden/removed via storyboard.hideOverlay() 
function scene:overlayEnded(event) 
    local group = self.view 
    local overlay_name = event.sceneName -- name of the overlay scene 

end 

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

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

-- "willEnterScene" event is dispatched before scene transition begins 
scene:addEventListener("willEnterScene", 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) 

-- "didExitScene" event is dispatched after scene has finished transitioning out 
scene:addEventListener("didExitScene", 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) 

-- "overlayBegan" event is dispatched when an overlay scene is shown 
scene:addEventListener("overlayBegan", scene) 

-- "overlayEnded" event is dispatched when an overlay scene is hidden/removed 
scene:addEventListener("overlayEnded", scene) 

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

return scene 

level01

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

local widget = require ("widget") 
local calendar_scenefile = require ("calendar_scene") 


-- local forward references should go here -- 

local completegameBtn 


local function buttonHit(event) 
    storyboard.gotoScene ( event.target.destination, {effect = "slideLeft"}) 
    return true 
end 


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

    local title = display.newText("Level 1", 0, 0, "Helvetica", 38) 
    title.x = 160 
    title.y = display.screenOriginY + 40 
    group:insert(title) 

    local backBtn = display.newText( "Back", 0, 0, "Helvetica", 25) 
    backBtn.x = display.screenOriginX + 50 
    backBtn.y = display.contentHeight - 30 
    backBtn.destination = "calendar_scene" 
    backBtn:addEventListener("tap", buttonHit) 
    group:insert(backBtn) 
end 


-- Called immediately after scene has moved onscreen: 
function scene:enterScene(event) 
    local group = self.view 

     local function btnClicked (event) 
    --levelProgress = levelProgress + 1 
    levels[1] = 3 
    levels[2] = 1 
    completegameBtn.destination = "calendar_scene" 
    completegameBtn:addEventListener("tap", buttonHit) 
    end 

    completegameBtn = widget.newButton { label = "Complete game", onRelease=btnClicked} 
    completegameBtn.x = 160 
    completegameBtn.y = 240 
    group:insert (completegameBtn) 
end 

-- Called when scene is about to move offscreen: 
function scene:exitScene(event) 
    local group = self.view 

    -- INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) 
    -- Remove listeners attached to the Runtime, timers, transitions, audio tracks 

end 

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

    -- INSERT code here (e.g. remove listeners, widgets, save state, etc.) 
    -- Remove listeners attached to the Runtime, timers, transitions, audio tracks 

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

您的縮進全部搞砸了,使得難以看到代碼的結構。請注意SO上的格式化:用空格替換所有的選項卡,並確保代碼按照原樣對齊。 – Schollii

+0

對不起我非常瞭解SO(和編程本身!)。我會嘗試將其編輯爲更易讀的格式。感謝您的建議。 – kanedk90

+0

@ kanedk90你會從main.lua去calendar_scene嗎?如果是這樣,請包括main.lua在您的文章... – Melquiades

回答

0

嘗試註釋掉到removeAll調用,它是不太可能,你需要他們:清除和刪除(卸載)的場景主要是爲了節省內存,所以清洗和清除功能只能用在場景很少被重新訪問但包含大量對象。對於所有其他場景(那些經常訪問的場景或沒有太多對象的場景),這些場景並不是必需的,並且可能會導致您可能遇到的細微的錯誤。

請注意,storyboard.purgeOnScreenChange不存在,您可能打算storyboard.purgeOnSceneChange。但是,錯字意味着設置保持不真實,所以它不是您遇到的問題的一部分。但是,如果您修復了錯字,也可以在main.lua中註釋掉該行,否則它會產生相同的效果。

因此,請注意與故事板場景清除和清除功能有關的所有行,如果您覺得您真的需要它們(因爲默認的Corona行爲不足),請重新引入一個位置並測試場景之間。

0

請注意,電暈已移動到作曲家來管理場景,而不是故事板。話雖如此,你的關卡還是鎖定的,因爲你的關卡和設置聽衆的代碼位於createScene函數中,因此在你的情況下這個函數只會被調用一次,因爲Corona不會調用createScene如果你的場景有沒有被完全銷燬。

從文檔場景:創建()的作曲(適用於故事板以及):

注意的情景:創建()如果場景的 視圖不存在功能只叫 - 也就是說,它將在第一次加載場景時被調用。但是,作曲家試圖將場景保留在內存 中,假設您稍後會重新訪問該場景,因此如果場景保留在 內存中,則不會調用scene()()。

所以,即使你修改全球層面表,當你回到calendar_scene,createScene不被再次調用。你有3種選擇:

  1. 你可以將你的繪圖器/接收器代碼設置爲willEnterScene 功能,它會在你回到 calendar_scene再次調用,這一次更新後的值(但它的不是一個好主意 - 記得刪除exitScene顯示對象,空監聽器等)

  2. 使用storyboard.purgeOnSceneChange(而不是purgeOnScreenChange,如 Schollii注意到),並將其設置爲true,這樣的場景將當你離開它時,被破壞 ,意思g當你從級別選擇回來時,createScene將再次被調用 (更好,但是再次理解相應的場景內存管理和代碼比使用快捷方式更重要,儘管有時這些方法也有效)。

  3. MigrateMigrate您的代碼爲Composer API,與此同時,它可能是 到understand scene management的好主意。然後繪製您的關卡選擇,並在特定關卡返回時根據需要進行更新。