37th days one happy here-Android games hitting the plane (1) environment construction, 37th days android
August 3, Sunny. "The green trees are overcast and thick in summer, and the building is reflected in the pond. The crystal curtain starts with a breeze and is fragrant with roses. "
Cocos2d is an open-source game development framework that can be used to easily develop 2D games. Currently, the main branch is the iPhone version. You can develop 2D games on the iPhone. It provides physical, sprites, and parallax scrolling, and supports many features such as iPhone touch and accelerator. Currently, many games have been developed using cocos2d in App Store.
If you want to use Java for cocos2d game development on Andriod, you can download the relevant development files at this URL:
Unzip ZIP. During development, you only need to copy the contents in the libs directory of the cocos2d-android In the downloaded cocos-master to the libs directory of your project.
1. Import and download files.
1, after the download is decompressed, there is a "cocos2d-android" directory, because Android development, so the import of this directory.
2. Two errors will occur after the import.
Solution: The first error is that this project is a Library project. You can just cancel its Libray attribute in the attribute.
The second error is that the folder already contains a jar file, which is the same as the source code in src and only needs to delete the/libs/cocos2d-android.jar file to run.
3. Run on the simulator to get a list of demos with various functions of the cocos2d game for your reference.
2. Run the first game-static airplane
1. MainActivity. java
Package edu. eurasia. cocos2d_game01; import org. cocos2d. layers. CCScene; import org. cocos2d. nodes. CCDirector; import org. cocos2d. opengl. CCGLSurfaceView; import android. OS. bundle; import android. app. activity; public class MainActivity extends Activity {// create a view object. The cocos2d engine will draw the image on this view object private CCGLSurfaceView view = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); view = new CCGLSurfaceView (this); setContentView (view); // gets the director object CCDirector ctor = CCDirector. sharedDirector (); // sets the output target of the Game Engine screen Viewdirector. attachInView (view); // sets whether the game displays the FPS value director. setDisplayFPS (true); // set the game's update rate FPS = frame per seconddirector. setAnimationInterval (1/30366f); // generate the scenario object CCScene scene = CCScene. node (); // generate the layer object PlaneLayer layer = new PlaneLayer (this); // Add the layer to scene in the scenario. addChild (layer); // notify the director and run the scenario director. runWithScene (scene );}}
2. PlaneLayer. java
Package edu. eurasia. cocos2d_game01; import org. cocos2d. layers. CCLayer; import org. cocos2d. nodes. CCDirector; import org. cocos2d. nodes. CCSprite; import org. cocos2d. types. CGPoint; import org. cocos2d. types. CGSize; import android. content. context; public class PlaneLayer extends CCLayer {// declare a private CCSprite plane; private CCDirector ctor; private CGSize winSize; public PlaneLayer (Context context) {director = CCDirector. sharedDirector (); winSize = director. winSize (); // initialize the sprite object plane = CCSprite. sprite ("p.png"); // set the location of the sprite object plane. setPosition (CGPoint. ccp (winSize. width/2,200); this. addChild (plane );}}
Put p.png, a flying image, in the assets Directory.
3. Running result
Source code: http://download.csdn.net/detail/zwszws/7717559
4. Code Analysis
The core structure of Cocos2d is as follows:
1. CCDirector: the director class is the organizer and leader in the game, responsible for scheduling all objects and resources in the game;
Director is a singleton. Only one instance exists in all games. Director is similar to the "Director" in a movie, and, as its name suggests, it manages Scheduling for all scenes in the Cocos2d-x: It can switch the current scene (there is only one current scene in the game ), pause the action in the scenario-this is the same as the actual director shouting "seek"-it will pause all the actions or sound effects in all scenarios, and of course it can also start the action and sound effects in a scenario. In short, ctor plays the role of a Director.
2. CCScene: A scenario can be simply understood as a level. When multiple levels are combined, a game is formed;
Scene is like a stage where various elements are placed. Some are fixed: Landscape and props, and some are movable: male and heroine. This is also true in the gaming world. In a game scenario, we place undesirable elements to embellish our scenario. Of course, in the game, it may not be exactly the same as in reality. To facilitate the operation of elements, we place different elements on different layers. You can place multiple layers in one scenario. You can use the addChild function to add layers to the scenario. When designing a scenario, a scenario object is generally generated and a layer is added to it.
CCScene is responsible for the function of a container. During Game Development, the objects to be rendered are centrally managed in CCScene, including creation, destruction, and scenario switching, and from the structure of its class can be seen that it directly inherits the CCNode class, CCNode is the rendering chain of the cocos2d-x, write the game is basically dealing with it, the cocos2d-x can only render a CCScene, therefore, CCScene is the rendering root node. When building a game, you generally add one or more cclayers to one Scene, multiple CCSprite or CCMenu to one Layer, and CCParticleSystem to CCSprite. In this way, a rendering tree is built, and the cocos2d-x goes through the tree to display the image on the screen.
3. CCLayer: A scene can contain multiple set layers;
The concept of layers here is the same as that of Adobe Photoshop. In each game, a scenario can be composed of multiple layers, each of which is responsible for different tasks, such as displaying the background, displaying items, and displaying tasks. On each layer, you can add different elements, such as text, Genie, and menus. Through the combination between layers, the game can display a variety of interfaces. Of course, to see what is on each layer, many layers are set to transparent or translucent. Otherwise, you can only see what is on the top.
4. CCSprite: almost all items in the game that can be moved are genie. A set layer can contain multiple genie;
Sprite is the primary element in the layer. The Sprite CCSprite is the main object of game development and processing. For example, the planes in the sky, tanks on the ground, and tasks controlled by players are all genie. CCSprite is a 2D image. CCSprite can be created through an image or a rectangular subarea in the image. In fact, the genie is an image that can change constantly, including position change, rotation, amplification, and motion.
You can use the C language to write a shooting and plane-hitting game. You can use a keyboard to play the game. The operation is "a", "d", "w", or"
Hundreds of lines of code? Create a 2-dimensional array display interface, use switch to get direction control characters, write the corresponding processing logic for each character and display it. This idea is almost the same. I wrote it in my sophomore year.
A simple little game-some methods of airplane hitting I will point C ++ and use Java as well.
It seems that there is no difficulty... I wrote a brick .. It's almost the same as this one .. Send it to you on QQ. Check it out .. Haha... , Looking forward to communication! QQ: 279794658