【问题标题】:Corona function position inside events scene事件场景内的电晕功能位置
【发布时间】:2018-11-27 22:11:46
【问题描述】:

我创建了这个简单的游戏,其中一个球在重力作用下进入一个移动的硬币容器。问题在于容器应该随机左右移动,这要归功于名为 "moveContainer" 的函数,但它仍然保持不动。容器由 3 个矩形组成,称为 bottomSide、leftSide 和 rightSide,在事件 scene:create 中创建。

如您所见,我在事件 scene:show 中调用了该函数,但容器没有移动。游戏在不使用场景的情况下完美运行,所以我猜问题与场景实现有关。 你能检查我的代码并告诉我我做错了什么吗?这对我来说意义重大。随意在您的编辑器中尝试我的代码。谢谢!

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()"
-- -----------------------------------------------------------------------------------


local physics = require( "physics" )
physics.start()
physics.setGravity(0, 5 )
physics.setDrawMode( "normal" )


local left_side_piece
local center_piece
local right_side_piece

local obstacle
local rotation

local bottomSide
local leftSide
local rightSide

local lineCollision

local speed
local distance

local ball

--local fg
local fg = display.newGroup()


local line
local x
local y


local function moveContainer()
  speed = math.round(math.random(1700,2000))
  distance = math.round(math.random(50,display.contentWidth-50))
  transition.to(bottomSide,{time = speed, x =  distance, onComplete = moveBall})
    transition.to(leftSide,{time = speed, x =  distance-25})
    transition.to(rightSide,{time = speed, x =  distance+25})
    transition.to(lineCollision,{time = speed, x =  distance})
end




function draw(event)
    if event.phase=="began" then
        x = event.x
        y = event.y



    elseif event.phase=="moved" then
             line = display.newLine(fg, x,y,event.x,event.y)
             physics.addBody( line, "static", { radius=4 })
                 line.strokeWidth = 2
                 x = event.x
               y = event.y

    elseif event.phase=="ended" then
          physics.start( )
      timer.performWithDelay( 3000, bodyAwake,6 )


  end
end



local function bodyAwake()

  if(ball.isAwake== false)then
    local myText = display.newText( "RITENTA", 120, 30, native.systemFont, 16 )
    myText:setFillColor( 1, 0, 0 )
    setWorldChange = true
    transition.cancel()
    physics.pause()
  end
end



local function onLocalCollision( self, event )

    if ( event.phase == "began" ) then

            if ( self.myName == "sensore" and event.other.myName== "ball1" ) then
                local myText = display.newText( "PALLA ENTRATA", 120, 30, native.systemFont, 16 )
                myText:setFillColor( 1, 0, 0 )
        setWorldChange = true
                transition.cancel()


      elseif ( self.myName == "fondale" and event.other.myName== "ball1" ) then
                local myText = display.newText( "RITENTA", 120, 30, native.systemFont, 16 )
                myText:setFillColor( 1, 0, 0 )
        setWorldChange = true
                transition.cancel()
        physics.pause()

      end

    end
end



local function removeLine()
    if (setWorldChange == true) then
            for i = fg.numChildren, 1, -1 do
                 fg[i]:removeSelf()
                 fg[1] = nil
                 end
    setWorldChange = false
    end
end



local function updateBall()
  leftSide.x = bottomSide.x-25
    rightSide.x = bottomSide.x+25
    lineCollision.x = bottomSide.x

end
-- -----------------------------------------------------------------------------------
-- 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
    physics.pause()

--  local fg = display.newGroup()
    sceneGroup:insert( fg )

    local left_side_piece = display.newRect( 0-5, display.contentHeight/2, 660, 10 )
    physics.addBody( left_side_piece, "static" )
    left_side_piece.rotation = 90

    local center_piece = display.newRect( display.contentCenterX, display.contentHeight+160, 400, 120 )
    physics.addBody( center_piece, "static" )
    center_piece.myName="fondale"    --se la pallina collide con fondo, si va a pagina di replay

    local right_side_piece = display.newRect( display.contentWidth+5, display.contentHeight/2, 660, 10 )
    physics.addBody( right_side_piece, "static" )
    right_side_piece.rotation = -90


    local obstacle = display.newRect( 150,300,50,50 )
    obstacle:setFillColor( 222.0/255.0, 184.0/255.0, 135.0/255.0 )
    physics.addBody( obstacle, "static" )
    obstacle.gravityScale = 0
    local rotation = transition.to(obstacle, {time=3000, rotation=-360, iterations=-1} )


    local bottomSide = display.newRect( 160, display.contentHeight, 50, 3 )
    physics.addBody( bottomSide, "static",{ density=1.0, friction=0.4, bounce=0.2} )
    local leftSide = display.newRect( 134, display.contentHeight-24, 50, 3 )
    physics.addBody( leftSide, "static" )
    leftSide.rotation = -90
    local rightSide = display.newRect( 185, display.contentHeight-24, 50, 3 )
    physics.addBody( rightSide, "static" )
    rightSide.rotation = -90



    local lineCollision = display.newRect( 160, display.contentHeight-10, 35, 2 )
    physics.addBody( lineCollision, "static", {isSensor=true} )
    lineCollision.myName = "sensore"

    local ball = display.newCircle( display.contentWidth/2, 0, 8 )
    ball:setFillColor( 1,0,0 )
    physics.addBody( ball, "dynamic", { density=2.0, friction=0.6, bounce=0, radius=8 } )
    ball.myName= "ball1"

  lineCollision.collision = onLocalCollision
  lineCollision:addEventListener( "collision" )

  center_piece.collision = onLocalCollision
  center_piece:addEventListener( "collision" )



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
    physics.pause()
    moveContainer()


        Runtime:addEventListener("touch",draw)
    Runtime:addEventListener( "enterFrame", removeLine)
    --Runtime:addEventListener("enterFrame", updateBall)




    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
        physics.pause()

    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

【问题讨论】:

    标签: function coronasdk scene


    【解决方案1】:

    scene:createfunction 中更改bottomSideleftSiderightSide 的声明

    -- I have removed local keyword
    bottomSide = display.newRect( 160, display.contentHeight, 50, 3 )
    ...
    leftSide = display.newRect( 134, display.contentHeight-24, 50, 3 )
    ...
    rightSide = display.newRect( 185, display.contentHeight-24, 50, 3 )
    

    否则,您将创建具有相同名称的不同变量。

    Lua - Two local variables with the same name

    祝你有美好的一天:)

    【讨论】:

    • 你救了我!最后一个问题,我完成了:在函数 draw() 的“结束”阶段,我设置了一个 timer.performWithDelay(),它应该每 3 秒调用一次函数 bodyAwake(),但它不起作用。有什么提示可以解决这个问题吗?
    • 我不确定,但将bodyAwake 函数的主体移到draw 函数上方应该可以解决问题。请参阅Forward define a function in Lua?Lua: How to call a function prior to it being defined? 了解更多信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多