【问题标题】:Can't Move Group backwards! Corona SDK不能向后移动组!电晕SDK
【发布时间】:2018-03-29 06:55:00
【问题描述】:

我是新来的,我正在尝试使用 corona sdk 为 android 做一个视频游戏,但我坚持在背景上创建随机粒子, 这是代码:

local menu = composer.newScene()

function menu:create( event )
  local bg = self.view
  local particlesGroup = self.view
  local sceneGroup = self.view


  blackLayer = display.newImageRect(  "black_layer.png",  1280, 300 )
  blackLayer.y = display.contentCenterY + 150
  blackLayer.x = display.contentCenterX
  blackLayer.alpha = 0.6



  background = display.newImageRect( "background.png",  1280 , 720 )
  background.x = display.contentCenterX
  background.y = display.contentCenterY




  playBtn = widget.newButton( {
    id="playBtn",
    x=display.contentCenterX+350,
    y=blackLayer.y,
    labelColor = { default={0.922, 0.938, 0.941}, over={0.9, 0.3, 0.234, 0.6}},
    label = "Play Now",
    defaultFile = "redBtn.png",
    overFile = "whiteBtn.png",
    width = 200,
    height = 200,
    fontSize = 30,
    font = "Fonts/Raleway-Bold.ttf",
    --onRelease = composer.gotoScene( "game")
  } )


  settingsBtn = widget.newButton( {
      id="playBtn",
      x=display.contentCenterX,
      y=blackLayer.y-50,
      font = "Fonts/Raleway-Bold.ttf",
      label = "Settings",
      defaultFile = "rectWhiteBtn.png",
      overFile = "rectRedBtn.png",
      width =250,
      height = 100,
      fontSize = 30,
      labelColor = { default={0.9, 0.3, 0.234} , over={0.922, 0.938, 0.941, 0.6} },
      onRelease = gotoSettings
    } )


  highScoresBtn = widget.newButton( {
        id="highScoresBtn",
        x=display.contentCenterX,
        y=settingsBtn.y + 100,
        font = "Fonts/Raleway-Bold.ttf",
        label = "High Scores",
        defaultFile = "rectWhiteBtn.png",
        overFile = "rectRedBtn.png",
        width =250,
        height = 100,
        fontSize = 30,
        labelColor = { default={0.9, 0.3, 0.234} , over={0.922, 0.938, 0.941, 0.6} },
        --onRelease = composer.gotoScene("highScores")
      } )

      bg:insert(background)
      sceneGroup:insert(blackLayer)
      sceneGroup:insert(playBtn)
      sceneGroup:insert(settingsBtn)
      sceneGroup:insert(highScoresBtn)

      function generateParticles()
        local xy=math.random(30,70)
        local newParticle = display.newImageRect( particlesGroup,  objectSheet , math.random(13), xy, xy )
        newParticle.x = (math.random(1280)*math.random(88932))%1280
        newParticle.y = (math.random(720)*math.random(13546))%720
        newParticle.alpha = 0
        transition.to( newParticle, {alpha=1, time=150, onComplete=dimParticle, onCompleteParams=newParticle})
      end

      function generateParticlesSmall()
        local xy=math.random(5,15)
        local newParticle = display.newImageRect( particlesGroup,  objectSheet , 1, xy, xy )
        newParticle.x = (math.random(1280)*math.random(88932))%1280
        newParticle.y = (math.random(720)*math.random(13546))%720
        newParticle.alpha = 0
        transition.to( newParticle, {alpha=1, time=150, onComplete=dimParticle, onCompleteParams=newParticle})
      end

end


function menu:show( event )
  local bg = self.view
  local particlesGroup = self.view
  local sceneGroup = self.view
  local phase = event.phase



    if ( phase == "will" ) then


      bigPTimer1=timer.performWithDelay( 1, generateParticles, 0 )
      -- Start the timer wich call 'generateParticles' for each ms


      smallPTimer=timer.performWithDelay( 1, generateParticlesSmall, 0 )
      -- Start the timer wich call 'generateParticlesSmall' for each ms
      particlesGroup:toBack()

    elseif ( phase == "did" ) then


        if event.phase=="began"then
        end
        if event.phase=="ended"then
        end


    end
end


function menu:hide( event )
  local bg = self.view
  local particlesGroup = self.view
  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

function menu:destroy( event )
  local bg = self.view
  local particlesGroup = self.view
  local sceneGroup = self.view
end

--Funzioni

local function gotoGame()
    composer.gotoScene( "game" )
end

local function gotoHighScores()
    composer.gotoScene( "highscores" )
end

function gotoSettings()
  reproduceEffet1()
  timer.cancel(bigPTimer1)
  timer.cancel(smallPTimer)
  composer.gotoScene( "settings", options)
end

--funzioni

local function removetotally( particle )
  display.remove( particle )
end

function dimParticle( particle )
    transition.to( particle, {alpha=0, time=500, onComplete = removetotally, onCompleteParams = particle})
end

--

menu:addEventListener( "create", menu )
menu:addEventListener( "show", menu )
menu:addEventListener( "hide", menu )
menu:addEventListener( "destroy", menu )


--

return menu

所有功能都运行良好,但问题是我无法将粒子组移回 bg 和 sceneGroup 之间。

感谢聆听。

【问题讨论】:

    标签: android lua coronasdk


    【解决方案1】:

    在您的代码中,您只有 1 个显示组。

    local bg = self.view
    local particlesGroup = self.view
    local sceneGroup = self.view
    

    您需要将particlesGroup 创建为子级才能将其移回:

    在文件顶部声明局部变量:

    local menu = composer.newScene()
    local bg, particlesGroup
    

    menu:create中创建群组

    bg = display.newGroup()
    particlesGroup = display.newGroup()
    local sceneGroup = self.view
    sceneGroup:insert(bg)
    sceneGroup:insert(particlesGroup) -- here particlesGroup will be on the top
    
    particlesGroup:toBack() --now particlesGroup is under bg
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-19
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 2013-04-12
      • 2015-04-24
      • 1970-01-01
      相关资源
      最近更新 更多