在场景变化CoronaSDK Storyboard中,表格中的图像不会被删除

时间:2014-01-09 23:27:33

标签: lua corona lua-table scene corona-storyboard

我有一张桌子,当选择正确答案时会显示图像,当我点击“backButton”移回“MainMenu”时,图像会停留在那里,但是当没有选择正确的答案时没有出现,它没有出现在现场过渡。

以下是代码:

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

2 个答案:

答案 0 :(得分:3)

无法在组中添加本机对象,因此它们也会出现在其他电晕显示对象上方,因为它们不是OpenGL的一部分。

它们应该在scene:destroy()函数中单独删除。

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

使用这个: table.removeSelf() 表=零

答案 1 :(得分:-1)

在CoronaSDK中,始终将所有内容添加到组(将组视为图层)非常重要

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

执行此操作时,如果删除该组,则图像随附。同样重要的是要记住将新组添加到视图中。

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