在Corona SDK中移动到下一个场景的问题

时间:2015-05-07 04:10:58

标签: lua corona

我使用的是Corona SDK。我基本上试图这样做,当你点击箭头时,你被引导到另一个场景。出于某种原因,它引发了一个我无法理解的错误。

首先,它说我实施的暂停按钮有问题,所以我删除它以查看问题是否仍然存在。现在它表示该功能存在问题'取消'?!

错误:

?:0: attempt to index a nil value
stack trackback:
?: in function 'cancel'
scene3.lua:377: in function <scene3.lua.368>
?: in function DispatchEvent
?: in function ?
?: in function GoToScene
scene3.lua:74 in fuction <scene3.lua:63>
?: in function <?:221>

这是我的scene3.lua:

---------------------------------------------------------------------------------
--
-- scene3.lua
--
---------------------------------------------------------------------------------

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
system.activate("multitouch")

local physics = require ("physics")
physics.start()
local sound_pickup = audio.loadSound ("pickup.mp3")
local sound_drop = audio.loadSound ("drop.mp3")
local sound_pause = audio.loadSound ("pausebutton.mp3")
--local backgroundMusic = audio.loadStream("Backgroundmusic.mp3")
--local backgroundMusicChannel = audio.play( backgroundMusic, { channel=-1, loops=-100, fadein=5000 }  )



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

local image, text1, text2, text3, memTimer, pause, BedInfo

local function restarto( self, event )
    if event.phase == "began" then
        print("restarted")
        audio.play(sound_click)
        physics.pause()

        return true
    end
end


-- Called when the scene's view does not exist:
function scene:createScene( event )
    --audio.play(backgroundMusic)
 audio.play(backgroundMusic)

    print( "3: createScene event" )

    local screenGroup = self.view

    image = display.newImage( screenGroup, "gamebackground.png" )
    image.x = _W * 0.5
    image.y = _H * 0.5


local function Gooutside( self, event )
    if event.phase == "began" then
        print( "Move outside mate" )


        return true
    end
end



    local function ArrowMove( self, event )
    if event.phase == "began" then

        outside = display.newImage( screenGroup, "Gooutside.png" )
        local group1 = display.newGroup()
        group1:insert(outside)
        outside.x = _W * 0.5
        outside.y = _H * 0.9

        outside.touch = Gooutside
        outside:addEventListener( "touch", outside )
    storyboard.gotoScene( "scene1", "fade", 1000  )
        print( "Button Pressed to leave house" )


        return true
    end
end







    -- Bounding walls, So player cannot walk off screen 
    local TopWall = display.newRect(_W * 0.5, 0, _W, 5)
    local BottomWall = display.newRect(_W * 01, _H * 0, _W, 5)
    local LeftWall = display.newRect(0, _H * 0.5, 5, _H) 
    local RightWall = display.newRect(_W, _H * 0.5, 5, _H) 

    local OutsideWall = display.newRect(_W * 0.5, _H * 0.8, _W, 5)
    OutsideWall.rotation = 25
    OutsideWall:setFillColor( 1,1,1,0)

    local SideWall = display.newRect(_W * 0.8, _H * 0.8, _W, 5)
    SideWall.rotation = -25
    SideWall:setFillColor( 1,1,1,0)

    local BackWall = display.newRect(_W * 0.8, _H * 0.5, _W, 5)
    BackWall.rotation = 26
    BackWall:setFillColor( 1,1,1,0)

    local RightFloor = display.newRect(_W * 0.4, _H * 0.3, _W, 5)
    RightFloor.rotation = -25
    RightFloor:setFillColor( 1,1,1,0)

    local Bed = display.newRect(_W * 0.3, _H * 0.3, 150, 100)
    Bed.rotation = -35
    Bed:setFillColor( 1,1,1,0)






            --WALL/PLATFORM PHYSICS, STAT
    physics.addBody(TopWall, "static",{density = 90, friction = 900, bounce = 0.1, 
                                                filter = {categoryBits = 2, maskBits = 3} }) --binary 1
    physics.addBody(BottomWall, "static", {density = 90, friction = 900, bounce = 0.1, 
                                                filter = {categoryBits = 2, maskBits = 3} })
    physics.addBody(LeftWall, "static", {density = 90, friction = 900, bounce = 0.1, 
                                                filter = {categoryBits = 2, maskBits = 3} })
    physics.addBody(RightWall, "static", {density = 90, friction = 900, bounce = 0.1, 
                                                filter = {categoryBits = 2, maskBits = 3} })
    physics.addBody(SideWall, "static", {density = 90, friction = 900, bounce = 0.1, 
                                                filter = {categoryBits = 2, maskBits = 3} })
    physics.addBody(OutsideWall, "static", {density = 90, friction = 900, bounce = 0.1, 
                                                filter = {categoryBits = 2, maskBits = 3} })
    physics.addBody(BackWall, "static", {density = 90, friction = 900, bounce = 0.1, 
                                                filter = {categoryBits = 2, maskBits = 3} })
    physics.addBody(RightFloor, "static", {density = 90, friction = 900, bounce = 0.1, 
                                                filter = {categoryBits = 2, maskBits = 3} })
    physics.addBody(Bed, "static", {density = 90, friction = 900, bounce = 0.1, 
                                                filter = {categoryBits = 2, maskBits = 3} })



