控制台上的输出两次,通道与播放器重叠



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

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

这是我的主要.lua

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

这是我的游戏2.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

在此处输入图像描述

在此处输入图像描述

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

在 botom 我从 Corona 文档中放置了场景模板。它可以用作新场景的起点。

我用Runtime:addEventListener("enterFrame", pathScroll)注释掉了一行.你有两个,但一个就足够了。

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

  • 作曲家库

  • 作曲家 API 简介

  • 点击/触摸/多点触控

试用(已测试(

...
---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

最新更新