Multiscene Applicatione

Il mio piccolo gioco prevede che si ripeta per alcune scene

cambiando solo l’aspetto grafico,

allora ho utilizzato Multiscene Applicatione.

Ho letto il tutorial sulle Storyboard

è ho cercato di dividere il mio gioco nelle parti giuste

ocal sceneName = ... local composer = require( "composer" ) -- Load scene with same root filename as this file local scene = composer.newScene( sceneName ) --------------------------------------------------------------------------------- local nextSceneButton function scene:create( event ) local sceneGroup = self.view local centerX = display.contentCenterX local centerY = display.contentCenterY local \_W = display.contentWidth local \_H = display.contentHeight local bkg = display.newImage ("nuovo\_prato.png", centerX, centerY,true) score = 0 cont = 0 local scoreText = display.newText("Score:", display.contentCenterX + 150, 20, native.systemFontBold, 30) scoreText:setFillColor( 1, 0, 0 ) local scoreNumber = display.newText(score, display.contentCenterX + 210, 20, native.systemFontBold, 30) scoreNumber:setFillColor( 1, 0, 0 ) -- "CESTINI" -- local baskets = {} local basket = display.newCircle(80, 162, 14) --rosso basket.fillColor = {1, 0, 0} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(300, 162, 14) --bianco basket.fillColor = {1, 1, 1} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(190, 192, 14) --giallo basket.fillColor = {1, 1, 0} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(410, 192, 14) --viola basket.fillColor = {1, 0, 1} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local function updateScore() score = score + 1 scoreNumber.text = score end local function createsmile() smile = display.newImage ("smile.png", basket.x, basket.y+35,true) smile:scale( 0.5, 0.5 ) smile.alpha = 0.0 end local function smileHide() --print("immagine nascosta!") end local printCerchi end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen local title = self:getObjectByTag( "Title" ) title.x = display.contentWidth / 2 title.y = display.contentHeight / 2 title.size = display.contentWidth / 10 local goToScene2Btn = self:getObjectByTag( "GoToScene2Btn" ) goToScene2Btn.x = display.contentWidth - 95 goToScene2Btn.y = display.contentHeight - 35 local goToScene2Text = self:getObjectByTag( "GoToScene2Text" ) goToScene2Text.x = display.contentWidth - 92 goToScene2Text.y = display.contentHeight - 35 elseif phase == "did" then local function checkCollision(event) if event.target then local rect = event.target for i = 1, #baskets do local basket = baskets[i] local basketColor = basket.fillColor local rectColor = rect.fillColor local sameColor = basketColor[1] == rectColor[1] and basketColor[2] == rectColor[2] and basketColor[3] == rectColor[3] local distance = math.sqrt((basket.x - rect.x)^2 + (basket.y - rect.y)^2) if distance \< 20 and sameColor then local newX, newY = rect.x, rect.y display.remove(rect) updateScore() smile.x = newX smile.y = newY smile.alpha = 1.0 transition.to( smile, { time=400, alpha=0.0, delay=400, onComplete=smileHide } ) if(cont == 7) then cont = 0 printCerchi() end end end end end local smile createsmile() local function onTouch( event ) local t = event.target checkCollision(event) local phase = event.phase if "began" == phase then local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then t.x = event.x - t.x0 t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end return true end function printCerchi() local cerchi = { { x=100, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=30, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=170, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=240, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=310, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=380, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=450, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, } for \_,item in ipairs( cerchi ) do local button = display.newCircle(item.x, item.y, item.r ) button.fillColor = {item.red, item.green, item.blue} button:setFillColor(unpack(button.fillColor)) button.strokeWidth = 3 button:setStrokeColor(0) button:addEventListener( "touch", onTouch ) end end printCerchi() nextSceneButton = self:getObjectByTag( "GoToScene2Btn" ) if nextSceneButton then -- touch listener for the button function nextSceneButton:touch ( event ) local phase = event.phase if "ended" == phase then composer.gotoScene( "scene2", { effect = "fade", time = 300 } ) end end -- add the touch event listener to the button nextSceneButton:addEventListener( "touch", nextSceneButton ) end end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen if nextSceneButton then nextSceneButton:removeEventListener( "touch", nextSceneButton ) end end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

ma mi da errore alla global createsmile(),

dove sbaglio?

Per “pulire” tutto e alleggerire la memoria cosa va

nella function scene:destroy( event )?

ancora grazie per l’aiuto

Ciao,

local function createsmile()

è creata all’interno di “scene:create” e può essere richiamata solo dal suo interno, per questo va in errore.

Devi spostare la dichiarazione in un punto “visibile” per essere richiamato.

Nella scene:destroy puoi rimuovere quello che non ti serve più.

Es:

&nbsp; obj:removeSelf() obj = nil &nbsp;

