Я создал эту простую игру, состоящую из шара, движущегося в движущийся монетодержатель благодаря силе гравитации.Проблема касается контейнера, который должен перемещаться вправо и влево случайным образом благодаря функции под названием « moveContainer », но он остается неподвижным.Контейнер состоит из 3 прямоугольников с именами bottomSide, leftSide и rightSide, созданных в событии scene: create .
Как вы можете видеть, я вызываю эту функцию внутри события scene:покажите но контейнер не двигается.Игра отлично работает без использования сцен, поэтому я думаю, что проблема связана с реализацией сцены.Не могли бы вы проверить мой код и сказать, что я делаю не так?Это будет много значить для меня.Не стесняйтесь попробовать мой код в вашем редакторе.Спасибо!
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