2014-01-09 27 views
1

我有一个表格,当正确答案被选中时显示图像,当我点击“backButton”返回到“MainMenu”时,图像停留在那里,但是如果没有选择正确的答案并且图像没有显示出来,它就不会显示在场景转换中。一张桌子上的图像在场景变化时不会被删除,CoronaSDK Storyboard

下面是代码:

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

storyboard.purgeOnSceneChange = true 
display.setStatusBar(display.HiddenStatusBar) 
local globalGameAnswer = 0 
-- local functions go here 



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



local function onButtonTouch(event) 
    if (event.phase == "ended") then 
     -- move to next scene 
     --storyboard.gotoScene('MainMenu') 
    end 
end 

local function backButtonTouch(event) 
    print("button is touched") 
    if(event.phase == "ended") then 
     storyboard.gotoScene('MainMenu') 
    end 
end 


---------------------------------------------------------------------------------- 
-- 
--  NOTE: 
--  
--  Code outside of listener functions (below) will only be executed once, 
-- 
--------------------------------------------------------------------------------- 

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

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

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

    --  CREATE display objects and add them to 'group' here. 
    --  Example use-case: Restore 'group' from previously saved state. 
    ---------------------Lists------------------------ 

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


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



end 


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

    --  INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) 


    ----------------------------------------------------------------------------- 
    local backgroundImage = display.newImageRect("./images/background.png" , 1024, 768) 
    backgroundImage.x = display.contentWidth/2 
    backgroundImage.y = display.contentHeight/2 
    group:insert(backgroundImage) 

    local animalChoiceOne = (math.random(1,10)) 
    local mammalTable = { 
     [1] = "Deer", 
     [2] = "Bear", 
     [3] = "Dog", 
     [4] = " Cat", 
     [5] = "Fox", 
     [6] = "Pig", 
     [7] = "Horse", 
     [8] = "Cow", 
     [9] = "Sheep", 
     [10] = "Wolf" 
} 
    print(mammalTable[animalChoiceOne]) 
    local animalNameOne = (mammalTable[animalChoiceOne]) 

    local animalChoiceTwo = (math.random(11,20)) 
    local birdTable = { 
     [11] = "Crow", 
     [12] = "Loon", 
     [13] = "Duck ", 
     [14] = " Jays", 
     [15] = "Goose", 
     [16] = "Parrot", 
     [17] = "Eagle", 
     [18] = "Dove", 
     [19] = "Ostrich", 
     [20] = "Owl" 

       } 
    print(birdTable[animalChoiceTwo]) 
    local animalNameTwo = (birdTable[animalChoiceTwo]) 

    local animalChoiceThree = (math.random(21,30)) 
    local insectTable = { 

     [21] = "Wasp", 
     [22] = "Ant", 
     [23] = "Tick ", 
     [24] = "Bee", 
     [25] = "Moth", 
     [26] = "Fly", 
     [27] = "Beetle", 
     [28] = "Cricket", 
     [29] = "Mantis", 
     [30] = "Hornet" 

       } 
    print(insectTable[animalChoiceThree]) 
    local animalNameThree = (insectTable[animalChoiceThree]) 

    local animalChoiceFour = (math.random(31,35)) 
    local reptileTable = { 
     [31] = "Gecko", 
     [32] = "Snake", 
     [33] = "Lizard", 
     [34] = " Python", 
     [35] = "Turtle", 

       } 
    print(reptileTable[animalChoiceFour]) 
    local animalNameFour = (reptileTable[animalChoiceFour]) 

    local animalChoiceFive = (math.random(36,40)) 
    local amphibianTable = { 
     [36] = "Tadpole", 
     [37] = "Toad", 
     [38] = "Frog", 
     [39] = "Newt", 
     [40] = "Eel", 

       } 
    print(amphibianTable[animalChoiceFive]) 
    local animalNameFive = (amphibianTable[animalChoiceFive]) 



    --******Change every level*******----- 
    --table containing Animal Question. 

    local levelQuestionList = { 
     [1] = "What is big, brown, and has antlers like a moose?",--Deer 
     [2] = "This Animal is big, fuzzy, and likes to eat Honey. ",--Bear 
     [3] = "Which Animal likes to bury bones in the backyard ?",--Dog 
     [4] = " What is small, furry, and says meow.",--Cat 
     [5] = "What does the ____ Say!", --Fox 
     [6] = "This Animal is pink, fat, and says oink oink!",--Pig 
     [7] = "Giddy up ____ lets go for a ride on your back!",--Horse 
     [8] = "Moo! I make milk when you squeeze my Utter.",--Cow 
     [9] = "I'm the Animal that you use to make warm cotton sweaters.",--sheep 
     [10] = "I like to live in the woods and I travel in my pack.",--Wolf 
     [11] = "I'm a small, black, bird and I'm everywhere in Canada.",--Crow 
     [12] = "I'm an animal that is on the Canadian dollar coin!",-- Loon 
     [13] = "Quack, Quack! Ever read the story of The Ugly______?",--Duck 
     [14] = " Were known for a famous baseball Team!",--Jays 
     [15] = "We fly away in Winter, but comeback in Summer. We are the Canadian______.", --Goose 
     [16] = "Which bird can be taught to speak english?",--Parrot 
     [17] = "(Tricky) Which bird is America's most favourited bird ?",--Eagle 
     [18] = "A white bird representing the Holy Spirit in Christianity.",--Dove 
     [19] = "A big bird that runs really fast, but cannot fly?",--Ostrich 
     [20] = "Hoo-hoo, I make this sound at night time, what am I ?",--Owl 
    ---------------------------------Next Update-------------------------------------- 
     [21] = "When you see me, you can stand very still, or run as fast as you can.",--Wasp 
     [22] = "I build tunnels undergound and sometimes, you'll see my hill in the grass",--Ant 
     [23] = "I make dogs very itchy. and once you see me, I'll run away.",--Tick 
     [24] = " Although I sting, I make sweet delicious honey for you guys.",--Bee 
     [25] = "I'm just like a butterfly, except instead of a caterpillar, I start off as an Inch worm.", --Moth 
     [26] = "Buzzz, I like to fly around your house and sometimes you will try and swat me.",--Fly 
     [27] = "I'm one of the bigger bugs, but I can still fly. My wings are always hidden thats all.",--Beetle 
     [28] = "I'm an animal that makes a noise all the time at night. All I do is rub my feet together ?",--Cricket 
     [29] = "I'm known as the Praying_____. I have claws on each of my arms for snatching things.",--Mantis 
     [30] = "(Tricky) I am one of the most feared stingers, even more than a bee and a wasp! what am I?",--Hornet 
     [31] = "I can climb on walls and sometimes, people keep me as a Pet!",--Gecko 
     [32] = "Hisss, I am Long and slithery but I have no legs.", -- Snake 
     [33] = "I'm scaly, I have 4 feet, and I can regrow my tail if it is cut off, what am I?",--Lizard 
     [34] = " I'm A huge Snake that can Swallow animals as big as Sheep!",--Python 
     [35] = "I have a hardshell and I'm very slow, what am I?", --Turtle 
     [36] = "I'm a baby version of a Frog that just hatched from its egg. What am I called?",--Tadpole 
     [37] = "I have warts on my back and I make the sound Croak.", --Toad 
     [38] = "I have big eyes, a longue tongue and I make the sound Ribbit.",--Frog 
     [39] = "I'm sort of like a Lizard, But I can go in and live in the water as well.",--Newt 
     [40] = "Some people call me the snake of the sea, Do you know what I am?"--Eel 

       } 




    local backButton = display.newImageRect("./images/backButton.png", 125, 125) 
    backButton.x = 80 
    backButton.y = 65 
    backButton.isVisible = true 
    backButton:addEventListener ("touch", backButtonTouch) 
    group:insert(backButton) 

    local pictureTable = { 
     [1] = display.newImage("./images/deer.png",400, 300), 
     -- [2] = display.newImage("./images/bear.png",400, 300), 
     -- [3] = display.newImage("./images/dog.png",400, 300), 
     --[4] = display.newImage("./images/cat.png",400, 300), 
     -- [5] = display.newImage("./images/fox.png",400, 300), 
     --[6] = display.newImage("./images/pig.png",400, 300), 
     --[7] = display.newImage("./images/horse.png",400, 300), 
     --[8] = display.newImage("./images/cow.png",400, 300), 
     --[9] = display.newImage("./images/sheep.png",400, 300), 
     --[10] = display.newImage("./images/wolf.png",400, 300), 
     --[11] = display.newImage("./images/crow.png",400, 300), 
     -- [12] = display.newImage("./images/loon.png",400, 300), 
     -- [13] = display.newImage("./images/duck.png",400, 300), 
    -- [14] = display.newImage("./images/jay.png",400, 300), 
    -- [15] = display.newImage("./images/goose.png",400, 300), 
    -- [16] = display.newImage("./images/parrot.png",400, 300), 
    -- [17] = display.newImage("./images/eagle.png",400, 300), 
    -- [18] = display.newImage("./images/dove.png",400, 300), 
    -- [19] = display.newImage("./images/ostrich.png",400, 300), 
    -- [20] = display.newImage("./images/owl.png",400, 300) 
} 
    print(mammalTable[animalChoiceOne]) 
    local animalNameOne = (mammalTable[animalChoiceOne]) 
    ----------------Groups--------------------- 