Ciao,

Ale

Ciao Ale,

l’errore è sempre su

createsmile()

ho spastato createsmile() in altri  posti del codice ma l’errore non cambia,

ti inserisco il codice

--------------------------------------------------------------------------------- -- -- scene.lua -- --------------------------------------------------------------------------------- local sceneName = ... local composer = require( "composer" ) -- Load scene with same root filename as this file local scene = composer.newScene( sceneName ) --------------------------------------------------------------------------------- local nextSceneButton function scene:create( event ) local sceneGroup = self.view local centerX = display.contentCenterX local centerY = display.contentCenterY local \_W = display.contentWidth local \_H = display.contentHeight local bkg = display.newImage ("nuovo\_prato.png", centerX, centerY,true) score = 0 cont = 0 local scoreText = display.newText("Score:", display.contentCenterX + 150, 20, native.systemFontBold, 30) scoreText:setFillColor( 1, 0, 0 ) local scoreNumber = display.newText(score, display.contentCenterX + 210, 20, native.systemFontBold, 30) scoreNumber:setFillColor( 1, 0, 0 ) -- "CESTINI" -- local baskets = {} local basket = display.newCircle(80, 162, 14) --rosso basket.fillColor = {1, 0, 0} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(300, 162, 14) --bianco basket.fillColor = {1, 1, 1} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(190, 192, 14) --giallo basket.fillColor = {1, 1, 0} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(410, 192, 14) --viola basket.fillColor = {1, 0, 1} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local function updateScore() score = score + 1 scoreNumber.text = score end local function createsmile() smile = display.newImage ("smile.png", basket.x, basket.y+35,true) smile:scale( 0.5, 0.5 ) smile.alpha = 0.0 end local function smileHide() --print("immagine nascosta!") end local printCerchi end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen local title = self:getObjectByTag( "Title" ) title.x = display.contentWidth / 2 title.y = display.contentHeight / 2 title.size = display.contentWidth / 10 local goToScene2Btn = self:getObjectByTag( "GoToScene2Btn" ) goToScene2Btn.x = display.contentWidth - 95 goToScene2Btn.y = display.contentHeight - 35 local goToScene2Text = self:getObjectByTag( "GoToScene2Text" ) goToScene2Text.x = display.contentWidth - 92 goToScene2Text.y = display.contentHeight - 35 elseif phase == "did" then local function checkCollision(event) if event.target then local rect = event.target for i = 1, #baskets do local basket = baskets[i] local basketColor = basket.fillColor local rectColor = rect.fillColor local sameColor = basketColor[1] == rectColor[1] and basketColor[2] == rectColor[2] and basketColor[3] == rectColor[3] local distance = math.sqrt((basket.x - rect.x)^2 + (basket.y - rect.y)^2) if distance \< 20 and sameColor then local newX, newY = rect.x, rect.y display.remove(rect) updateScore() smile.x = newX smile.y = newY smile.alpha = 1.0 transition.to( smile, { time=400, alpha=0.0, delay=400, onComplete=smileHide } ) if(cont == 7) then cont = 0 printCerchi() end end end end end local smile createsmile() local function onTouch( event ) local t = event.target checkCollision(event) local phase = event.phase if "began" == phase then local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then t.x = event.x - t.x0 t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end return true end function printCerchi() local cerchi = { { x=100, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=30, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=170, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=240, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=310, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=380, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=450, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, } for \_,item in ipairs( cerchi ) do local button = display.newCircle(item.x, item.y, item.r ) button.fillColor = {item.red, item.green, item.blue} button:setFillColor(unpack(button.fillColor)) button.strokeWidth = 3 button:setStrokeColor(0) button:addEventListener( "touch", onTouch ) end end printCerchi() nextSceneButton = self:getObjectByTag( "GoToScene2Btn" ) if nextSceneButton then -- touch listener for the button function nextSceneButton:touch ( event ) local phase = event.phase if "ended" == phase then composer.gotoScene( "scene2", { effect = "fade", time = 300 } ) end end -- add the touch event listener to the button nextSceneButton:addEventListener( "touch", nextSceneButton ) end end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen if nextSceneButton then nextSceneButton:removeEventListener( "touch", nextSceneButton ) end end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

mi puoi aiutare a capire come mai passando

da un progetto normale ad uno Multiscene

createsmile() mi dà errore?

grazie

Ciao,

local function createsmile()

è creata all’interno di “scene:create” e può essere richiamata solo dal suo interno, per questo va in errore.

Devi spostare la dichiarazione in un punto “visibile” per essere richiamato.

Nella scene:destroy puoi rimuovere quello che non ti serve più.

Es:

&nbsp; obj:removeSelf() obj = nil &nbsp;

Ciao,

