Corona SDK Composer无法正常工作,但也没有给我任何错误

时间:2015-10-01 19:34:15

标签: android lua corona corona-storyboard

我制作了这个小游戏,一切都运行良好,但我觉得是时候添加一个开始菜单,以使游戏更具代表性。我尝试使用Composer进行场景管理,到目前为止我还没有运气。我没有通过电晕终端收到任何错误,但我也没有在模拟器上看到任何内容,我只是得到一个黑屏。

这是代码

local composer = require( "composer" )

local scene = composer.newScene()

-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called.
-- -----------------------------------------------------------------------------------------------------------------

-- local forward references should go here

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


-- "scene:create()"
function scene:create( event )

    local sceneGroup = self.view
    -- Initialize the scene here.
    -- Example: add display objects to "sceneGroup", add touch listeners, etc.
    --Image Assets
stoneGroup = display.newGroup()
    local bg = display.newImageRect("images/GameBackground.png",display.viewableContentHeight*2,display.viewableContentWidth*2)
    bg.x= display.viewableContentWidth/2
    bg.y = display.viewableContentHeight/2
    bg.anchorX = 0.5
    bg.anchorY = 0.5
    physics = require("physics")
    physics.start()
    physics.setDrawMode( "normal" )
    sceneGroup:insert(bg)

    local bg2 = display.newImageRect("images/GameBackground.png",display.viewableContentHeight*2,display.viewableContentWidth*2)
    bg2.x= display.viewableContentWidth*4
    bg2.y = display.viewableContentHeight/2
    bg2.anchorX = 0.5
    bg2.anchorY = 0.5
    sceneGroup:insert(bg2)

    local ground = display.newRect(display.viewableContentWidth/2,display.viewableContentHeight-84,display.viewableContentWidth*3.5, 65)
    ground.anchorX = 0.5
    ground.anchorY = 0.5
    ground:setFillColor(0,0,0)
    physics.addBody( ground, "static", { density=3.0, friction=0.5, bounce=0.1} )
    sceneGroup:insert(ground)

    local ghost = display.newImageRect("images/Ghost.png", 83,55)
    ghost.y = display.viewableContentHeight-170
    physics.addBody( ghost, "dynamic", { density=3, friction=5.5, bounce=.3 } )
    ghost.isFixedRotation = true
    sceneGroup:insert(ghost)

    local stoned = {"images/Bones_Gravestone_nograss.png", "images/Skull_Gravestone_nograss.png", "images/Web_Gravestone_nograss.png"}

--Stones
        local stone1 = display.newImageRect(stoned[math.random(1,3)], 50,50)
        stone1.x = display.viewableContentWidth * 2.2
        stone1.anchorX = 0.5
        stone1.anchorY = 0.5
        stone1.y = (ground.y - stone1.height-3)
        sceneGroup:insert(stone1)

        local stone2 = display.newImageRect(stoned[math.random(1,3)], 50,50)
        stone2.x = display.viewableContentWidth * 3.2
        stone2.anchorX = 0.5
        stone2.anchorY = 0.5
        stone2.y = (ground.y - stone2.height-3)
        sceneGroup:insert(stone2)

        local stone3 = display.newImageRect(stoned[math.random(1,3)], 50,50)
        stone3.x = display.viewableContentWidth * 5.2
        stone3.anchorX = 0.5
        stone3.anchorY = 0.5
        stone3.y = (ground.y - stone2.height-3)
        sceneGroup:insert(stone3)

        local stone4 = display.newImageRect(stoned[math.random(1,3)], 50,50)
        stone4.x = display.viewableContentWidth * 7.2
        stone4.anchorX = 0.5
        stone4.anchorY = 0.5
        stone4.y = (ground.y - stone2.height-3)
        sceneGroup:insert(stone4)

        local stone5 = display.newImageRect(stoned[math.random(1,3)], 50,50)
        stone5.x = display.viewableContentWidth * 8.5
        stone5.anchorX = 0.5
        stone5.anchorY = 0.5
        stone5.y = (ground.y - stone2.height-3)
        sceneGroup:insert(stone5)

--Grass
    local grass = display.newImageRect("images/Grass.png", display.viewableContentWidth*5, 200)
    grass.anchorX = .5
    grass.y = (display.viewableContentHeight - 171)
    sceneGroup:insert(grass)

--Wall Left
    local leftWall = display.newRect(display.viewableContentWidth-display.viewableContentWidth*2.13, 200,100,display.viewableContentHeight)
    physics.addBody(leftWall,"kinematic", { density=10, friction=10, bounce=5 } )
    leftWall.alpha=0
    sceneGroup:insert(leftWall)

    local rightWall = display.newRect(display.viewableContentWidth*2.13, 200,100,display.viewableContentHeight)
    physics.addBody(rightWall,"kinematic", { density=10, friction=10, bounce=5 } )
    rightWall.alpha=0
    sceneGroup:insert(rightWall)

--Coin
    local coin = display.newImageRect("images/Coin.png", 45, 45)
    coin.x = display.viewableContentWidth*2.5
    coin.y = 350
    physics.addBody( coin, "kinematic", { density=0, friction=0, bounce=0 } )
    sceneGroup:insert(coin)

    local coin2 = display.newImageRect("images/Coin.png", 45, 45)
    coin2.x = coin.x*2.5
    coin2.y = 350
    physics.addBody( coin2, "kinematic", { density=0, friction=0, bounce=0 } )
    sceneGroup:insert(coin2)

    --Score
    scoreNum = tonumber(0)
    local score = display.newText(scoreNum, 0, 0, native.systemFont, 32)
    score.anchorX = .5
    score.anchorY = .5
    score.x = display.viewableContentWidth*1.92
    score.y = 50
    sceneGroup:insert(score)
    --scoreIcon
    local scoreIcon = display.newImageRect("images/Coin.png", 45, 45)
    scoreIcon.y = score.y-2
    scoreIcon.x = score.x - 50
    sceneGroup:insert(scoreIcon)

    -- Listener Functions
