Dvole
Dvole

Reputation: 5795

How to go to next level in cocos2d?

I have a simple game, breakout clone. There is gameplay layer, menu layer. I can call menu during play just fine, can use next level button ( it makes new gameplay layer with next level and transitions to its scene). But what i want is at the end of level, when I killed all blocks I want that menu to pop up so player could click next level. But the provlem is that i store blocks number in gameplay layer, so when they reach zero i get certain property turned to true. But when i try to read that property from menu when level ends, i can't, because my gameplay layer is already deallocated since i transited to menu.

Tldr; how to change level with button?

Upvotes: 0

Views: 482

Answers (1)

Jim Range
Jim Range

Reputation: 523

I think your question is about Game Architecture. There are lots of trade-offs regarding how you choose to architect your game. This is something that I have thought a lot about as I have created Cocos2d games.

The comment by Shailesh_ios will work. But if you want to create a more complex game you should consider designing the game in a way that neatly organizes the different modules and components in your game so that they are easy to use and support. As your game gets more complex it can become very messy if you add loads of functionality to the AppDelgate. To stay organized and keep your sanity when updating your code, I suggest creating a centralized game manager service in your game.

Create a Centralized GameManager Service --For all centralized services that your game will need, one thing that has worked good for me is to create a game manager class that is a CCNode or NSObject subclass. This class is made into a singleton. I then create separate classes for things like GameCenter, OpenFeint, In-App Purchase, player preferences, local scores and achievements, physics world manager, game audio, game state, etc.

Add class instances as modules to the GameManager --These classes are all then owned by the GameManager singleton class and can provide services to the game components that need them. Since these classes generally use very little memory there is no need to constantly be creating and destroying them each time a scene changes, which just wastes CPU cycles and battery life.

A note of caution --Be careful though, because it is easy to abuse the global singleton GameManager class by adding things to it that don't belong in it. Any centralized service that may be requested from various game components could be considered to be added to the GameManager. By adding to the GameManager, I mean coding each module in its own class and then have it be created and owned by the GameManager.

Examples-- For example, your custom local scores and achievements class could be setup to store a players scores, persist them to a file or NSUserDefaults, and make that information available to any game component that needs it.

The custom GameCenter or OpenFeint class would take care of authenticating the player and then as a service provide submitting scores/achievements, downloading scores/achievements, presenting the built in GameCenter/OpenFeint UI for leaderboards and achievements, etc.

This way you can focus on designing your game and have the basic services a game needs available from anywhere within your game.

Upvotes: 2

Related Questions