local correctAnswer = 1 
local answerOne = display.newImageRect("./images/default.png", 180, 88) 
    group:insert(answerOne) 
local answerTwo = display.newImageRect("./images/default.png", 180, 88) 
    group:insert(answerTwo) 
local answerThree = display.newImageRect("./images/default.png", 180, 88) 
    group:insert(answerThree) 
local answerFour = display.newImageRect("./images/default.png", 180, 88) 
    group:insert(answerFour) 
local answerFive = display.newImageRect("./images/default.png", 180, 88) 
    group:insert(answerFive) 
local answerSix = display.newImageRect("./images/default1.png", 180, 88) 
    group:insert(answerSix) 
local nextLevel = display.newImageRect("./images/nextLevel.png", 176, 88) 
nextLevel.isVisible = false 
    group:insert(nextLevel) 
local animalPicture = pictureTable[1] 
animalPicture.isVisible = false 


local answerOneText= display.newText(mammalTable[1],0,0,nil, 50) 
answerOneText:setTextColor(255, 255, 255) 
group:insert(answerOneText) 
local answerTwoText= display.newText(animalNameTwo,0,0,nil, 50) 
answerTwoText:setTextColor(255, 255, 255) 
group:insert(answerTwoText) 
local answerThreeText= display.newText(animalNameThree,0,0,nil, 50) 
answerThreeText:setTextColor(255, 255, 255) 
group:insert(answerThreeText) 
local answerFourText= display.newText(animalNameFour,0,0,nil, 50) 
answerFourText:setTextColor(255, 255, 255) 
group:insert(answerFourText) 
local answerFiveText= display.newText(animalNameFive,0,0,nil, 50) 
answerFiveText:setTextColor(255, 255, 255) 
group:insert(answerFiveText) 
local correctText = display.newText("Good Job" ,460,300,nil, 50) 
correctText.isVisible = false 
group:insert(correctText) 
local incorrectText = display.newText("Try Again" ,460,300,nil, 50) 
incorrectText.isVisible = false 
correctText:setTextColor(255, 255, 255) 
group:insert(incorrectText) 
local levelQuestion = display.newText(levelQuestionList[1] ,460,100,550, 300, native.systemFont, 45) 
levelQuestion:setTextColor(255, 255, 255) 
group:insert(levelQuestion) 

