如何破坏lua中的显示对象?

时间:2014-11-25 18:40:29

标签: lua corona

有两个文件scene2.lua和scene3.lua。当我第二次点击后退按钮[scene3.lua]时,extraText和gameOver显示对象不会完全破坏。他们在scene2.lua中出现,回到scene2。请帮我修复bug!我只分享了scene3.lua文件。

local physics = require("physics")
    local composer = require( "composer")
    local scene = composer.newScene()

     local kineticEnergy
     local potentialEnergy
     local height
     local gameOver

    local timer1
    local timer2
    local  timer3
    local  timer4
    local backBtn
    local textback

    local extraText

    function scene:create()

    local sceneGroup = self.view

    soundEffect = audio.loadSound( "bowling1.wav")
    mybg = display.newImage("bg.png")
    mybg.x = 480 * 0.5
    mybg.y = 320 * 0.5

    path = display.newRect(200, 330, 600, 200)
    path:setFillColor( 0.3, 0.3, 0.3 )
    path.rotation = 15

    rightWall = display.newRect( 500, 300, 20, 300 )

    road = display.newRect( 0, 0, 500, 10 )
    road.x =  display.contentCenterX 
    road.y = display.contentCenterY * 1.5
    road:setFillColor( 0.3, 0.3, 0.3 )
    road.rotation = 15

    myrect = display.newRect(61, 160, 95, 25)
    myrect:setFillColor( 0.54, 0.03 ,0.31 )
    myrect.rotation = 15

    wheelFront = display.newCircle( 32, 166, 12 )
    wheelFront:setFillColor(1, 0.72, 0.75)

    wheelRear = display.newCircle( 82, 179, 12 )
    wheelRear:setFillColor(1,  0.72, 0.75)

    text1 = display.newText("K.E", 200, 100, "Eraser", 25)
    text1:setFillColor( 1,0,0 )
    text2 = display.newText("P.E", 340, 100, "Eraser", 25)
    text2:setFillColor( 1,0,0 )
    text3 = display.newText("Height ", 70, 295, "Eraser", 20)
    text3:setFillColor( 1,1,1 )
    text3:toFront( )
    text4 = display.newText("Velocity ", 70, 250, "Eraser", 20)
    text4:setFillColor( 1,1,1 )

     startGame = display.newText( "TAP TO START", 250, 180, "KG Happy Solid", 30  )
     startGame:setFillColor( 0.86, 0.07, 0.20 )


    backBtn = display.newRoundedRect( 250, 300, 100, 30, 5 )

    backBtn:setFillColor( 0.8, 0.2, 0 )
    backBtn.isVisible = false


     textback = display.newText( "Back" , 250, 301, "Eraser", 20 )
     textback:setFillColor( 1, 1, 1 )
     textback.isVisible = false








    sceneGroup:insert( mybg )
    sceneGroup:insert( path )
    sceneGroup:insert(rightWall)
    sceneGroup:insert(road)
    sceneGroup:insert(myrect)
    sceneGroup:insert(wheelFront)
    sceneGroup:insert(wheelRear)
    sceneGroup:insert(text1)
    sceneGroup:insert(text2)
    sceneGroup:insert(text3)

    sceneGroup:insert(backBtn)
    sceneGroup:insert(textback)
    sceneGroup:insert(startGame)
    sceneGroup:insert(text4)
    --sceneGroup:insert(extraText)
    --sceneGroup:insert(soundEffect)
    --sceneGroup:insert(playBtn)
    --sceneGroup:insert(textPlay)




    end


    function scene:show(event)

    local sceneGroup = self.view

    local phase = event.phase

        if phase == "did" then
           composer.removeScene("scene2")
            physics.start()
             function playGame(event)
            numTaps = event.numTaps
            if numTaps == 2 then
                --if event.phase == "ended" then
                audio.play( soundEffect )

                display.remove(startGame)
                startGame = nil



            physics.setGravity( 0,  9.8)

            physics.addBody(rightWall, "static")
            physics.addBody( road, "static", {groupIndex = -1} )
            physics.addBody( myrect, "dynamic", {density = 2, groupIndex = -1} )
            physics.addBody( wheelFront, "dynamic", {density =10, radius = 12, groupIndex = -1})
            physics.addBody(wheelRear,"dynamic", {density=10, radius = 12, groupIndex = -1})

            local LeftwheelJoint = physics.newJoint( "pivot", myrect, wheelFront, 32, 166)

            local RightwheelJoint = physics.newJoint( "pivot",  myrect, wheelRear ,  82, 179)

            LeftwheelJoint.isMotorEnabled = true
            LeftwheelJoint.motorSpeed = 200
            RightwheelJoint.isMotorEnabled = true
            RightwheelJoint.motorSpeed = 200

            --LeftwheelJoint.maxMotorTorque = 1000
            --RightwheelJoint.maxMotorTorque = 1000



                                                                function energyFirst()

                                                                     if(extraText) then
                                                                    extraText.parent:remove( extraText )
                                                                    --print("K.E medi removed")
                                                                end
                                                                extraText = nil

                                                                local options = {
       text = "As the cart rolls down the hill from its elevated position, its mechanical energy is transformed from potential energy to kinetic energy.",
       x = display.contentCenterX,
       y = 50,
      fontSize = 18,
      font = "Eraser",

       width = 480,
       height = 0,
       align = "center"
    }
                                                                    extraText = display.newText( options)
                                                                    --extraText.font = native.newFont("Eraser")
                                                                    extraText:setFillColor( 0.54, 0.03 ,0.31 )

                                                                     if(kineticEnergy) then
                                                                    kineticEnergy.parent:remove( kineticEnergy )
                                                                    --print("K.E medi removed")
                                                                end

                                                                    if (potentialEnergy) then
                                                                    potentialEnergy.parent:remove( potentialEnergy )
                                                                    --print("p.E medi removed")
                                                                end

                                                                if (height) then
                                                                  height.parent:remove( height )
                                                                    --print("p.E medi removed")
                                                                end

                                                                 if (velocity) then
                                                                  velocity.parent:remove( velocity)
                                                                    --print("p.E medi removed")
                                                                end



                                                                 kineticEnery = nil
                                                                 potentialEnergy = nil
                                                                 height = nil
                                                                 velocity = nil


                                                             kineticEnergy = display.newText("zero", 250, 100, "Eraser", 20)
                                                             potentialEnergy = display.newText("Maximum", 410, 100, "Eraser", 20)
                                                             height = display.newText("2 ", 120, 295, "Eraser", 20)
                                                            velocity = display.newText("1 ", 120, 250, "Eraser", 20)



                                                            end

                                                        function energySecond()

                                                                if(kineticEnergy) then
                                                                    kineticEnergy.parent:remove( kineticEnergy )
                                                                   -- print("K.E zero removed")
                                                                end

                                                                if (potentialEnergy) then
                                                                    potentialEnergy.parent:remove( potentialEnergy )
                                                                    --print("p.E max removed")
                                                                end

                                                                 if (height) then
                                                                  height.parent:remove( height )
                                                                    --print("p.E medi removed")
                                                                end

                                                                if (velocity) then
                                                                  velocity.parent:remove( velocity)
                                                                    --print("p.E medi removed")
                                                                end
                                                              velocity = nil

                                                                 kineticEnery = nil
                                                                 potentialEnergy = nil
                                                                 height = nil
                                                         --print(kineticEnergy)
                                                         --print(potentialEnergy)


                                                                 kineticEnergy = display.newText("MidValue", 270, 100, "Eraser", 20)
                                                                 potentialEnergy = display.newText("MidValue", 410, 100, "Eraser", 20)
                                                                  height = display.newText("1 ", 120, 295, "Eraser", 20)
                                                                    velocity = display.newText("2 ", 120, 250, "Eraser", 20)


                                                        end
                                                         ---------


                                                        function energyThird()


                                                                    if(kineticEnergy) then
                                                                    kineticEnergy.parent:remove( kineticEnergy )
                                                                    --print("K.E medi removed")
                                                                end
                                                                kineticEnergy = nil

                                                                    if (potentialEnergy) then
                                                                    potentialEnergy.parent:remove( potentialEnergy )
                                                                    --print("p.E medi removed")
                                                                end
                                                                potentialEnergy = nil

                                                                if (height) then
                                                                  height.parent:remove( height )
                                                                    --print("p.E medi removed")
                                                                end
                                                                height = nil

                                                                 if (velocity) then
                                                                  velocity.parent:remove( velocity)
                                                                    --print("p.E medi removed")
                                                                end
                                                                 velocity = nil


                                                                 --kineticEnery = nil
                                                                 --potentialEnergy = nil
                                                                 --height = nil
                                                         --print(kineticEnergy)
                                                         --rint(potentialEnergy)

                                                                 kineticEnergy = display.newText("Maximum", 270, 100, "Eraser", 20)
                                                                 potentialEnergy = display.newText("Zero", 400, 100, "Eraser", 20)
                                                                  height = display.newText("0 ", 120, 295, "Eraser", 20)
                                                                    velocity = display.newText("0 ", 120, 250, "Eraser", 20)
                                                                    gameOver = display.newText( "GAME OVER", 250, 180, "KG Happy Solid", 30  )
                                                                     gameOver:setFillColor( 0.86, 0.07, 0.20 )





                                                        end

                                                            function energyForth()

                                                                     if(extraText) then
                                                                    extraText.parent:remove( extraText )
                                                                    --print("K.E medi removed")
                                                                end
                                                                extraText = nil

                                                                local options = {
       text = "The amount of potential energy loss equals the amount of kinetic energy gained.",
       x = display.contentCenterX,
       y = 50,
      fontSize = 18,
      font = "Courier New",

       width = 480,
       height = 0,
       align = "center"
    }

                                                                    extraText = display.newText(options)
                                                                    extraText:setFillColor( 0.54, 0.03 ,0.31 )


                                                                    if(kineticEnergy) then
                                                                    kineticEnergy.parent:remove( kineticEnergy )
                                                                    --print("K.E medi removed")
                                                                end
                                                                kineticEnergy = nil

                                                                    if (potentialEnergy) then
                                                                    potentialEnergy.parent:remove( potentialEnergy )
                                                                    --print("p.E medi removed")
                                                                end
                                                                potentialEnergy = nil

                                                                if (height) then
                                                                  height.parent:remove( height )
                                                                    --print("p.E medi removed")
                                                                end
                                                                height = nil

                                                                 if (velocity) then
                                                                  velocity.parent:remove( velocity)
                                                                    --print("p.E medi removed")
                                                                end
                                                                 velocity = nil


                                                               --[[ if (gameOver) then
                                                                  gameOver.parent:remove( gameOver)
                                                                    --print("p.E medi removed")
                                                                end]]--
                                                                --gameOver = nil
                                                                 backBtn.isVisible = true
                                                                 textback.isVisible = true








                                                        end


    -------calling functions----

                                                timer1 = timer.performWithDelay(200, energyFirst, 1)
                                                timer2 = timer.performWithDelay(1500, energySecond, 1)
                                                timer3 = timer.performWithDelay(2500, energyThird, 1)
                                                timer4 = timer.performWithDelay( 3100, energyForth, 1 )




     ------





    end
    end

      function showScreen2(event)
      if event.phase == "ended" then

        composer.gotoScene("scene2")

        backBtn:removeEventListener( "touch", showScreen2 )
       end

      end


       function showScreen3(event)
      if event.phase == "ended" then
        composer.gotoScene("scene3", "fade", 500)
        playBtn:removeEventListener( "touch", showScreen3)
       end

      end


         backBtn:addEventListener( "touch", showScreen2 )
         Runtime:addEventListener("tap", playGame)
         --playBtn:addEventListener( "touch", showScreen3 )


        end 
    end

    function scene:hide(event)

    local sceneGroup = self.view
    local phase = event.phase
    if phase == "will" then

    physics.stop()
    timer.cancel( timer1 )
     timer.cancel( timer2 )
     timer.cancel( timer3 )
     timer.cancel( timer4 )



      --end  
    elseif phase == "did" then



    --display.remove( gameOver )

     display.remove( extraText )
    extraText = nil


         end
    end

    function scene:destroy(event)


         print("Destroyed Game OVER")
        display.remove( kineticEnergy )
         kineticEnergy = nil
        display.remove( potentialEnergy )
         potentialEnergy = nil
        display.remove( height )
         height = nil

        display.remove( velocity )
        velocity = nil
       -- display.remove( gameOver )
        --gameOver = nil
       -- gameOver.isVisible = false
    display.remove( extraText )
    extraText = nil



        display.remove(sceneGroup)
        sceneGroup = nil
        audio.dispose( soundEffect )
        soundEffect = nil

        display.remove(gameOver)
          gameOver = nil



    end

    scene:addEventListener( "create", scene )
    scene:addEventListener( "show", scene )
    scene:addEventListener( "hide", scene )
    scene:addEventListener( "destroy", scene )

    return scene

1 个答案:

答案 0 :(得分:0)

您的密码:

--sceneGroup:insert(extraText)

您需要将其插入到sceneGroup中才能自动处理。你注释掉了这个插入,因此不允许作曲家处理这个对象。