We can create a simple cocos2d helloWorld app in android sdk simply by follow the steps below. Before that you need eclipse in your system with android sdk.
1. Open eclips and select File-->New-->Android Project, then you got a window like this
2. You just name the project and other fields like in the image and hit the finish button. Now your project is in workspace
3. Open your project --> src --> com.shatechs.firstcocos2d and double click on the java file. Now your java file appears on the code window as below.
4. After that you right click on project folder -->new-->folder and named as libs. Then you download cocos2d-android.jar and copy to the libs folder.
5. Then right click on cocos2d-android.jar --> Build Path-->Add to Build Path. Now the jar file is added.
6. Now replace the Activity class with the following code
CCGLSurfaceView mGLSurfaceView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hiding the app's title from the screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
// set the screen flag to full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Keeps screen active
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mGLSurfaceView = new CCGLSurfaceView(this);
// set the view as mGLSurfaceView
setContentView(mGLSurfaceView);
}
@Override
public void onStart() {
super.onStart();
// attach the OpenGL view to a window
CCDirector.sharedDirector().attachInView(mGLSurfaceView);
//Change the device orientation to Portrait
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);
// set false to disable FPS display, but don't forget to put fps_images.png in the assets folder
CCDirector.sharedDirector().setDisplayFPS(true);
// set frame rate
CCDirector.sharedDirector().setAnimationInterval(1.0f / 60);
// Create an instance of scene from HelloWorldLayer class
CCScene scene = HelloWorldLayer.scene();
// Make the Scene active
CCDirector.sharedDirector().runWithScene(scene);
}
@Override
public void onPause() {
super.onPause();
CCDirector.sharedDirector().pause();
}
@Override
public void onResume() {
super.onResume();
CCDirector.sharedDirector().resume();
}
@Override
public void onStop() {
super.onStop();
CCDirector.sharedDirector().end();
}
5. After closing the Activity class add the following class by just copy pasting
class HelloWorldLayer extends CCLayer {
//Gets screen size of the android device
CGSize s = CCDirector.sharedDirector().winSize();
public static CCScene scene() {
//Creates scene
CCScene scene = CCScene.node();
//creates an instance of HelloWorldLayer
CCLayer layer = new HelloWorldLayer();
//adds layer to scene
scene.addChild(layer);
return scene;
}
protected HelloWorldLayer() {
CCLabel lab = CCLabel.makeLabel("Hello World", "serif", 40);
lab.setPosition(240, 160);
addChild(lab);
}
}
6. Download the following image and copy to the asset folder
7. You completed your first android cocos2d project just now. Now you can build and run the project
Note: For getting the emulator in landscape mode press Ctrl+F12/F11 .
1. Open eclips and select File-->New-->Android Project, then you got a window like this
2. You just name the project and other fields like in the image and hit the finish button. Now your project is in workspace
3. Open your project --> src --> com.shatechs.firstcocos2d and double click on the java file. Now your java file appears on the code window as below.
4. After that you right click on project folder -->new-->folder and named as libs. Then you download cocos2d-android.jar and copy to the libs folder.
5. Then right click on cocos2d-android.jar --> Build Path-->Add to Build Path. Now the jar file is added.
6. Now replace the Activity class with the following code
CCGLSurfaceView mGLSurfaceView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hiding the app's title from the screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
// set the screen flag to full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Keeps screen active
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mGLSurfaceView = new CCGLSurfaceView(this);
// set the view as mGLSurfaceView
setContentView(mGLSurfaceView);
}
@Override
public void onStart() {
super.onStart();
// attach the OpenGL view to a window
CCDirector.sharedDirector().attachInView(mGLSurfaceView);
//Change the device orientation to Portrait
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);
// set false to disable FPS display, but don't forget to put fps_images.png in the assets folder
CCDirector.sharedDirector().setDisplayFPS(true);
// set frame rate
CCDirector.sharedDirector().setAnimationInterval(1.0f / 60);
// Create an instance of scene from HelloWorldLayer class
CCScene scene = HelloWorldLayer.scene();
// Make the Scene active
CCDirector.sharedDirector().runWithScene(scene);
}
@Override
public void onPause() {
super.onPause();
CCDirector.sharedDirector().pause();
}
@Override
public void onResume() {
super.onResume();
CCDirector.sharedDirector().resume();
}
@Override
public void onStop() {
super.onStop();
CCDirector.sharedDirector().end();
}
5. After closing the Activity class add the following class by just copy pasting
class HelloWorldLayer extends CCLayer {
//Gets screen size of the android device
CGSize s = CCDirector.sharedDirector().winSize();
public static CCScene scene() {
//Creates scene
CCScene scene = CCScene.node();
//creates an instance of HelloWorldLayer
CCLayer layer = new HelloWorldLayer();
//adds layer to scene
scene.addChild(layer);
return scene;
}
protected HelloWorldLayer() {
CCLabel lab = CCLabel.makeLabel("Hello World", "serif", 40);
lab.setPosition(240, 160);
addChild(lab);
}
}
6. Download the following image and copy to the asset folder
7. You completed your first android cocos2d project just now. Now you can build and run the project
Note: For getting the emulator in landscape mode press Ctrl+F12/F11 .
Heya, great tutorial. I have a question, in my app, I set kCCDeviceOrientationPortrait, but when I move the device orientation (I'm working with a nexus one), the app restarts. Do you know wath happens in this situation? Greetings!!!!!
Thank you So much for your code I tried every possible websites but of no use always there was an error in code i was loosing hope in the cosco2d. But again because of you cosco2d is ignited in my mind.
Hello Shajir, That was a nice tutorial. I wanted to put on the admob ads onto a scene for 20sec. Can u help me with it. How to display the ad for 20sec and then the game layer must be focused.