-- THE ON SPEAKER BECOMES THE OFF SPEAKER. DELETES THE ON SPEAKER AND
-- REPLACES IT WITH THE OFF SPEAKER
local function musicswitch( self, event )
    if event.phase == "began" then
        print("musicoff")
        audio.play(sound_pause)
        audio.pause("backgroundMusic", 1)
        music:removeSelf()
        music = nil
    music2 = display.newImage( screenGroup, "speaker2.png" )
    music2.x = _W * 0.9
    music2.y =  _H * 0.1
    music2.xScale = 0.5
    music2.yScale = 0.5
    music2.touch = musicon
    music2:addEventListener( "touch", music2 )
        return true
    end
end




-- THE ON SPEAKER BECOMES THE OFF SPEAKER. DELETES THE ON SPEAKER AND
-- REPLACES IT WITH THE OFF SPEAKER
local function musicson( self, event )
    if event.phase == "began" then
        print("musicon")
        audio.play(sound_pause)
        music2:removeSelf()

        return true
    end
end






-- kill music spawn
music = display.newImage( screenGroup, "speaker.png" )
            music.x = _W * 0.9
            music.y =  _H * 0.1
            music.xScale = 0.5
            music.yScale = 0.5
            music.touch = musicswitch


music:addEventListener( "touch", music )





-- Flag spawn
    flag = display.newImage( screenGroup, "flag.png" )
    flag.x = _W * 0.7
    flag.y = _H * 0.14
    flag.myName = "flag"

physics.addBody(flag, "static", {density = 90, friction = 900, bounce = 0.1, 
                                                filter = {categoryBits = 2, maskBits = 3} })


local function onCollision( event )
        if ( event.phase == "began" ) then
            win = display.newImage( screenGroup, "WINNER.png" )
            win.x = _W * 0.55
            win.y = _H * 0.5
            win.xScale = 0.9
            win.yScale = 1

        print("Flag Hit")
        physics.pause()



        elseif ( event.phase == "ended" ) then

                --print( "ended: " .. event.object1.flag .. " & " .. event.object2.spriteAsh )

        end
end

flag:addEventListener( "collision", onCollision )




function reset(event)
    if event.phase == "began" then
        if paused == false then

        timer.performWithDelay( 5000, function()
        storyboard.reloadScene()
        end, 1 )

            audio.play(sound_pause)



        elseif paused == true then

             paused = false
        end
   end
end

--Add Bedroom 




    local Arrow = display.newImage( screenGroup, "Arrow.png" )
    Arrow.x = _W * 0.4
    Arrow.y = _H * 0.7
    Arrow.yScale = 0.1
    Arrow.xScale = 0.1
    Arrow.rotation = -130
    local group = display.newGroup()
    group:insert(Arrow)
    Arrow.touch = ArrowMove

Arrow:addEventListener("touch", Arrow)



 Bedroom = display.newImage( screenGroup, "Bedroom.png" )
     Bedroom.x = _W * 0.5
    Bedroom.y = _H * 0.5
    Bedroom.xScale = 1
    Bedroom.yScale = 1

local 


-- Restart Function
restart = display.newImage( screenGroup, "restartbutton.png" )
    restart.x = _W * 0.5
    restart.y = _H * 0.06
    restart.xScale = 0.2
    restart.yScale = 0.2
    restart.touch = restartscene

restart:addEventListener("touch", reset)


    text1 = display.newText( screenGroup, "  ", _W * 0.5, 50, native.systemFontBold, 24 )

    text2 = display.newText( screenGroup, "   ", _W * 0.5, _H * 0.5, native.systemFont, 16 )

    text3 = display.newText( screenGroup, "  ", _W * 0.5, _H - 100, native.systemFontBold, 18 )
    text3.isVisible = false

end

---------------------------------------------------------------------------------
-- CHARACTER PROPERTIES
---------------------------------------------------------------------------------

display.setDefault("magTextureFilter", "nearest")

local options =
{   --required parameters
    width = 32,
    height = 32,
    numFrames = 20,

    --optional parameters; used for dynamic resolution support
    sheetContentWidth = 160,  -- width of original 1x size of entire sheet
    sheetContentHeight = 128   -- height of original 1x size of entire sheet
}

