Reputation:
Currently, I am working at a Sprite Kit game with Swift.
On the Screen, at the beginning, there are 5 Platforms. They are all SKSpriteNode's
with color, all looking exactly the same.
As the Player moves, the bottom Platform moves out of the screen on the Y-Axis, and a new generated Platform spawns on top of the screen.
Now I am curious, how would be the best way to code the Platforms? Should I create 5 Platforms, and as they move out of the Screen, they appear on top of the Screen again? Or do I recreate them again on top of the Screen? There is no limitation of the Platforms.
The Platforms also perform SKActions
, as for e.g. recolerize after the Player hits a Platform.
Example Code of the Platform:
Platform = SKSpriteNode (color: SKColor.redColor(), size: CGSize(width: self.frame.size.width , height: 25))
Platform.position = CGPoint(x:self.frame.size.width / 2, y: 40)
Platform.zPosition = 1
addChild(Platform)
Upvotes: 0
Views: 113
Reputation: 6781
The answer is yes: Reuse game objects if possible. Each time you instantiate an object will incur some overhead. Avoiding that will always be better.
For instance, you could just set the Platform.position
to the top of the screen whenever you go out of the bottom bounds of the screen. It shouldn't be difficult, always reuse game objects when possible.
Upvotes: 1