Cocos2dx就不废话介绍了, 非常火的游戏引擎,关键是它开源,可以免费下载、学习、开发,不用搞这么多激活的东西。下面以Cocos2dx 2.x为例说明这个平台的一些基本东西。虽然现在Cocos2dx早已去到3.x的稳定版本,但之所以选用Cocos2dx 2.x版本,主要是这方面的资料比较多,本来搞Cocos2dx的人就不多了,搞了也没几个人写写编程记录,再去毫无资料只有一堆难以看懂的官方文档的Cocos2dx 3.x,基本上等于瞎整。
首先,由于这引擎在Windows的主流平台是通过大家熟悉的C++写出来的,所以Cocos2dx 2.x的开发,你首先要准备Visual Studio 2010以上的版本。如果你选用Cocos2dx 3.x还要支持准备Visual Studio 2012以上的版本。已经不支持Visual Studio 2008了,这就没办法了,怪怪下载一个完整安装。网上一搜一堆,不说了。
之后,由于Cocos2dx 2.x在当今网站的最后一版2.2.6不再提供InstallWizardForVS2010.js这鬼东西,需要用Python语言创建新的工程,因此你可以参看我之前的《【Python】Windows版本的Python开发环境的配置,Helloworld,Python中文问题,输入输出、条件、循环、数组、类》(点击打开链接)先配好Python2.x的环境。
然后,你才在Cocos2dx的中文官网(点击打开链接),如下图,拉到最下面,下载Cocos2dx 2.x版本。
具体下载地址为:http://54.192.159.100/cocos2d-x-2.2.6.zip?fid=D2qKo-*f7vaAbUj7fijGQlgs5hzdkV4YAAAAAOeOX4E0-gk5fRKd*Y-Bb8j7lCvn&mid=666&threshold=150&tid=5D3FD9855047216E67D27C85E859FC2D&srcid=119&verno=1
下载之后,得到一个cocos2d-x-2.2.6.zip,解压,这个解压位置就是你以后cocos2dx的开发环境、工程的所在目录,和PHP是一样的,不好迁移,请小心选择。
在你的cocos2dx的解压目录找到build-win32.bat这东西,双击运行。
经历如下的一个漫长的编译过程:
搞好之后,会出现如下的一个界面,cocos2dx官方提供的一大堆例子,有兴趣看看之后,可以直接关闭。
至此,Cocos2dx的配置完成。
下面创建一个属于我们自己的Helloworld,不要用官方的例子,根本看不出什么端倪。
利用命令行进入.\cocos2d-x-2.2.6\tools\project-creator这个文件夹,利用python命令,如下图,创建一个工程。
- create_project.py -project 你的工程文件夹名称 -package 包名 -language 开发语言(基本上为cpp,不排除有大神精通肥猪流的Lua,也不建议使用javascript开发!)
用python 3的朋友会遇到新的问题:上面py文件出错。
解决方案是。修改之:
- #!/usr/bin/python
- #coding=utf-8
- # create_project_v3.py
- # Create cross-platform cocos2d-x project
- # Copyright (c) 2012 cocos2d-x.org
- # Author: WangZhe
- # define global variables
- context = {
- "language" : "undefined",
- "src_project_name" : "undefined",
- "src_package_name" : "undefined",
- "dst_project_name" : "undeifned",
- "dst_package_name" : "undefined",
- "src_project_path" : "undefined",
- "dst_project_path" : "undefined",
- "script_dir" : "undefined",
- }
- platforms_list = []
- # begin
- import sys
- import os, os.path
- import json
- import shutil
- def dumpUsage():
- print ("Usage: create_project.py -project PROJECT_NAME -package PACKAGE_NAME -language PROGRAMING_LANGUAGE")
- print ("Options:")
- print (" -project PROJECT_NAME Project name, for example: MyGame")
- print (" -package PACKAGE_NAME Package name, for example: com.MyCompany.MyAwesomeGame")
- print (" -language PROGRAMING_LANGUAGE Major programing lanauge you want to used, should be [cpp | lua | javascript]")
- print ("")
- print ("Sample 1: ./create_project.py -project MyGame -package com.MyCompany.AwesomeGame")
- print ("Sample 2: ./create_project.py -project MyGame -package com.MyCompany.AwesomeGame -language javascript")
- print ("")
- def checkParams(context):
- # generate our internal params
- context["script_dir"] = os.getcwd() + "/"
- global platforms_list
- # invalid invoke, tell users how to input params
- if len(sys.argv) < 7:
- dumpUsage()
- sys.exit()
- # find our params
- for i in range(1, len(sys.argv)):
- if "-project" == sys.argv[i]:
- # read the next param as project_name
- context["dst_project_name"] = sys.argv[i+1]
- context["dst_project_path"] = os.getcwd() + "/../../projects/" + context["dst_project_name"]
- elif "-package" == sys.argv[i]:
- # read the next param as g_PackageName
- context["dst_package_name"] = sys.argv[i+1]
- elif "-language" == sys.argv[i]:
- # choose a scripting language
- context["language"] = sys.argv[i+1]
- # pinrt error log our required paramters are not ready
- raise_error = False
- if context["dst_project_name"] == "undefined":
- print ("Invalid -project parameter")
- raise_error = True
- if context["dst_package_name"] == "undefined":
- print ("Invalid -package parameter")
- raise_error = True
- if context["language"] == "undefined":
- print ("Invalid -language parameter")
- raise_error = True
- if raise_error != False:
- sys.exit()
- # fill in src_project_name and src_package_name according to "language"
- if ("cpp" == context["language"]):
- context["src_project_name"] = "HelloCpp"
- context["src_package_name"] = "org.cocos2dx.hellocpp"
- context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-cpp"
- platforms_list = ["ios",
- "android",
- "win32",
- "winrt",
- "wp8",
- "mac",
- "blackberry",
- "linux",
- "marmalade",
- "tizen",
- "wp8-xaml"]
- elif ("lua" == context["language"]):
- context["src_project_name"] = "HelloLua"
- context["src_package_name"] = "org.cocos2dx.hellolua"
- context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-lua"
- platforms_list = ["ios",
- "android",
- "win32",
- "blackberry",
- "linux",
- "marmalade"]
- elif ("javascript" == context["language"]):
- context["src_project_name"] = "HelloJavascript"
- context["src_package_name"] = "org.cocos2dx.hellojavascript"
- context["src_project_path"] = os.getcwd() + "/../../template/multi-platform-js"
- platforms_list = ["ios",
- "android",
- "win32"]
- # end of checkParams(context) function
- def replaceString(filepath, src_string, dst_string):
- content = ""
- f1 = open(filepath, "rb")
- for line in f1:
- temp_content = line.decode('utf-8')
- if src_string in temp_content:
- content += temp_content.replace(src_string, dst_string)
- else:
- content += temp_content
- f1.close()
- f2 = open(filepath, "wb")
- f2.write(content.encode(encoding='utf_8', errors='strict'))
- f2.close()
- # end of replaceString
- def replaceLastNameInPath(raw_path):
- cnt = raw_path.count("PROJECT_NAME")
- if (cnt > 0):
- raw_path = raw_path.replace("PROJECT_NAME", context["src_project_name"], cnt - 1)
- dst = raw_path.replace("PROJECT_NAME", context["dst_project_name"])
- return dst
- return ""
- # end of replaceLastNameInPath
- def processPlatformProjects(platform):
- # determine proj_path
- proj_path = context["dst_project_path"] + "/proj.%s/" % platform
- java_package_path = ""
- # read josn config file or the current platform
- f = open("%s.json" % platform)
- data = json.load(f)
- # rename package path, like "org.cocos2dx.hello" to "com.company.game". This is a special process for android
- if (platform == "android"):
- src_pkg = context["src_package_name"].split('.')
- dst_pkg = context["dst_package_name"].split('.')
- os.rename(proj_path + "src/" + src_pkg[0],
- proj_path + "src/" + dst_pkg[0])
- os.rename(proj_path + "src/" + dst_pkg[0] + "/" + src_pkg[1],
- proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1])
- os.rename(proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1] + "/" + src_pkg[2],
- proj_path + "src/" + dst_pkg[0] + "/" + dst_pkg[1] + "/" + dst_pkg[2])
- java_package_path = dst_pkg[0] + "/" + dst_pkg[1] + "/" + dst_pkg[2]
- # rename files and folders
- for i in range(0, len(data["rename"])):
- tmp = data["rename"][i].replace("PACKAGE_PATH", java_package_path)
- src = tmp.replace("PROJECT_NAME", context["src_project_name"])
- if (platform == "wp8-xaml"):
- dst = replaceLastNameInPath(tmp)
- else:
- dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
- if (os.path.exists(proj_path + src) == True):
- os.rename(proj_path + src, proj_path + dst)
- # remove useless files and folders
- for i in range(0, len(data["remove"])):
- dst = data["remove"][i].replace("PROJECT_NAME", context["dst_project_name"])
- if (os.path.exists(proj_path + dst) == True):
- shutil.rmtree(proj_path + dst)
- # rename package_name. This should be replaced at first. Don't change this sequence
- for i in range(0, len(data["replace_package_name"])):
- tmp = data["replace_package_name"][i].replace("PACKAGE_PATH", java_package_path)
- dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
- if (os.path.exists(proj_path + dst) == True):
- replaceString(proj_path + dst, context["src_package_name"], context["dst_package_name"])
- # rename project_name
- for i in range(0, len(data["replace_project_name"])):
- tmp = data["replace_project_name"][i].replace("PACKAGE_PATH", java_package_path)
- dst = tmp.replace("PROJECT_NAME", context["dst_project_name"])
- if (os.path.exists(proj_path + dst) == True):
- replaceString(proj_path + dst, context["src_project_name"], context["dst_project_name"])
- # done!
- print ("proj.%s\t\t: Done!" % platform)
- # end of processPlatformProjects
- # -------------- main --------------
- # dump argvs
- # print sys.argv
- # prepare valid "context" dictionary
- checkParams(context)
- # import pprint
- # pprint.pprint(context)
- # copy "lauguage"(cpp/lua/javascript) platform.proj into cocos2d-x/projects/<project_name>/folder
- if (os.path.exists(context["dst_project_path"]) == True):
- print ("Error:" + context["dst_project_path"] + " folder is already existing")
- print ("Please remove the old project or choose a new PROJECT_NAME in -project parameter")
- sys.exit()
- else:
- shutil.copytree(context["src_project_path"], context["dst_project_path"], True)
- # call process_proj from each platform's script folder
- for platform in platforms_list:
- processPlatformProjects(platform)
- # exec "import %s.handle_project_files" % (platform)
- # exec "%s.handle_project_files.handle_project_files(context)" % (platform)
- print ("New project has been created in this path: " + context["dst_project_path"].replace("/tools/project-creator/../..", ""))
- print ("Have Fun!")
在创建的过程中,可能会有如上图的报错,不用管,因为你创建的MyDemo工程已经成功出现在.\cocos2d-x-2.2.6\projects文件夹了,进入.\cocos2d-x-2.2.6\projects\MyDemo\proj.win32打开HelloCpp.sln,开始利用Cocos2dx引擎(框架)在Windows上开发游戏。
你可以观察到在.\cocos2d-x-2.2.6\projects\MyDemo,出现proj.Android、proj.iOS等平台的文件夹,这里意味着,你可以在这些平台同样利用Cocos2dx引擎(框架)进行开发。不是说一个平台开发,多平台共同编译……
打开HelloCpp.sln之后,等待Visual Studio 2010加载一大堆外部库之后,你可以看到如下的文件结构:
首先,你要明白一个概念,Cocos2dx中最简单的Helloworld,是由一个叫Helloworld的场景、然后在这个Helloworld场景上放上一个Helloworld字符串所组成的,
官方初始的Helloworld还在Helloworld这个场景中放上背景图片、关闭按钮等杂七杂八的东西,让Helloworld文件看起来比较复杂。
我们先从上图的main.cpp与AppDelegate.cpp入手,先设置好程序的一些基本东西。
首先是main.cpp,这东西的主函数就5行代码,将其关于程序标题与窗口尺寸的18、19行进行修改,如下:
- #include "main.h"
- #include "AppDelegate.h"
- #include "CCEGLView.h"
- USING_NS_CC;
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- UNREFERENCED_PARAMETER(hPrevInstance);
- UNREFERENCED_PARAMETER(lpCmdLine);
- // create the application instance
- AppDelegate app;
- CCEGLView* eglView = CCEGLView::sharedOpenGLView();
- eglView->setViewName("HelloWorld");//修改程序运行的标题为Helloworld
- eglView->setFrameSize(1024, 768);//修成程序运行的尺寸为1024x768
- return CCApplication::sharedApplication()->run();
- }
其余代码看不懂暂时先不要管,就像你当初学C语言,不必在乎#include<stdio.h>与void main(){}是什么鬼,你首先要会改printf()中的内容。关于引入、函数这些东西,回头再来学。
之后,还没有改完,对AppDelegate.cpp进行修改,把第22行的帧数调试信息关了,第25行的帧数有兴趣可以改下,不过改了也看不出效果,修改如下:
- #include "AppDelegate.h"
- #include "HelloWorldScene.h"
- USING_NS_CC;
- AppDelegate::AppDelegate() {
- }
- AppDelegate::~AppDelegate()
- {
- }
- bool AppDelegate::applicationDidFinishLaunching() {
- // initialize director
- CCDirector* pDirector = CCDirector::sharedDirector();
- CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
- pDirector->setOpenGLView(pEGLView);
- // turn on display FPS
- pDirector->setDisplayStats(false);//关闭调试信息
- // set FPS. the default value is 1.0/60 if you don't call this
- pDirector->setAnimationInterval(1.0 / 60);//这里是设置游戏运行的帧数锁定为60,一般牛B机器、牛B的游戏都是锁这个帧数,30帧是低配
- // create a scene. it's an autorelease object
- CCScene *pScene = HelloWorld::scene();
- // run
- pDirector->runWithScene(pScene);
- return true;
- }
- // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
- void AppDelegate::applicationDidEnterBackground() {
- CCDirector::sharedDirector()->stopAnimation();
- // if you use SimpleAudioEngine, it must be pause
- // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
- }
- // this function will be called when the app is active again
- void AppDelegate::applicationWillEnterForeground() {
- CCDirector::sharedDirector()->startAnimation();
- // if you use SimpleAudioEngine, it must resume here
- // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
- }
之后,对整个程序的核心,管理HelloWorld这个场景的HelloWorldScene.cpp进行修改,放上属于我们自己的东西,去掉官方的东西。主要对HelloWorldScene.cpp中的初始化参数bool HelloWorld::init(){}进行修改,其余不要动,整个HelloWorldScene.cpp修改如下:
- #include "HelloWorldScene.h"
- USING_NS_CC;
- CCScene* HelloWorld::scene()
- {
- // 'scene' is an autorelease object
- CCScene *scene = CCScene::create();
- // 'layer' is an autorelease object
- HelloWorld *layer = HelloWorld::create();
- // add layer as a child to scene
- scene->addChild(layer);
- // return the scene
- return scene;
- }
- // on "init" you need to initialize your instance
- bool HelloWorld::init()
- {
- //保留已经存在的初始化部分,不要进行修改,开始
- if ( !CCLayer::init() )
- {
- return false;
- }
- CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
- CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
- //保留已经存在的初始化部分,不要进行修改,结束
- CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 96);//声明一个标签文本Helloworld,其字体为Arial,字号为96
- pLabel->setPosition(ccp(origin.x + visibleSize.width/2,//设置标签文本的位置ccp是必须的,visibleSize.width/2为屏幕的中央,前面的origin.x是必须加上,用于多平台固定好位置
- origin.y + visibleSize.height/2));//y部分同理
- this->addChild(pLabel, 1);//把这个标签文本Helloworld放到场景中,层叠关系为1,相当于css中的z-index,这个数字越大,越在上面
- return true;
- }
- void HelloWorld::menuCloseCallback(CCObject* pSender)
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
- CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
- #else
- CCDirector::sharedDirector()->end();
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
- exit(0);
- #endif
- #endif
- }
利用Ctrl+F5运行,或者点击上方的运行按钮则看到如下效果:
如果在编译的时候,遇到如下图的“LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏”错误:
那不关Cocos2dx的问题,是由于电脑中有多个Visual Studio冲突,或者之前版本的Visual Studio没有卸载干净。当前系统中存在两个cvtres.exe文件,版本不同就会有问题了。我们让VS2010直接使用系统的cvtres.exe程序。重命名或删除:(vs2010安装的位置).\VC\bin\cvtres.exe
如图,我就把cvtres.exe搞成_cvtres.exe完事。
最后总结一下,Cocos2dx的Helloworld比起SSH算是简单多了,代码清晰。关键是注意好版本,正如php5.5之后不支持windows2003这样的问题,你必须搞清楚Visual Studio 2010只能支持Cocos2dx 2.x之类的。就是安装可能有些繁琐,毕竟python环境的安装对于很多人来说比较陌生,Visual Studio 2010的安装的耗时实在是无力吐槽了,再加上Cocos2dx 2.x的编译过程,就是比较久。最主要网上许多蛋疼的资料就不说了……