nextLevel.x = 500 
nextLevel.y = 700 

animalPicture.x = 230 
animalPicture.y = 280 

answerSix.x = 800 
answerSix.y = 350 

local function checkAnswer (correctAnswer) 
    if (correctAnswer == globalGameAnswer) then 
    animalPicture.isVisible = true 
    correctText.isVisible = true 
    incorrectText.isVisible = false 
    nextLevel.isVisible = true 


    end 
--Incorrect Answer 
if correctAnswer ~= globalGameAnswer then 
incorrectText.isVisible = true 
correctText.isVisible = false 


end 
    end 

local answerNumberOne = display.newGroup() 

answerNumberOne:insert(answerOne,true) 
answerNumberOne:insert(answerOneText, true) 
answerNumberOne:translate(200, 500) --- INIT piece's position to 200,500 
group:insert(answerNumberOne) 

local function AnswerOneTouch(touch) 
--moving image answerOne  

    if (touch.phase=="begin") then 

    end 
    if (touch.phase=="moved") then 
    end 
    if (touch.phase=="ended") then 

    transition.to(answerNumberOne, { time=800, x=(answerSix.x), y=(answerSix.y),onComplete=returnToSender}) 
    print ("touch ended") 
     globalGameAnswer = 1 
     checkAnswer(correctAnswer) 
    end 
