From: http://hi.baidu.com/184367426/blog/item/b1f4c6396c798412baa1678b.html
Now the latest project has been moved to https://github.com/ZhouWeikuan/cocos2d
This blog explains the most basic cocos2d-android-1 usage, mainly refer to this article
For cocos2d and cocos2d-android-1 do not know Copper shoes move here
Or here
OK. Back to the topic, make the following preparations before development:
- Android Development Environment
- Download the latest cocos2d-template.zip file.
- In eclipse, I will just decompress the cocos2d-template project import in and change the project name to your game name, such as tankwar. Now I can do the game development after this template.
The running result of this game template is as follows: The game displays the real-time frames in the lower left corner and the coordinates of the touch points on the page, therefore, if you want to add other rich content, you have to do it yourself.
Observe this project. There are several important parts to illustrate. First, this is an android project, so you can see it after some simple development. Second, there is a fps_images.png image in the assetsdirectory, in the lower left corner of the FPS used in the font is from this file, the back will also find that the game materials, under normal circumstances are put under the Assets Directory; finally, in the libs directory is cocos2d-android.jar, is a cocos2d-android-1 engine that implements various movements and effects such as scenes, layers, Sprite, and those needed during the game, it is the most important part of the project.
There is only one gameactivity. Java source file in the SRC folder. The following describes the important parts of the file:
1234567 |
... import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
public class GameActivity extends
Activity { |
Gameactivity is an android activity that contains all the methods for an activity from create to destory:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
/** Called when the activity is first created. */ @Override public
void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); // set the window status, no tile, full screen and don't sleep // Windows has no title, full screen, and does not sleep requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); CCGLSurfaceView mGLSurfaceView; mGLSurfaceView =
new CCGLSurfaceView( this ); setContentView(mGLSurfaceView); // attach the OpenGL view to a window // Connect OpenGL to the current window. I am not very familiar with this principle... CCDirector.sharedDirector().attachInView(mGLSurfaceView); // no effect here because device orientation is controlled by manifest CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait); // show FPS // set false to disable FPS display, but don't delete fps_images.png!! // Display FPS CCDirector.sharedDirector().setDisplayFPS( true ); // Frames per second // Number Of Frames CCDirector.sharedDirector().setAnimationInterval( 1 .0f /
60 ); // Create a scenario CCScene scene = TemplateLayer.scene(); // Make the scene active to start running CCDirector.sharedDirector().runWithScene(scene); } @Override public
void onStart() { super .onStart(); } @Override public
void onPause() { super .onPause(); CCDirector.sharedDirector().pause(); } @Override public
void onResume() { super .onResume(); CCDirector.sharedDirector().resume(); } @Override public
void onDestroy() { super .onDestroy(); CCDirector.sharedDirector().end(); } |
The following is the templatelayer class in the gameactivity class,
123456789101112131415161718192021222324252627282930313233343536373839 |
static class TemplateLayer extends
CCLayer { CCLabel lbl; public
static CCScene scene() { CCScene scene = CCScene.node(); CCLayer layer =
new TemplateLayer(); scene.addChild(layer); return
scene; } protected
TemplateLayer() { this .setIsTouchEnabled( true ); lbl = CCLabel.makeLabel( "Hello World!" ,
"DroidSans" ,
24 ); addChild(lbl,
0 ); lbl.setPosition(CGPoint.ccp( 160 ,
240 )); } @Override public
boolean ccTouchesBegan(MotionEvent event) { CGPoint convertedLocation = CCDirector.sharedDirector() .convertToGL(CGPoint.make(event.getX(), event.getY())); String title = String.format( "touch at point(%.2f, %.2f)" , convertedLocation.x, convertedLocation.y); if
(lbl != null ) { lbl.setString(title); } return
CCTouchDispatcher.kEventHandled; } } |
Check the scene () method. In fact, ccdirector. shareddire() requires a ccscene as the start point, while scence requires a cclayer as the start point. The templatelayer scence () method returns a ccscence containing the cclayer to Director, which is like this. In other parts, a label is placed in the layer to display the location of the last touch event, and the cctouchesbegan (touch donw) event is reloaded. In this way, the label is updated.
At this point, a simple cocos2d-android-1 engine based on not very much on the game has been running through the game, call ~ I also have a rest, check some things, and sort out ccsprite related things in a few days.
==================================
PS. The advantage of using templates and ready-made jar is convenience. You can build something in a few minutes and start to focus on time-consuming levels. But at this stage of cocos2d-android-1 is not very mature, sometimes there will be some strange things, this case can not through the jar package to locate the problem in the end where, so some time ago, I used the code checkout from here in the experiment process. This solved the debugging problem, and then I could provide more specific and targeted feedback to developers.
And there is a cocos2d demo in the source code. We can refer to this demo to start the experiment :)