Cocos2d-xは、iPhone、Android、コンシューマゲーム機、PC、Macなど数多くのデバイスに対応した、ゲーム開発フレームワークです。それに対して、Cocos2dは、iPhone, iPad, Macに特化したフレームワークです。今後のマルチデバイス対応を考えると、Cocos2d-xを使うべきなのかなと思い、それぞれを軽くつかってみたのですが、Cocos2d-xではプロジェクトのビルドにかかる時間が長かったり、開発言語がC++であったりと(Objective-Cやcの経験はそれなりにありますが、C++は10年近くブランクがあります)、あまり効率的に開発できそうにありませんでした。対してCocos2dは、慣れ親しんでいるObjective-cを使え、ビルドも早く(簡単なプロジェクトであれば数秒で動きました)SpriteBuilder との連携も快適で、かなり好印象でした。Androidがメインであれば、Cocos2d-xの方が良さそうですが、iPhoneをメインに開発し、それがAndroidでも動けばよい。といった感じの開発になりそうなので、Cocos2d-xより、Cocos2d+Apportableでの開発が良さそうです。
以下のチュートリアルをやってみましたが、1週間ではなく、1時間で動くところまで作れました。すごくよいチュートリアルでしたよ!
[1: インストール] cocos2d-iPhoneとSpriteBuilderでゲームを1週間で作ろう
cocos2d v2.0.0では、setDisplayFPSではなく、setDisplayStatsを使って表示のON/OFFを切り替えます。
// Display FSP and SPF
[ director_ setDisplayStats: YES ] ;
cocos2dでは、デフォルトの設定のままではマルチタッチイベント(複数の指による同時操作)を処理できません。
AppDelegate.mの
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
メソッドに以下の1行を追加することでマルチタッチイベントを処理できるようになります。
[ glView setMultipleTouchEnabled: YES ] ;
タップを検知設定は、以下の記事も御覧ください。
cocos2d 2.0.0 でタップイベントを検出
cocos2d 2.0.0でタップイベントを検知する方法です。0.99の頃とは2の部分が変わっているのでご注意ください。
1. HogeLayerのinitでisTouchEnabledにYESを設定
- ( id ) init
{
if ( ( self= [ super init] ) ) {
self.isTouchEnabled = YES ;
}
}
2. HogeLayerに以下のメソッドを追加
- ( void ) registerWithTouchDispatcher
{
CCDirector * director = [ CCDirector sharedDirector] ;
[ [ director touchDispatcher] addTargetedDelegate: self priority: 0 swallowsTouches: YES ] ;
}
3. 実際の処理を記述する。
- ( BOOL ) ccTouchBegan: ( UITouch * ) touch withEvent: ( UIEvent * ) event
{
return YES ;
}
- ( void ) ccTouchEnded: ( UITouch * ) touch withEvent: ( UIEvent * ) event
{
}
- ( void ) ccTouchCancelled: ( UITouch * ) touch withEvent: ( UIEvent * ) event
{
}
- ( void ) ccTouchMoved: ( UITouch * ) touch withEvent: ( UIEvent * ) event
{
}
cocos2d v2.0がリリースされていたので、早速インストールしてみた。といっても、cocos2dの最新版をダウンロードしてきて、シェルスクリプトを実行するだけ。簡単です。
1. 最新版のcocos2dをダウンロードする。
http://www.cocos2d-iphone.org/download から、最新のStable版(cocos2d-iphone-2.0.tar.gz)をダウンロードしました。
2. シェルスクリプトを実行する。
ダウンロードしたファイルを解凍し、./install-templates.shを実行しました。
3. 新規プロジェクトを作成
新規プロジェクトの選択肢にcocos2d 2.0が追加されました。
Cocos2dを使って、簡単なボールシミュレータを作ってみました。画面には複数のボールが表示され、iPhoneを傾けるとボールが移動します。ボール間の当たり判定、ボールと壁の当たり判定が実装されています。
プロジェクトはGitHubからダウンロードしてください。
GitHub
https://github.com/realbasic/BallSimulator
ソースコード
主要な部分だけ貼り付けておきます。
- ( id ) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if ( ( self
= [ super init
] ) ) {
objectArray
= [ [ NSMutableArray alloc
] init
] ;
for ( int x
= 0 ; x <
5 ; x
++ ) {
for ( int y
= 0 ; y <
5 ; y
++ ) {
Weight
* weight
= [ [ Weight alloc
] initWithPosition
: ccp
( x
* 25 + 50 , y
* 25 + 50 ) ] ;
[ self addChild
: weight
] ;
[ objectArray addObject
: weight
] ;
}
}
[ [ UIAccelerometer sharedAccelerometer
] setUpdateInterval
: 0.05 ] ;
[ [ UIAccelerometer sharedAccelerometer
] setDelegate
: self
] ;
[ self schedule
: @selector ( nextFrame
: ) ] ;
}
return self;
}
- ( void ) accelerometer
: ( UIAccelerometer
* ) accelerometer
didAccelerate
: ( UIAcceleration
* ) acceleration
{
gx
= - acceleration.y
* 0.02 ;
gy
= acceleration.x
* 0.02 ;
}
- ( void ) nextFrame
: ( ccTime
) dt
{
for ( int i
= 0 ; i <
10 ; i
++ )
for ( Weight
* weight
in objectArray
) {
weight.vy
+= ( ( random
( ) % 100 - 50 ) / 10000.0 ) ;
weight.vx
+= ( ( random
( ) % 100 - 50 ) / 10000.0 ) ;
weight.vx
*= 0.999 ;
weight.vy
*= 0.999 ;
weight.vx
+= gx;
weight.vy
+= gy;
weight.position
= CGPointMake
( weight.position.x
+ weight.vx, weight.position.y
+ weight.vy
) ;
if ( weight.position.x <
10 ) {
weight.vx
= fabs ( weight.vx
) * 0.7 ;
weight.position
= ccp
( 10 , weight.position.y
) ;
} else if ( weight.position.x >
470 ) {
weight.vx
= - fabs ( weight.vx
) * 0.7 ;
weight.position
= ccp
( 470 , weight.position.y
) ;
}
if ( weight.position.y <
10 ) {
weight.vy
= fabs ( weight.vy
) * 0.7 ;
weight.position
= ccp
( weight.position.x,
10 ) ;
} else if ( weight.position.y >
310 ) {
weight.vy
= - fabs ( weight.vy
) * 0.7 ;
weight.position
= ccp
( weight.position.x,
310 ) ;
}
for ( Weight
* weight2
in objectArray
) {
if ( weight
== weight2
) break ;
double dx
= weight2.position.x
- weight.position.x;
double dy
= weight2.position.y
- weight.position.y;
double d
= sqrt ( dx
* dx
+ dy
* dy
) ;
if ( d < D
) {
double dvx
= weight2.vx
- weight.vx;
double dvy
= weight2.vy
- weight.vy;
double p
= ( dvx
* dx
+ dvy
* dy
) / d
* 0.9 ;
if ( p<
0 ) {
double px
= p
* dx
/ d;
double py
= p
* dy
/ d;
weight.vx
+= px;
weight.vy
+= py;
weight2.vx
-= px;
weight2.vy
-= py;
double mx
= - ( D
- d
) * dx
/ d;
double my
= - ( D
- d
) * dy
/ d;
weight.position
= ccp
( weight.position.x
+ mx, weight.position.y
+ my
) ;
weight2.position
= ccp
( weight2.position.x
- mx, weight2.position.y
- my
) ;
}
}
}
}
}
BB Classic
ブロック崩しゲームの新バージョンをリリースいたしました。
バージョンアップの新機能
・難易度調整
・ステージ数が10に増加
・アイテムを実装(ボールが増える)
・GameCenterと連動
無料なので是非遊んでみてください!
cocos2d v2.0-rc0 released
cocos2d v2.0 の rc0(リリース候補版)がリリースされていた。β2と比べて数多くのバグフィックスが行われた他、新iPadのRetinaディスプレイをサポートしたり、1系との互換性を向上させたりしているようだ。
Highlights since v2.0-beta2:
Improved sprite animation format
Easier to migrate to v2.0: Added deprecated classes/methods, base shaders are inline
Added “number of batches” on director’s stats
Improved Menu/Menu item API
Improved RenderTexture
iPad Retina Display support
Many many bug fixes
Full Changelog: CHANGELOG
Migration guide: migrate_to_v2.0
What’s next:
More bug fixes
More bug fixes
More bug fixes
Opportunistic low-risk features
I would appreciate if you could test this version with your game and report any possible issue that you might find.
Thanks!
Cocos2dでアニメーションを実行するにはCCNodeのrunActionを使います。
以下の例では、countDownLabelが、0.4秒かけて、薄く小さくなります。
CCScaleTo * scale = [ [ CCScaleTo alloc] initWithDuration: 0.4 scale: 0.3 ] ;
CCFadeOut * fadeOut = [ [ CCFadeOut alloc] initWithDuration: 0.4 ] ;
[ countDownLabel runAction: scale] ;
[ countDownLabel runAction: fadeOut] ;
iPod(2009年夏モデル)でゲームアプリの動作確認をしたところ、Portrait(縦置)で起動すべきアプリがLandscape(横置)で起動してしまいました。これは、armv6世代の端末では画面回転処理が非常に重いため、無効化されているのが原因です。初期化時に1度だけ回転するのなら処理の重さは特に問題にはならないので、回転処理の有効化で問題は解決しました。
GameConfig.hを以下のように書き換えると、正しく回転処理するようになりました。
#define GAME_AUTOROTATION kGameAutorotationUIViewController
// ARMv6 (1st and 2nd generation devices): Don't rotate. It is very expensive
#elif __arm__
#define GAME_AUTOROTATION kGameAutorotationUIViewController
armv6世代のうち日本国内で販売されたのは、iPhone 3G(2008年7月リリース), iPodTouch 1G(2007年9月リリース)、2G(2008年9月)の3機種です。
投稿ナビゲーション
iPhoneアプリ開発に関する情報を公開中 ソースコードもあるよ。