end 

answerNumberOne:addEventListener ("touch", AnswerOneTouch) 


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

local answerNumberTwo = display.newGroup() 
answerNumberTwo:insert(answerTwo, true) 
answerNumberTwo:insert(answerTwoText, true) 
answerNumberTwo:translate(810, 500) --- INIT piece's position to 810, 500 
group:insert(answerNumberTwo) 

local function AnswerTwoTouch(touch) 
--moving image answerTwo 
    if (touch.phase=="begin") then 
    end 
    if (touch.phase=="moved") then 
    end 
    if (touch.phase=="ended") then 

    transition.to(answerNumberTwo, { time=800, x=(answerSix.x), y=(answerSix.y),onComplete=returnToSender}) 
    print ("touch ended") 
     globalGameAnswer = 2 
     checkAnswer(correctAnswer) 
    end 
end 

answerNumberTwo:addEventListener ("touch", AnswerTwoTouch) 



---------------------------------------------------- 
local answerNumberThree = display.newGroup() 

answerNumberThree:insert(answerThree, true) 
answerNumberThree:insert(answerThreeText, true) 
answerNumberThree:translate(500, 580) --- INIT piece's position to 500, 580 
group:insert(answerNumberThree) 

local function AnswerThreeTouch(touch) 
--moving image answerThree 
    if (touch.phase=="begin") then 
    end 
    if (touch.phase=="moved") then 
    end 
    if (touch.phase=="ended") then 
    transition.to(answerNumberThree, { time=800, x=(answerSix.x), y=(answerSix.y),onComplete=returnToSender}) 
    print ("touch ended") 
     globalGameAnswer = 3 
     checkAnswer(correctAnswer) 
    end 
end 
answerNumberThree:addEventListener ("touch", AnswerThreeTouch) 

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

local answerNumberFour = display.newGroup() 
answerNumberFour:insert(answerFour, true) 
answerNumberFour:insert(answerFourText, true) 
answerNumberFour:translate(200, 660) --- INIT piece's position to 200,660 
group:insert(answerNumberFour) 


local function AnswerFourTouch(touch) 
--moving image answerThree 
    if (touch.phase=="begin") then 
    end 
    if (touch.phase=="moved") then 
    end 
    if (touch.phase=="ended") then 
    transition.to(answerNumberFour, { time=800, x=(answerSix.x), y=(answerSix.y),onComplete=returnToSender}) 
    print ("touch ended") 
     globalGameAnswer = 4 
     checkAnswer(correctAnswer) 
    end 
end 
answerNumberFour:addEventListener ("touch", AnswerFourTouch) 

------------------------------------------------------ 
local answerNumberFive = display.newGroup() 