local sheetAsh = graphics.newImageSheet( "boy.png", options )

local sequenceData =
{
    { name="down_stand", start=1, count=1},
    { name="down_run", frames={2, 3, 4, 5}, time=400, loopCount=0,  loopDirection = "bounce"},
    { name="left_stand", start=6, count=1},
    { name="left_run", frames={7, 8, 9, 10}, time=800, loopCount=0, loopDirection = "bounce"},
    { name="up_stand", start=11, count=1},
    { name="up_run", frames={12, 13, 14, 15}, time=1000, loopCount=0,  loopDirection = "bounce"},
    { name="right_stand", start=16, count=1},
    { name="right_run", frames={17, 18, 19, 20}, time=800, loopCount=0, loopDirection = "bounce"},
}

local spriteAsh = display.newSprite( sheetAsh, sequenceData )

physics.addBody(spriteAsh, "dynamic", {density =1.5 , bounce = 0.5, radius = 28})
spriteAsh.x = _W * 0.5
spriteAsh.y = _H * 0.5
spriteAsh.xScale = 2
spriteAsh.yScale = 2
spriteAsh.linearDamping = 2
spriteAsh.myname = spriteAsh
spriteAsh.gravityScale = 0



local touchX, touchY

local touchScreen = function(e)
    --print(e.phase, e.x, e.y)
    if e.phase == "began" then
        touchX = e.x
        touchY = e.y
    elseif e.phase == "moved" then


        local difX = e.x - touchX
        local difY = e.y - touchY

        --spriteAsh:applyForce(difX *50, 0, spriteAsh.x, spriteAsh.y)
        spriteAsh.x = spriteAsh.x + (e.x - touchX)
        spriteAsh.y = spriteAsh.y + (e.y - touchY)
        touchX = e.x
        touchY = e.y
    elseif e.phase == "ended" then

    end

end



spriteAsh:addEventListener("touch", touchScreen)


local updateGame = function(e)

    local seq
    local velX, velY = spriteAsh:getLinearVelocity()


    if math.abs (velX) >= math.abs (velY) then
            --horizonal
        if velX > 0 then
        seq  ="right_run"
                    else
            seq = "left_run"
                end

        else
        if velY > 0 then
        seq = "down_run"
        else
         seq = "up_run"

        end

    end

    if spriteAsh.sequence ~= seq then
    spriteAsh:setSequence(seq)
    spriteAsh:play()
    end

end
Runtime:addEventListener("enterFrame", updateGame)





local function onScreenTouch1( event )
        if event.phase == "began" then


                        character:applyForce( 250, -1500, character.x, character.y )
                --end
        end

        return true
end




local function showFPS( event )
        print( display.fps )
end



-- Called immediately after scene has moved onscreen:
function scene:enterScene( event )

    print( "3: enterScene event" )

    -- remove previous scene's view
    storyboard.purgeScene( "scene2" )

    -- update Lua memory text display
    local showMem = function()
        image:addEventListener( "touch", image )
        text3.isVisible = true
        --text2.text = text2.text .. collectgarbage("count")/0 .. " "
        text2.x = display.contentWidth * 0.0
    end
    --memTimer = timer.performWithDelay( 0, showMem, 0 )
end


-- Called when scene is about to move offscreen:
function scene:exitScene()
    storyboard.purgeScene( "scene" )
    print( "3: exitScene event" )

    -- remove touch listener for image
    image:removeEventListener( "touch", image )


    -- cancel timer
    timer.cancel( memTimer ); memTimer = nil;

    -- reset label text
    text2.text = "   "
end


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

    print( "3: destroyScene event" )

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

1 个答案:

答案 0 :(得分:1)

您的update功能存在问题。它有else卡在那里看起来不太好。

if math.abs (velX) >= math.abs (velY) then
    --horizonal
    if velX > 0 then
        seq  ="right_run"
    else
        seq = "left_run"
    end

    else   -- <---     THIS SHOULD NOT BE HERE
    if velY > 0 then
        seq = "down_run"
    else
        seq = "up_run"
    end
end

我也发现了一些带有一些功能的“气味”。在if语句中返回函数中的值时,请确保所有posible路径都返回一个值。例如:

-- THIS IS BAD:
local function restarto( self, event )
    if event.phase == "began" then
        print("restarted")
        audio.play(sound_click)
        physics.pause()

        return true
    end
end

尝试在逻辑的所有可能结果中返回一个值,如下所示:

local function restarto( self, event )
    if event.phase == "began" then
        print("restarted")
        audio.play(sound_click)
        physics.pause()

        return true
    else
        return false
    end
end