Its necessary to grasp the various parts of your game and what goes where! For the foremost part, you’ll be coping with layers (CClayer class) adding nodes, sprites, animations etc to that. this is often wherever the magic happens. however its conjointly necessary to comprehend that each one of this happens on one android Activity (a page) that is your primary access purpose to the android OS context. All the screen changes are all done by the CCDirector category that swaps in (and out) completely different scenes and layers.
This is why the android Back button (and a couple of alternative things) won’t compute of the box in switching between layers/scenes because as so much as android is bothered, solely one Activity page is in sight. Pressing the rear button will exit the application.
Knowing this may facilitate in making easy solutions to problems you may face e.g implementing the rear button , obtaining the android context, launching intents etc.
Main Activity – Android Activity
This is the entry point of your application and is the 1st page initialized once the game is launched. In regular android apps, the interface of this page is set out in an xml file that is hooked up to the Activity using the setContentView technique. Instead in your game, the interface is connected with GLSurfaceView category for rendering animations (adding your layers etc).
You can get the android application context needed to access several inbuilt android functions from this activity. it’s on this page that you simply can write code to concentrate for keypress (e.g backbutton) , intent activity result , manage application lifecycle (saving and loading game state) etc.
All of the sleek buttery animations pass by cocos2D uses the OpenGL ES normal and also the GLSurfaceView category provides the glue code to attach OpenGL ES to the android view system. allows OpenGL ES work with the Activity life-cycle. Makes it simple to decide on an applicable frame buffer pixel format. Creates and manages a separate rendering thread to enable sleek animation. GLSurfaceView is the main base for building applications that uses OpenGL ES for part or all of its rendering
Works sort of a real director. Its the category that makes and handles the main Window and manages how and when to execute the Scenes. The CCDirector is additionally responsible for:
– initialising the OpenGL ES context
– setting up the OpenGL pixel format (default on is RGB565)
– setting the OpenGL buffer depth (default one is 0-bit)
– setting the projection (default one is 3D) setting the orientation (default one is Portrait)
CCScene is a subclass of CCNode that’s used solely as an abstract conception. CCScene and CCNode are nearlyidentical with the distinction that CCScene has it’s anchor point (by default) at the middle of the screen. For the instant CCScene has no alternative logic than that, however in future releases it would have extra logic. it’s a goodpractice to use and CCScene as the parent of all your nodes.
CCLayer could be a subclass of CCNode that implements the TouchEventsDelegate protocol. All features from CCNode are valid, plus the subsequent new features:
It can receive Touches It can receive accelerometer input CCLayer.h.
Implementing the Back Button
Currently, to implement the rear button in my Ccocs2D games implement some state tracking among my MainActivity.java file to grasp that layer to show when the rear button is pressed. In MainActivity.java, among the onKeyPress technique, you’ll listen for a back button press event then show the suitable layers. you’ll implement a world stack data structure that keeps track of the foremost recent layer. Once the rear button is pressed, the present layer is replaced with the one below it.