我是新来的,我试图用电晕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之间移回粒子组。
感谢收听我。
答案 0 :(得分:0)
在您的代码中,您只有一个显示组。
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