Directory:
The first part: Cocos2d-x Introduction
Part II: Windows 7 In the environment Visual Studio Built in cocos2d-x 3.3beta0 Integrated development Environment
Part III: HelloWorld An example explanation
First, Cocos2d-x Introduction
- Cocos2d-x is an open source mobile 2D game Framework, published under the MIT License. This is a version of the C + + Cocos2d-iphone project. The focus of cocos2d-x development is around the cocos2d cross-platform, Cocos2d-x provides the framework. Mobile games, can be written in C + + or LUA, using the API is Cocos2d-iphone fully compatible. The Cocos2d-x project can be easily built and run in ios,android, BlackBerry BlackBerry and other operating systems. Cocos2d-x also supports desktop operating systems such as Windows, Macs, and Linux, so the source code written by the developer is easy to edit and debug in the desktop operating system.
- Why use this game frame?
-It uses standard C and C + + voice development, which is very good cross-platform and is supported by the mainstream platform. such as: Just write a copy of the code can be run on the Android platform and iOS platform. Almost all of the 2D games we see today are developed using this game framework (because their API design is very reasonable and easy to use)
3. Cocos2d-iphone is developed using Objective C voice and can only be run on the iOS platform.
Cocos2d-x is a C + + language version of this framework that is implemented across platforms.
COCOS2D-HTML5 is developed using the JavaScript language.
4. http://cocos2d-x.org/official website
5. Learn can see the developer's Help document Http://cocos2d-x.org/wiki/Cocos2d-x
6. The description of each class in the framework can be seen in reference http://www.cocos2d-x.org/reference/native-cpp/V3.3rc0/index.html
Second, Windows 7 In the environment Visual Studio Built in cocos2d-x 3.3beta0 integrated development environment.
Steps:
- Windows Cocos2d-x 3.3BETA0 integrated development environment configuration
- Create and run the Cocos2d-x project
- How to compile the Cocos2d-x program into an android program
Required Files:
- Cocos2d-x-3.3beta0.zip
- Python-2.7.8.msi
- Apache-ant-1.9.4-bin.zip
- Adt-bundle-windows-x86_64-20140702.zip
- Android-ndk-r9d-windows-x86_64.zip
(You'll actually need to install Eclipse and visual Studio 2012 and configure the appropriate environment variables first.)
(a) Windows Lower Cocos2d-x 3.3beta0 Integrated development Environment Configuration
1. Install Python, set environment variables (computer-right-click Properties-Advanced system settings-environment variables-user variables) path, add Road E:\Cocos2D\Python27, test: Cmd-python;
2. Unzip the above several compressed packages into a custom installation directory (note that the path can not have spaces, which is necessary to configure the NDK later);
3. cmd-Enter cocos2d-x-3.3beta0 path, dir, and then run setup.py
1) Configure Ndk_root (the path must not have spaces):
E:\Cocos2D\adt-bundle-windows-x86_64-20140702\android-ndk-r9d
(This is my own decompression into the adt-bundle-windows-x86_64-20140702)
2) configuration android_sdk_root:
E:\Cocos2D\adt-bundle-windows-x86_64-20140702\sdk
3) configuration ant_root:
E:\Cocos2D\Cocos2D\apache-ant-1.9.4\bin
4. Test: Cmd-cocos;
(ii) creation and operation of Cocos2d-x Engineering
Method One: Use the create_project.py in Cocos2d-x-3.0beta2\tools\project-creator to create
Double-click create_project.py
Type the project name, package name, select the project path and the language you are using, and click the Creat button.
After a successful creation, you can see it in e:\cocos2d\cocos2dx\helloc++ with Android, Mac, Linux, Win32 platform, choose Win32 Platform
Double-click Open helloc++ and open the solution using Visual Studio 2012. and press F5 to run.
Method Two: Create in the command line
1. cmd-Enter the directory (①E:②CD E:\Cocos2D\cocos2dx) you want to use to create the Cocos2d-x project, using Cocos new Hellocc-l cpp-p com.jikexueyuan.hellocc-d. At the current target Record the creation of HELLOCC project, using C + + language, package named COM.JIKEXUEYUAN.HELLOCC;
2. Use Explorer after creation. To open the directory where you created the Cocos2d-x project, open the Proj.win32 directory, and double-click HelloCC.sln to open the solution using Visual Studio 2012.
3. Open the Classes directory in the Solution Explorer, double-click HelloWorldScene.cpp, this is the program source code, press F5 compile run.
(c) the Cocos2d-x program compiled into Android program
1. Method one cmd-into the directory of the Cocos2d-x project HELLOCC that you want to compile into the Android program (①e:②
E:\Cocos2D\cocos2dx\HelloCC), using Cocos compile-p Android compiler to generate the APK directly;
The problems encountered in this process are as follows:
1) Error "The Java display is not an internal or external command, and it is not a running program," Because Java environment variables are not set properly. New system variable Java_home, the value is: E:\Program files\java\jdk1.7.0_67\, and then add%java_home%\bin in the system variable path;
2) Error "Unable to resolve project target ' android-10 '" means that the Android AVD version of this machine does not have the version specified in the Cocos2d-x project. The workaround is download the API10 for Android, I do it from the Android SDK manager.
3) In addition to encounter two wonderful problems, but does not affect the compilation into an apk:
① will error when compiling at the beginning. We use Cocos compile–p Android>1.txt to put the compilation information in the TXT text file 1. You can see the initial error as follows:
ERROR:SWT folder ' does not exist.
Please set ANDROID_SWT to the folder containing Swt.jar for your platform.
The reason for the network check is that there are spaces in the Android environment configuration and the test cmd-android H can be successful:
② error is as follows:
But checking the C:\Windows\System32 under the Java.exe is no problem:
Although these two problems have always occurred, but will not affect the compilation into the API, the following success:
Compilation succeeded.
The APK directory is E:\Cocos2D\cocos2dx\HelloCC\bin\debug\android and can be sent directly to the phone for installation to run on the phone.
- Method Two manually compile the native code into a library and then use Eclipse to package it as an Android application.
1) cmd-Enter E:\Cocos2D\cocos2dx\HelloCC\proj.android, execute build_native.py generate so file.
2) Open Eclipse (note that Eclipse's workspace cannot be consistent with MyEclipse), import
E:\Cocos2D\cocos2dx\HelloCC\proj.android (the compilation is already in use at the time of import, it is compiled in cmd, and cmd is turned off).
If you import eclipse after the error: "Cocos2dxactivity cannot be resolved ...". The workaround is to
E:\Cocos2D\cocos2dx\HelloCC\cocos2d\cocos\platform\android\java\ The org directory under the SRC directory (this is the source code provided to us by Android) is dragged directly into the SRC directory of HELLOCC in Eclipse (-copy films and Folders-yes), which copies the system-provided classes.
3) At this point, open the Libs directory to see the native library file it has generated
Run as Android application:
This approach, which runs in eclipse, sometimes succeeds and sometimes fails, and so far has not found the cause. No harm, the APK file can be generated automatically using the previous method.
Third, HelloWorld An example explanation
Open a project solution in Visual Studio 2012
- How to start the application:
BOOL Appdelegate::applicationdidfinishlaunching () { Initialize Director Auto director = Director::getinstance ();//Get a Director: Auto Glview = Director->getopenglview ();//Create a Eglview to render the graphic if (!glview) { Glview = Glviewimpl::create ("My Game"); Director->setopenglview (Glview); } Turn on display FPS Director->setdisplaystats (TRUE);//Set whether to render frame rate data Set FPS. The default value is 1.0/60 if you don ' t call this Director->setanimationinterval (1.0/60);//Set Frame frequency Create a scene. It ' s an Autorelease object Auto scene = Helloworld::createscene ();//create Scene (Scene) Run Director->runwithscene (scene);//Start execution, start the scene (you can render the content after startup) return true; } |
2. Class Helloworld:public Cocos2d::layer
Class Helloworld:public Cocos2d::layer {//HelloWorld is a layer class that inherits from Layer and whose parent container is a scene Public There ' s no ' id ' in CPP, so we recommend returning the class instance pointer Static cocos2d::scene* Createscene ();//Create Scene Here ' s a difference. Method ' init ' in cocos2d-x returns BOOL, instead of returning ' ID ' in Cocos2d-iphone virtual BOOL init (); A selector callback void Menuclosecallback (cocos2d::ref* psender); Implement the "Static Create ()" Method manually Create_func (HelloWorld); }; |
3. scene* Helloworld::createscene ()
scene* Helloworld::createscene () { ' Scene ' is an Autorelease object Auto scene = Scene::create ();//Create a scene ' Layer ' is an Autorelease object Auto layer = Helloworld::create ();//Create a layer Add layer as a child to scene Scene->addchild (layer);//Add layer to scene Return the scene Return scene;//returning a scene instance } |
COCOS2DX Study Note One