On the 37th day, a-android in the middle of the game flying (a) environmental construction

Source: Internet
Author: User
Tags addchild

August 3, sunny day. " The green trees are thick and summer long . The tower reflected into the pond. The crystal curtain moves the breeze, the full frame rose one hospital fragrant.

"

Cocos2d is an open-source game development framework. Use it to develop 2D games very easy. Now the most basic branch is the iphone version number, you can develop 2D games on the iphone.

It provides the physics. Genie (Sprites). Parallax Reels (parallax scrolling), support iphone touch and accelerator are very versatile. There are many games that have been developed with COCOS2D in the App Store today.

Suppose you want to use the Java language for the development of cocos2d games on andriod to download the relevant development files at this URL:

https://github.com/ZhouWeikuan/cocos2d. after entering the URL above, let's say you don't need Git or SVN's friend to click on the "Download ZIP" in the lower right corner of the page.

at development time. Just copy the contents of the Libs folder in Cocos2d-android in the downloaded cocos-master to your project's Libs folder .

First, import the download file, throughout the whole picture

1, downloaded after the decompression, there is a folder called "Cocos2d-android", because of the development of Android, so import this folder.

2, after the import, there will be two errors.

Correction Method: The first error is that this project is a library project and it is possible to cancel its Libray property in the property.

The second error is that a jar file is already included in the directory. and SRC in the source code again and again. Simply delete the/libs/cocos2d-android.jar file to execute it.

3, execute on the simulator, get a demo list with various functions of cocos2d game, it can be used for everyone to study .

Second, the implementation of the first game-static aircraft

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 draws the graphic above the View object private Ccglsurfaceview view = null; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); view = new Ccglsurfaceview (this); Setcontentview (view);//Get Director Object Ccdirector Director = Ccdirector.shareddirector ();// Set the game engine screen output target Viewdirector.attachinview (view),//Set whether the game shows FPS value Director.setdisplayfps (TRUE);//Set the game's refresh rate FPS = Frame per Seconddirector.setanimationinterval (1/30.0f);//Generate Scene object Ccscene scene = Ccscene.node ();//Generate Layer object Planelayer layers = new Pl Anelayer (this);//Add layer to scene where scene.addchild (layer);//Notify Director, execution scene 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{//declares an Elf object private Ccsprite plane;private Ccdirector Director;private cgsize winsize;    Public Planelayer (Context context) {        director = Ccdirector.shareddirector (); winsize = Director.winsize ();    Initializes the Sprite object    plane = Ccsprite.sprite ("P.png");    Sets the position of the Sprite object    plane.setposition (CGPOINT.CCP (WINSIZE.WIDTH/2));    This.addchild (plane);    }}

you need to place a picture of an airplane p.png in the assets folder.

3. Implementation results


Source: http://download.csdn.net/detail/zwszws/7717559

4. Code Analysis

the core structure of the cocos2d is for example


1. Ccdirector: Director class is the organizer and leader of the game, which is responsible for dispatching all the objects and resources of the game.

Director is a singleton and only one instance exists in all games.

Director is similar to a film in the "Directing". As its name says, it manages the dispatch of all scenes in the Cocos2d-x: the ability to toggle the current scene (there is only one current scene in the game) and pause the action in the scene-this is the same as the actual director shouting "Ka"-pauses all actions or sounds in all scenes, Of course, it is also possible to start the action and sound effects in a scene. the total. director plays the role of a director.


2, Ccscene: The scene can be simply understood as a level. Multiple levels are combined to form a game.

scene is like a stage that will put all kinds of elements. Some are fixed: sets and props, some elements can be moved: male, heroine and so on.

This is also true in the game world, where the elements are placed in a game scene to adorn our scene.

Of course in the game. is not necessarily the same as the actual, in order to facilitate the operation of elements, we put different elements in different layers.

The ability to place multiple layers in a scene can be used to add layers to the scene using the Addchild function. When designing a scene, the scene object is usually generated and then added to the layer.

The Ccscene is responsible for the functionality of a container. Game development needs to render the object in Ccscene unified management, including the creation, destruction and scene switching, and from its class structure diagram can be seen, it directly inherits the Ccnode class, Ccnode is cocos2d-x the rendering chain, writing the game is basically dealing with it. Cocos2d-x can only render a ccscene at the same time, so ccscene is the root node of the rendering. When building a game, it is usually a scene to add one or more cclayer, a layer in addition to multiple ccsprite or ccmenu,ccsprite can also join Ccparticlesystem and so on.

This builds a rendering tree that cocos2d-x the tree to display the image on the screen.

3, Cclayer: A scene can have multiple scene layer composition.

The concept of the layers mentioned here is the same as the concept on Adobe Photoshop. In each game, the scene can be composed of many layers, each of which is responsible for the different tasks, such as showing the background, responsible for displaying props and displaying tasks. On top of each layer, you can add different elements. such as text, sprites, and menus.

The combination of layers and layers allows the game to display a wide variety of interfaces. Of course, in order to see the things on each layer, very many layers are set to transparent or translucent, otherwise you can only see the top of the thing.

4, Ccsprite: The game all can exercise things almost are elves. A scene layer which can include multiple sprites;

Sprite is the first element in the layer, the sprite ccsprite is the main object of the whole game development and processing, such as the aircraft in the sky, the tanks on the ground, the players control the task and so on are elves.

Ccsprite is a 2D image. Ccsprite can be created by an image or a rectangular sub-region of the image. As a matter of fact. The sprite is a constantly changing picture that includes changes in position, rotation, magnification, movement, and so on.





On the 37th day, a-android in the middle of the game flying (a) environmental construction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.