answerNumberFive:insert(answerFive, true) 
answerNumberFive:insert(answerFiveText, true) 
answerNumberFive:translate(810, 660) --- INIT piece's position to 810,660 
group:insert(answerNumberFive) 


local function AnswerFiveTouch(touch) 
--moving image answerThree 
    if (touch.phase=="begin") then 
    end 
    if (touch.phase=="moved") then 
    end 
    if (touch.phase=="ended") then 
     transition.to(answerNumberFive, { time=800, x=(answerSix.x), y=(answerSix.y),onComplete=returnToSender}) 
     print ("touch ended") 

     globalGameAnswer = 5 
     checkAnswer(correctAnswer) 
    end 
end 

answerNumberFive:addEventListener ("touch", AnswerFiveTouch) 

------------------------------------------------------ 
--Returning answers to original postions. 
function returnToSender() 
     transition.to(answerNumberOne, { delay= 1500, time=800, x=200 , y=500}) 
     transition.to(answerNumberTwo, { delay= 1500, time=800, x=810 , y=500}) 
     transition.to(answerNumberThree, { delay= 1500, time=800, x=500 , y=580}) 
    transition.to(answerNumberFour, { delay= 1500, time=800, x=200 , y=660}) 
    transition.to(answerNumberFive, { delay= 1500, time=800, x=810 , y=660}) 


end 
-------------------------------- 
--Clearing Scene For the Next Scene 
local function LevelTransition(touch) 
--moving image answerOne  

    if (touch.phase=="begin") then 

    end 
    if (touch.phase=="moved") then 
    end 
    if (touch.phase=="ended") then 
    storyboard.gotoScene ("LevelTwo","fade", 1000) 
    animalPicture:removeSelf() 
    levelQuestion:removeSelf() 
    correctText:removeSelf() 
    incorrectText:removeSelf() 
    answerNumberOne:removeSelf() 
    answerNumberTwo:removeSelf() 
    answerNumberThree:removeSelf() 
    answerNumberFour:removeSelf() 
    answerNumberFive:removeSelf() 
    nextLevel:removeSelf() 
    backButton:removeSelf() 


    end 
end 

nextLevel:addEventListener ("touch", LevelTransition) 


end 


-- Called when scene is about to move offscreen: 
function scene:exitScene(event) 

    print("1: exitScene event") 
    ----------------------------------------------------------------------------- 

    --  INSERT code here (e.g. stop timers, remove listeners, unload sounds, etc.) 

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

end 


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


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

    --  INSERT code here (e.g. remove listeners, widgets, save state, etc.) 



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

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

请澄清你的问题,以指出什么可行,什么不可行。另外,请尽可能删除与问题无关的代码。有时你甚至可以自己找到解决方案。 – Schollii

+0

您可以删除不必要的代码吗?我不想读200多行随机废话。 –

回答

3

本地对象不能在基被添加,以便它们也出现上述其他电晕显示对象,因为它们不是的OpenGL的一部分。

它们应该在场景中分开移除:destroy()函数。

正如你可能知道我们在self.view &中添加的所有显示对象,self.view被故事板清除,但由于无法将表添加到显示对象,所以无法清除。

使用本: table.removeSelf() 表=零

+0

@ CrazyGeek提问者了解解决方案。我们不能为他编写完整的代码。 –

+0

@Bahshat(RAIS):您的解决方案应该对每个人都有用,并且一旦接受,我们就会考虑,而不是我们可以说提问者有合适的解决方案。 :) – CrazyGeek

+0

@ CrazyGeek许多提问者不知道接受答案,其他谁知道不关心它,他们甚至没有投票,他们得到他们的解决方案和逃跑。 –

-1

在CoronaSDK它随时添加一切一组非常重要的(想想组作为层)

local group = display.newGroup() 
local image = display.newImage("image.png") 
group:insert(image) 

当你这样做,如果该组被删除,图像会随之一起被删除。记住将新组添加到视图也很重要。

local view = self.view 
view:insert(group)