Ale

Ciao Ale,

l’errore è sempre su

createsmile()

ho spastato createsmile() in altri  posti del codice ma l’errore non cambia,

ti inserisco il codice

--------------------------------------------------------------------------------- -- -- scene.lua -- --------------------------------------------------------------------------------- local sceneName = ... local composer = require( "composer" ) -- Load scene with same root filename as this file local scene = composer.newScene( sceneName ) --------------------------------------------------------------------------------- local nextSceneButton function scene:create( event ) local sceneGroup = self.view local centerX = display.contentCenterX local centerY = display.contentCenterY local \_W = display.contentWidth local \_H = display.contentHeight local bkg = display.newImage ("nuovo\_prato.png", centerX, centerY,true) score = 0 cont = 0 local scoreText = display.newText("Score:", display.contentCenterX + 150, 20, native.systemFontBold, 30) scoreText:setFillColor( 1, 0, 0 ) local scoreNumber = display.newText(score, display.contentCenterX + 210, 20, native.systemFontBold, 30) scoreNumber:setFillColor( 1, 0, 0 ) -- "CESTINI" -- local baskets = {} local basket = display.newCircle(80, 162, 14) --rosso basket.fillColor = {1, 0, 0} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(300, 162, 14) --bianco basket.fillColor = {1, 1, 1} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(190, 192, 14) --giallo basket.fillColor = {1, 1, 0} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local basket = display.newCircle(410, 192, 14) --viola basket.fillColor = {1, 0, 1} basket:setFillColor(unpack(basket.fillColor)) table.insert(baskets, basket) local function updateScore() score = score + 1 scoreNumber.text = score end local function createsmile() smile = display.newImage ("smile.png", basket.x, basket.y+35,true) smile:scale( 0.5, 0.5 ) smile.alpha = 0.0 end local function smileHide() --print("immagine nascosta!") end local printCerchi end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen local title = self:getObjectByTag( "Title" ) title.x = display.contentWidth / 2 title.y = display.contentHeight / 2 title.size = display.contentWidth / 10 local goToScene2Btn = self:getObjectByTag( "GoToScene2Btn" ) goToScene2Btn.x = display.contentWidth - 95 goToScene2Btn.y = display.contentHeight - 35 local goToScene2Text = self:getObjectByTag( "GoToScene2Text" ) goToScene2Text.x = display.contentWidth - 92 goToScene2Text.y = display.contentHeight - 35 elseif phase == "did" then local function checkCollision(event) if event.target then local rect = event.target for i = 1, #baskets do local basket = baskets[i] local basketColor = basket.fillColor local rectColor = rect.fillColor local sameColor = basketColor[1] == rectColor[1] and basketColor[2] == rectColor[2] and basketColor[3] == rectColor[3] local distance = math.sqrt((basket.x - rect.x)^2 + (basket.y - rect.y)^2) if distance \< 20 and sameColor then local newX, newY = rect.x, rect.y display.remove(rect) updateScore() smile.x = newX smile.y = newY smile.alpha = 1.0 transition.to( smile, { time=400, alpha=0.0, delay=400, onComplete=smileHide } ) if(cont == 7) then cont = 0 printCerchi() end end end end end local smile createsmile() local function onTouch( event ) local t = event.target checkCollision(event) local phase = event.phase if "began" == phase then local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t ) t.isFocus = true t.x0 = event.x - t.x t.y0 = event.y - t.y elseif t.isFocus then if "moved" == phase then t.x = event.x - t.x0 t.y = event.y - t.y0 elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( nil ) t.isFocus = false end end return true end function printCerchi() local cerchi = { { x=100, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=30, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=170, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=240, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=310, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=380, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, { x=450, y=300, w=50, h=50, r=20, red=1, green=math.random(0,1), blue=math.random(0,1) }, } for \_,item in ipairs( cerchi ) do local button = display.newCircle(item.x, item.y, item.r ) button.fillColor = {item.red, item.green, item.blue} button:setFillColor(unpack(button.fillColor)) button.strokeWidth = 3 button:setStrokeColor(0) button:addEventListener( "touch", onTouch ) end end printCerchi() nextSceneButton = self:getObjectByTag( "GoToScene2Btn" ) if nextSceneButton then -- touch listener for the button function nextSceneButton:touch ( event ) local phase = event.phase if "ended" == phase then composer.gotoScene( "scene2", { effect = "fade", time = 300 } ) end end -- add the touch event listener to the button nextSceneButton:addEventListener( "touch", nextSceneButton ) end end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen if nextSceneButton then nextSceneButton:removeEventListener( "touch", nextSceneButton ) end end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

mi puoi aiutare a capire come mai passando

da un progetto normale ad uno Multiscene

createsmile() mi dà errore?

grazie