Power Up sceneGroup:insert problem

Hello all, i’m still a very noob programmer and thats why i have such silly question.

My game is a classical breakout but i’m running on a problem with respaw, i simple cant insert it on scenegroup from composer as a consequense i cant destroy it when change scenes. i will send a code and explain what is happening.

function \_pwup.createPowerUp(px,py,sceneGroup) local poweruptype = {} poweruptype[1] = 'shield' poweruptype[2] = 'magicmissiles' poweruptype[3] = 'fireball' poweruptype[4] = 'bolt' poweruptype[5] = 'wall' local rnd = math.random(1,#poweruptype) local powerup = display.newImage("images/powerups/"..poweruptype[rnd]..".png",px + math.random(-64,64) ,py + math.random(-32,32)) powerup.name = 'powerup' powerup.type = poweruptype[rnd]--'shield' powerup.level = composer.getSceneName('current') physics.addBody(powerup, "dynamic")--,{density = 1, friction = 0, bounce = 0}) powerup.isSensor = true powerup:setLinearVelocity(0,300) local function contact(self,event,sceneGroup) --self:removeSelf() if event.other.name == 'paddle' then print('angle:'..event.other.angle..", px: "..event.other.px) --- filtrando que powerup foi pego if self.type == 'shield' then print("force field") local shield = {} timer.performWithDelay( 5, function() shield = \_pwup.createShield(sceneGroup) end) elseif self.type == 'magicmissiles' then timer.performWithDelay( 5, function() shield = \_pwup.createMagicMissiles(event,sceneGroup) end) end end end powerup.collision = contact powerup:addEventListener("collision") sceneGroup:insert(powerup) end

that function is called when we break a brick, in a similar listener of collision. The main problem is that its showing an error when try to insert to sceneGroup the powerup.

I already used print, to see if the fields of scenegroup and the powerup are nil but all of then return a table.

Someone have any idea how to solve this? i whould appreciate alot any help. 

Try

function \_pwup.createPowerUp(px,py) ...   local sceneName = composer.getSceneName( 'current' )   local currentScene = composer.getScene( sceneName  )    local powerup = display.newImage( currentScene.view, "images/powerups/"..poweruptype[rnd]..".png",px + math.random(-64,64) ,py + math.random(-32,32))   ...   local function contact(self,event)          --self:removeSelf()     if event.other.name == 'paddle' then       print('angle:'..event.other.angle..", px: "..event.other.px)       --- filtrando que powerup foi pego       if self.type == 'shield' then         print("force field")         local shield = {}         timer.performWithDelay( 5, function() shield = \_pwup.createShield() end)       elseif self.type == 'magicmissiles' then         timer.performWithDelay( 5, function() shield = \_pwup.createMagicMissiles(event) end)       end          end   end powerup.collision = contact powerup:addEventListener("collision")

You don’t need to pass sceneGroup argument any more.

Ty very much it seens to be ok, cant you explain why this happens? i mean why scenegroup was not woking and this way was working?

Another doubt is… when i change scenes the physical bodies and listeners goes or i should use some extra code to ensure that?

Sorry my bad english… and ty ty very very much for the help.

Try

function \_pwup.createPowerUp(px,py) ...   local sceneName = composer.getSceneName( 'current' )   local currentScene = composer.getScene( sceneName  )    local powerup = display.newImage( currentScene.view, "images/powerups/"..poweruptype[rnd]..".png",px + math.random(-64,64) ,py + math.random(-32,32))   ...   local function contact(self,event)          --self:removeSelf()     if event.other.name == 'paddle' then       print('angle:'..event.other.angle..", px: "..event.other.px)       --- filtrando que powerup foi pego       if self.type == 'shield' then         print("force field")         local shield = {}         timer.performWithDelay( 5, function() shield = \_pwup.createShield() end)       elseif self.type == 'magicmissiles' then         timer.performWithDelay( 5, function() shield = \_pwup.createMagicMissiles(event) end)       end          end   end powerup.collision = contact powerup:addEventListener("collision")

You don’t need to pass sceneGroup argument any more.

Ty very much it seens to be ok, cant you explain why this happens? i mean why scenegroup was not woking and this way was working?

Another doubt is… when i change scenes the physical bodies and listeners goes or i should use some extra code to ensure that?

Sorry my bad english… and ty ty very very much for the help.