控制台上的输出两次,并且通道与玩家重叠

时间:2017-04-03 12:08:52

标签: lua corona

我应该得到的输出是当我点击右边时小鸡可以向右跳,反之亦然。但是我在这里解决的两个问题是当我点击它时会在控制台输出,就像我点击两次一样。第二个问题是车道是阵列并且保持重叠一切,甚至是作为玩家的小鸡。

为此我包括2个lua文件和3个png。

这是我的main.lua

display.setStatusBar( display.HiddenStatusBar )

local composer = require( "composer" )
print("entering gotoScene")
composer.gotoScene( "game2" )
print("out from gotoScene")

这是我的game2.lua

---REQUIRES
local composer = require( "composer" )
local scene = composer.newScene()
local widget = require( "widget" )
local physics = require "physics"
physics.start()
physics.setGravity(0,0)
local lanes = {}
local laneID = 1

scroll = 2

    ---SIZE PHONE DECLARATION

    local screenW = display.contentWidth --640
    local screenH = display.contentHeight --1136
    local halfX = display.contentWidth * 0.5 --half width 320
    local halfY = display.contentHeight * 0.5 --half height 568

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

---WHEN TAP CHICK MOVE
local function tapListener( event )
    local object = event.target
       if object.name == "Right Side" then
        print( object.name.." TAPPED!" )
            if laneID < 3 then
            laneID = laneID + 1;
            transition.to(chick, {x=lanes[laneID].x,time=50})
            print( "At lane "..laneID.." to the right")
            end
        return true
       end

       if object.name == "Left Side" then
        print( object.name.." TAPPED!" )
            if laneID > 1 then
            laneID = laneID - 1;
            transition.to(chick, {x=lanes[laneID].x,time=50})
            print( "At lane "..laneID.." to the left")
            end
        return true
        end
end
    ----------------------

---CREATE 
-- Initialize the scene here.
-- Example: add display objects to "sceneGroup", add touch listeners, etc.

function scene:create(  )
    local group = self.view

    ---TAP BACKGROUND
    RightSide = display.newRect(500,halfY, halfX+50, screenH + 100 )
    RightSide.alpha = 0.1
    RightSide.name = "Right Side"

    LeftSide = display.newRect(140, halfY,halfX+50, screenH + 100)
    LeftSide.alpha = 0.1
    LeftSide.name = "Left Side"
    ----------------------

    ---TAP LABEL
    rightLabel = display.newText({ text = "", x = 0, y = 0 , fontSize = 50 } )
    rightLabel:setTextColor( 0 ) ; rightLabel.x = 500 ;rightLabel.y = halfY

    leftLabel = display.newText({ text = "", x = 0, y = 0, fontSize = 50 } )
    leftLabel:setTextColor( 0 ) ; leftLabel.x = 150 ; leftLabel.y = halfY
    ----------------------

    ---PATHWAY (BACKGROUND)
    path1 = display.newImageRect("road.png",screenW,screenH)
    path1.x = halfX
    path1.y = halfY

    path2 = display.newImageRect("road.png",screenW,screenH)
    path2.x = halfX
    path2.y = halfY - screenH 
    ----------------------

    ---LANES
    for i=1,3 do -- loop 3 times to create 3 lanes for our game
        --myGroup=display.newGroup()
        laneimg = display.newImageRect("lanesroad.png", 150, 1300)
            lanes[i] = laneimg
            lanes[i].x = (display.contentCenterX - 140*2) + (i*140)
            lanes[i].y = display.contentCenterY
            lanes[i].id = i
    end
    ----------------------

    ---CHICK
    chick = display.newImageRect("chick.png",100,100)
    chick.anchorY = 1
    chick.x = lanes[2].x
    chick.y = 1000
    physics.addBody(chick) 
    chick.bodyType = "dynamic"

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



path1:toBack();
path2:toBack();
group:insert( RightSide )
group:insert( LeftSide )
group:insert( rightLabel )
group:insert( leftLabel )
group:insert( laneimg)
group:insert( chick )

end

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


---BACKGROUND SCROLL
function pathScroll (self,event)
    path1.y = path1.y + scroll
    path2.y = path2.y + scroll

    if path1.y == screenH * 1.5 then
        path1.y = screenH * -.5
    end

    if path2.y == screenH * 1.5 then
        path2.y = screenH * -.5
    end
end
----------------------


---SHOW --that will show in scene
function scene:show (event)

---FOR ROAD TO SCROLL
path1.enterFrame = pathScroll
Runtime:addEventListener("enterFrame", pathScroll)
path2.enterFrame = pathScroll
Runtime:addEventListener("enterFrame", pathScroll)
----------------------

---WHEN TAP TO RIGHT
RightSide:addEventListener( "tap", tapListener )
rightLabel.text = "right"
----------------------

---WHEN TAP TO LEFT
LeftSide:addEventListener( "tap", tapListener )
leftLabel.text = "left"
----------------------

end
----------------------

---HIDE 
function scene:hide (event)



end
----------------------

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


return scene

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

方法scene:show被调用两次。一旦event.phase参数等于will,第二次event.phase参数等于did。它也适用于scene.hide方法。下面的代码检查。

在botom中我放置了来自Corona documentation的场景模板。它可以用作新场景的起点。

我评论了Runtime:addEventListener("enterFrame", pathScroll)。你有两个,但一个就足够了。

您通过这些链接找到的更多信息

尝试(测试)

...

---SHOW --that will show in scene
function scene:show (event)
local phase = event.phase

if ( phase == 'will' ) then

elseif ( phase == 'did' )  then   
---FOR ROAD TO SCROLL
path1.enterFrame = pathScroll
Runtime:addEventListener("enterFrame", pathScroll)
path2.enterFrame = pathScroll
--Runtime:addEventListener("enterFrame", pathScroll)
----------------------

---WHEN TAP TO RIGHT
RightSide:addEventListener( "tap", tapListener )
rightLabel.text = "right"
----------------------

---WHEN TAP TO LEFT
--LeftSide:addEventListener( "tap", tapListener )
leftLabel.text = "left"

----------------------
end

end
----------------------

...

场景模板

local composer = require( "composer" )

local scene = composer.newScene()

-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------




-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------

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

    local sceneGroup = self.view
    -- Code here runs when the scene is first created but has not yet appeared on screen

end


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

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

    if ( phase == "will" ) then
        -- Code here runs when the scene is still off screen (but is about to come on screen)

    elseif ( phase == "did" ) then
        -- Code here runs when the scene is entirely on screen

    end
end


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

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

    if ( phase == "will" ) then
        -- Code here runs when the scene is on screen (but is about to go off screen)

    elseif ( phase == "did" ) then
        -- Code here runs immediately after the scene goes entirely off screen

    end
end


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

    local sceneGroup = self.view
    -- Code here runs prior to the removal of scene's view

end


-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------

return scene