$ cd #回到家目录,也可以放在别的目录 $ wget https://cocos2d-x.googlecode.com/files/cocos2d-x-2.1.4.zip $ unzip cocos2d-x-2.1.4.zip
$ cd {cocos2d-x}/tools/project-creator
$ ./create_project.py -project MyGame -package com.MyCompany.AwesomeGame -language javascript #创建一个名叫MyGame的跨平台工程,会在{cocos2d-x}/projects/目录下自动生成MyGame工程代码
$ cd ../../projects/MyGame #切换到工程目录
$ rm -rf Resources/ #删除默认生成的资源目录,后面会用到CocosBuilder生成的资源目录
$ cd #回到家目录,也可以放在别的目录 $ git clone https://github.com/cocos2d/CocosBuilder.git $ cd CocosBuilder $ git submodule update --init --recursive $ open ./CocosBuilder/CocosBuilder.Xcodeproj #打开CocosBuilder的Xcode工程





$ cd {cocos2d-x}/projects/MyGame #切换到项目根目录
$ vim Classes/AppDelegate.cpp
最后,修改applicationDidFinishLaunching的内容如下:
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
CCSize designSize = CCSizeMake( 480,320);
CCSize resourceSize = CCSizeMake( 960,640);
CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();
std::vector<std::string> resDirOrders;
TargetPlatform platform = CCApplication::sharedApplication()->getTargetPlatform();
if (platform == kTargetIphone || platform == kTargetIpad)
{
std::vector<std::string> searchPaths = CCFileUtils::sharedFileUtils()->getSearchPaths();
searchPaths.insert(searchPaths.begin(), "Published-iOS");
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
if (screenSize.height == 1536)//iPad-hd
{
CCLog("resources-ipadhd");
resourceSize = CCSizeMake(2048, 1536);
resDirOrders.push_back("resources-ipadhd");
}
else if (screenSize.height == 768)//iPad
{
CCLog("resources-ipad");
resourceSize = CCSizeMake(1024, 768);
resDirOrders.push_back("resources-ipad");
}
else if (screenSize.height == 640)//iPhone 4/4s/5
{
CCLog("resources-iphonehd");
resourceSize = CCSizeMake( 960,640);
resDirOrders.push_back("resources-iphonehd");
}
else//iPhone 3GS
{
CCLog("resources-iphone");
resourceSize = CCSizeMake(480, 320);
resDirOrders.push_back("resources-iphone");
}
}
else if (platform == kTargetAndroid || platform == kTargetWindows)
{
if (screenSize.height > 720)
{
resourceSize = CCSizeMake( 960,640);
resDirOrders.push_back("resources-large");
}
else if (screenSize.height > 568)
{
resourceSize = CCSizeMake(480, 720);
resDirOrders.push_back("resources-medium");
}<
else
{
resourceSize = CCSizeMake(320, 568);
resDirOrders.push_back("resources-small");
}
}
CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(resDirOrders);
pDirector->setContentScaleFactor(resourceSize.width/designSize.width);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder);
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
ScriptingCore* sc = ScriptingCore::getInstance();
sc->addRegisterCallback(register_all_cocos2dx);
sc->addRegisterCallback(register_all_cocos2dx_extension);
sc->addRegisterCallback(register_all_cocos2dx_extension_manual);
sc->addRegisterCallback(register_cocos2dx_js_extensions);
sc->addRegisterCallback(register_CCBuilderReader);
sc->addRegisterCallback(jsb_register_chipmunk);
sc->addRegisterCallback(jsb_register_system);
sc->addRegisterCallback(JSB_register_opengl);
sc->addRegisterCallback(MinXmlHttpRequest::_js_register);
sc->addRegisterCallback(register_jsb_websocket);
sc->start();
CCScriptEngineProtocol *pEngine = ScriptingCore::getInstance();
CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
ScriptingCore::getInstance()->runScript("main.js");
return true;
}
$ open proj.ios/MyGame.Xcodeproj/ #打开iOS工程



$ cd {cocos2d-x}/projects/MyGame/Published-HTML5
$ python -m SimpleHTTPServer &
Serving HTTP on 0.0.0.0 port 8000 ...
$ open {cocos2d-x}/projects/MyGame/MyGame.ccbproj


$ vim {cocos2d-x}/extensions/CCBReader/CCLayerLoader.cpp
#define PROPERTY_TOUCH_ENABLED "isTouchEnabled" #define PROPERTY_ACCELEROMETER_ENABLED "isAccelerometerEnabled" #define PROPERTY_MOUSE_ENABLED "isMouseEnabled"
#define PROPERTY_KEYBOARD_ENABLED "isKeyboardEnabled"
#define PROPERTY_TOUCH_ENABLED "touchEnabled" #define PROPERTY_ACCELEROMETER_ENABLED "accelerometerEnabled" #define PROPERTY_MOUSE_ENABLED "mouseEnabled" #define PROPERTY_KEYBOARD_ENABLED "keyboardEnabled"

MainScene.prototype.onDidLoadFromCCB = function()
{
cc.log('MainScene ccb file has been loaded!');
this.rootNode.onTouchesBegan = function( touches, event) {
// 将触屏开始事件转发给控制器 (this)
this.controller.onTouchesBegan(touches, event);
return true;
};
this.rootNode.onTouchesMoved = function( touches, event) {
// 将触屏移动事件转发给控制器 (this)
this.controller.onTouchesMoved(touches, event);
return true;
};
this.rootNode.onTouchesEnded = function( touches, event) {
// 将触屏结束事件转发给控制器 (this)
this.controller.onTouchesEnded(touches, event);
return true;
};
};
MainScene.prototype.onTouchesBegan = function(touches, event)
{
// 修改文本内容
this.helloLabel.setString("TOUCH START: "+parseInt(touches[0].getLocation().x)+", "+parseInt(touches[0].getLocation().y));
};
MainScene.prototype.onTouchesMoved = function(touches, event)
{
// do some staff here
};
MainScene.prototype.onTouchesEnded = function(touches, event)
{
// do some staff here
};
$ cd {cocos2d-x}/projects/MyGame/Published-HTML5
$ echo '{"data":"I am from the remote server"}' > test.json
// Create callback for button
MainScene.prototype.onPressButton = function()
{
// Rotate the label when the button is pressed //this.helloLabel.runAction(cc.RotateBy.create(1,360)); //新增以下代码 var theHelloLabel = this.helloLabel; var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState==4) {// 4 = "loaded" if (xhr.status==200) {// 200 = "OK" var response = JSON.parse(xhr.responseText); cc.log(response); theHelloLabel.setString(response.data); } else { cc.log("Problem retrieving JSON data:" + xhr.statusText); } } }; //发起一个GET请求 xhr.open("GET", "http://localhost:8000/test.json"); xhr.send(null);
3154

被折叠的 条评论
为什么被折叠?



