クロスプラットフォームを意識して、開発環境の策定に翻弄し、enchant.jsから始まりいろいろ試した結果、最終的にはやはりというかなんというかcocos2d-xに落ち着きました。物理エンジン(box2d)を利用したいです。
これからはcocos2d-xもからめてブログを書いていきたいと思います。
まずは解像度対応から
cocos2dx画面解像度対応
coco2dxのテンプレートで新規プロジェクト作成後、4-inch画面に対応するには、スプラッシュ画面(LunchImages)を設定すれば認識してくれるみたいです。解像度に関しては@2xが推奨されていないようで、フォルダ単位で画像を分けることになります。
あとは、AppDelegate::applicationDidFinishLaunching{} で以下の関数を呼ぶようにすれば画面解像度には対応できました。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//画面解像度対応 | |
// 480x320ベースで実装を行う(iPhone 4inchのみ568x320) | |
// 画像は以下のそれぞれのフォルダより取得 | |
// @2x Resources/Published-iOS/resources-iphonehd | |
// 通常 Resources/Published-iOS/resources-iphone | |
//※AppDelegate::applicationDidFinishLaunching{} に実装する | |
// this->changeDisplayHd(); | |
void AppDelegate::changeDisplayHd() { | |
CCDirector *pDirector = CCDirector::sharedDirector(); | |
CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize(); | |
std::vector<std::string> searchPaths; | |
std::vector<std::string> resDirOrders; | |
TargetPlatform platform = CCApplication::sharedApplication()->getTargetPlatform(); | |
if (platform == kTargetIphone || platform == kTargetIpad){ | |
searchPaths.push_back("Published-iOS"); // Resources/Published-iOS | |
if (screenSize.height > 480){ | |
//Retainaによる幅調整 | |
resDirOrders.push_back("resources-iphonehd"); //Resources/Published-iOS/resources-iphonehd | |
pDirector->setContentScaleFactor(2.f); //2倍のスケールサイズ | |
if (screenSize.width==1136.0 || screenSize.height==1136.0 ) { | |
//4-inch | |
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(568, 320, kResolutionShowAll); | |
} else { | |
//3.5-inch | |
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(480, 320, kResolutionShowAll); | |
} | |
}else{ | |
//通常サイズ | |
resDirOrders.push_back("resources-iphone"); //Resources/Published-iOS/resources-iphone | |
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(480, 320, kResolutionShowAll); | |
} | |
}else{ | |
// iOS以外(Androidなど) | |
searchPaths.push_back("Published-iOS"); // Resources/Published-iOS | |
resDirOrders.push_back("resources-iphone"); | |
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(480, 320, kResolutionExactFit); | |
} | |
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths); | |
CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(resDirOrders); | |
} |
この関数では、以下の2点を設定しています。
- 開発ベースサイズの調整(480x320ベースで実装を行う(4inchのみ568x320))
- イメージ取得フォルダの調整
- @2x -> Resources/Published-iOS/resources-iphoneh
- 通常 -> Resources/Published-iOS/resources-iphone
どうやら、以下の2つのメソッドは[cocos2d-2.0-x-2.0.4]にはないようで、[cocos2d-2.1beta3-x-2.1.1]にて確認がとれました。(ソース読む必要があり、ここにかなりはまった...)
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
CCFileUtils::sharedFileUtils()->setSearchResolutionsOrder(resDirOrders);