0

There are two scenes(scene1, scene2) in my game, both of them have some sprites. scene1 will be presented at first with skView. scene2 will be presented with the same skView when the hero is stronger. In case the hero is weak, scene1 will be presented again. (Here is the question point.) In my case the sprites which were added to the scene1, are added to the scene1 again(I mean they are doubled).

I did another test, where i presented the scene1 just twice one after the other. I got the same issue.

Why is this happened? Does it mean, that I should remove all the sprites on scene1, before I switch to scene2, and add all sprites again when I switch back???

PS: the question is related to How to switch SKScenes in SpriteKit with Swift properly?.

Thanks in advance.

Community
  • 1
  • 1
Sifeng
  • 713
  • 9
  • 23
  • You have a flaw in your code somewhere where you either reuse the same scene without it having been deallocated, or you add the same sprites twice for some reason. – CodeSmile Oct 10 '14 at 10:08
  • @LearnCocos2D the same sprites were added twice, because the scene was presented again. So you mean, I must deallocate(remove) all sprites in the scene, before I leave the scene? If yes, i am confused, why can't i just switch back to old scene(which was initialized already) without add/remove stuff? (confusing...) – Sifeng Oct 10 '14 at 10:25
  • 3
    No, I mean you present the new scene but you may leave the first scene alive, for example by assigning it to an instance variable. Then you present the same scene again, instead of creating a new one, but you also run the code that adds the sprites again, which will now be duplicated. So either you let the scene deallocate and create a new one, or you make sure that when reusing the scene you don't run any code that adds nodes that should only be added the moment the scene is initialized. – CodeSmile Oct 10 '14 at 10:30
  • @LearnCocos2D I see. I have to check the initialization status in the scene before adding sprites (again). Like, if !isInit {add sprites}. Thank you! You saved me a lot of time. :> – Sifeng Oct 10 '14 at 10:49

0 Answers0