how to remove a group when scene changes

hi i am having a bit problem in removing images in a displaygroup.i created two functions one to 

keep adding an image every second using a timer which looked like this

group1= display.newGroup() 

group1.anchorChildren = true                

group1.x = 0;

group1.y = 0;–(in create scene)

function addenemy()

 enemy=display.newImageRect(“imageofenemy.png”,80,80);

enemy.x=centerX+100;

enemy.y=centerY;

group1:insert(enemy);

end

enemyTimer = timer.performWithDelay(1000, addenemy,11);

(canceled the timer in hide scene  in phase==“will”)


and another function to move the enemy

 function move()

for a = group1.numChildren,1,-1  do

if(group1[a].y < 700) then

group1[a].y = group1[a].y +18

else 

group1:remove(group1[a])

end

end

end

moveTimer=timer.performWithDelay(2, move, -1);

(canceled the timer in hide scene in phase==“will” )


when i change scene on collision between player and enemy the enemy keeps appearing even 

when i changed the scene.

Are you using composer? You need to read the documentation for that to see how to insert your objects into the scene view, and also about lua scope. All your variables and functions appear to be globals which is a good way to get into trouble early on.

hi found my error i wasnt adding timer.cancel properly.

Cool - I’d still recommend you look into the subjects I suggested, as you might be getting into some bad habits that will take time to unravel when your projects get bigger.

Are you using composer? You need to read the documentation for that to see how to insert your objects into the scene view, and also about lua scope. All your variables and functions appear to be globals which is a good way to get into trouble early on.

hi found my error i wasnt adding timer.cancel properly.

Cool - I’d still recommend you look into the subjects I suggested, as you might be getting into some bad habits that will take time to unravel when your projects get bigger.