--Scrolls bg
function scrollBG(self, event)
    if self.x < -(display.viewableContentWidth*3) then
        self.x = display.viewableContentWidth*4
    else
        self.x = self.x - 3
    end
end

function scrollBG2(self, event)
    if self.x < -(display.viewableContentWidth*3) then
        self.x = display.viewableContentWidth*4
    else
        self.x = self.x - 3
    end
end
-- Jump Function
function jump(event)
    if event.phase == "began" and ghost.y >=  (display.viewableContentHeight-200) then
        ghost:applyForce(200, -4500, ghost.x, ghost.y)
    end
    return true
end

--Scroll Tomb Stone
function scrollStone(self, event)
    if self.x < -(display.viewableContentWidth*3) then
        self.x = display.viewableContentWidth*math.random(2,5)
    else
        self.x = self.x - 5
    end
end

--Scroll Coin
function scrollCoin(self, event)
    if self.x < -(display.viewableContentWidth*3) then
        self.x = display.viewableContentWidth*math.random(2,5)
        self.y = math.random(300, 400)
        physics.addBody(coin, "kinematic", { density=0, friction=0, bounce=0 })
        transition.to(coin, {time=300, alpha=100, width=45, height=45})

    else
        self.x = self.x - 5
    end
end

--Scroll Coin
function scrollCoin2(self, event)
    if self.x < -(display.viewableContentWidth*3) then
        self.x = display.viewableContentWidth*math.random(2,5)
        self.y = math.random(300, 400)
        physics.addBody(coin2,"kinematic", { density=0, friction=0, bounce=0 })
        transition.to(coin2, {time=300, alpha=100, width=45, height=45})

    else
        self.x = self.x - 5
    end
end
    local function removeTheBody()
        physics.removeBody(coin)
    end

    local function removeTheBody2()
        physics.removeBody(coin2)
    end
--Coin Collision
local function onCoinCollision( self, event )
    transition.to(coin, {time=300, alpha=0, width=10, height=30})
    scoreNum = scoreNum+1
    score.text = scoreNum
    timer.performWithDelay( 1, removeTheBody )

end

local function onCoin2Collision( self, event )
    transition.to(coin2, {time=300, alpha=0, width=10, height=30})
    scoreNum = scoreNum+1
    score.text = scoreNum
    timer.performWithDelay( 1, removeTheBody2 )
end





--Stone Physics
physics.addBody( stone1, "static", { density=3, friction=5.5, bounce=.3 } )
physics.addBody( stone2, "static", { density=3, friction=5.5, bounce=.3 } )
physics.addBody( stone3, "static", { density=3, friction=5.5, bounce=.3 } )
physics.addBody( stone4, "static", { density=3, friction=5.5, bounce=.3 } )
physics.addBody( stone5, "static", { density=3, friction=5.5, bounce=.3 } )

end


-- "scene:show()"
function scene:show( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        -- Called when the scene is still off screen (but is about to come on screen).
    elseif ( phase == "did" ) then
        --Tomb Stoned
--Event Listeners
bg.enterFrame=scrollBG
Runtime:addEventListener("enterFrame", bg)
bg2.enterFrame=scrollBG2
Runtime:addEventListener("enterFrame", bg2)

stone1.enterFrame=scrollStone
Runtime:addEventListener("enterFrame", stone1)

stone2.enterFrame=scrollStone
Runtime:addEventListener("enterFrame", stone2)

stone3.enterFrame=scrollStone
Runtime:addEventListener("enterFrame", stone3)

stone4.enterFrame=scrollStone
Runtime:addEventListener("enterFrame", stone4)

stone5.enterFrame=scrollStone
Runtime:addEventListener("enterFrame", stone5)

--Ghost Listener
Runtime:addEventListener("touch", jump)

--Coin Move
coin.enterFrame=scrollCoin
Runtime:addEventListener("enterFrame", coin)

coin.collision=onCoinCollision
coin:addEventListener("collision", coin)

--Coin Move
coin2.enterFrame=scrollCoin2
Runtime:addEventListener("enterFrame", coin2)

coin2.collision=onCoin2Collision
coin2:addEventListener("collision", coin2)
        -- Called when the scene is now on screen.
        -- Insert code here to make the scene come alive.
        -- Example: start timers, begin animation, play audio, etc.
    end
end


-- "scene:hide()"
function scene:hide( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        -- Called when the scene is on screen (but is about to go off screen).
        -- Insert code here to "pause" the scene.
        -- Example: stop timers, stop animation, stop audio, etc.
    elseif ( phase == "did" ) then
        -- Called immediately after scene goes off screen.
    end
end


-- "scene:destroy()"
function scene:destroy( event )

    local sceneGroup = self.view

    -- Called prior to the removal of scene's view ("sceneGroup").
    -- Insert code here to clean up the scene.
    -- Example: remove display objects, save state, etc.
end


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

-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

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

return scene

1 个答案:

答案 0 :(得分:0)

尝试在场景中打印一些东西:这个lua文件的create().. 例如:打印(“测试”)..并检查终端以查看是否打印..