SOHOゆいちのサイト
大分間があいてしまいましたが、続きをコツコツと
cocos2dのサンプルを勉強がてら読みといていくメモです。テストコードのActionTestを読んでいってみます。全体の流れは他のテストと同じみたいなので個別の部分を読んでみます。
cocos2dサンプル:ActionTestの内容(2)の続きです。
アクションの繰り返し(無限ループや指定回数ループ)のテスト
// 1秒間で横に150PXへ移動するアクションを定義
id a1 = [CCMoveBy actionWithDuration:1 position:ccp(150,0)];
// 座標60,60へ移動し、上で定義した1秒間に横に150px移動するアクションを3回繰り返すアクションシーケンスを定義
id action1 = [CCRepeat actionWithAction:
[CCSequence actions: [CCPlace actionWithPosition:ccp(60,60)], a1, nil]
times:3];
// 上で定義した1秒間に横に150px移動するアクションとその逆の左に1秒間で150px移動するアクションを永遠に繰り返すアクションシーケンスを定義
id action2 = [CCRepeatForever actionWithAction:
[CCSequence actions: [[a1 copy] autorelease], [a1 reverse], nil]
];
// 各アクションをスプライトへセット
[kathia runAction:action1];
[tamara runAction:action2];
※前にも出てきたが、同じアクションをシーケンスなどにセットする場合には、上の例のようにcopyしてセットする必要がある。これはアクションの継承元の「CCIntervalAction」でアクションの開始してからの時間などの情報を保持しているためです。
3つのアクションで関数を呼び出すテスト。
cocos2dではアクション中に関数を呼び出す方法が3通りあるようです。
違いは、
という3種のようです。
// 2秒間で右に200px動いた後に「callback1」という関数を呼び出すアクションを定義
id action = [CCSequence actions:
[CCMoveBy actionWithDuration:2 position:ccp(200,0)],
[CCCallFunc actionWithTarget:self selector:@selector(callback1)],
nil];
// 2秒間にサイズを2倍にした後に2秒間かけてフェードアウトし、その後「callback2」関数を引数にこのアクションが適用されたスプライトをつけて呼び出す。
id action2 = [CCSequence actions:
[CCScaleBy actionWithDuration:2 scale: 2],
[CCFadeOut actionWithDuration:2],
[CCCallFuncN actionWithTarget:self selector:@selector(callback2:)],
nil];
// 3秒かけて360度回転した跡に2秒かけてフェードアウトし「callback3」関数をスプライトと「0xbebabeba」という変数(テスト用のダミー)
id action3 = [CCSequence actions:
[CCRotateBy actionWithDuration:3 angle:360],
[CCFadeOut actionWithDuration:2],
[CCCallFuncND actionWithTarget:self selector:@selector(callback3:data:) data:(void*)0xbebabeba],
nil];
// 各アクションをスプライトへセット
[grossini runAction:action];
[tamara runAction:action2];
[kathia runAction:action3];
テストのシミュレーター画面上では分かりにくいですが、xcodeのコンソールを見ると
ActionsTest[2168:207] callback 1 called ActionsTest[2168:207] callback 2 called from:<CCSprite = 05AE8150 | Rect = (0.00,0.00,52.00,139.00) | tag = -1 | atlasIndex = -1> callback 3 called from:<CCSprite = 05AE82D0 | Rect = (0.00,0.00,56.00,138.00) | tag = -1 | atlasIndex = -1> with data:bebabeba
とそれぞれの呼出の記録が残っています。
ActionCallFuncNDアクションのテスト(呼び出す際に引数に呼び出し元のスプライトを付け、更に任意の引数をセット出来る。)。
ActionCallFuncクラスとの違いは、actionWithTargetの引数のクラスがselfではなく、スプライトクラスとなっており、他のクラスのメソッドに引数を渡すことができるかを確認しているっぽい。
// 2秒間で右に200px移動した後、grossini(スプライト)のremoveFromParentAndCleanupメソッドを引数にYESを付けて呼び出す。
id action = [CCSequence actions:
[CCMoveBy actionWithDuration:2 position:ccp(200,0)],
[CCCallFuncND actionWithTarget:grossini selector:@selector(removeFromParentAndCleanup:) data:(void*)YES],
nil];
// 定義したアクションをスプライトへセット
[grossini runAction:action];
ここで、「removeFromParentAndCleanup」メソッドはCCNodeクラスのメソッドで、
-(void) removeFromParentAndCleanup:(BOOL)cleanup;
のように定義されていて、このcleanup引数にYESを渡している。
擬似3Dというか、2Dのスプライトを表示しているカメラ位置を変更するアニメーション(表示対象は2Dなので、平面を表示するカメラ位置が変わっているイメージ(分かりにくいですね、、、))
3Dはあまり理解していないので、今度時間があるときに少し勉強するとして、このクラスはザクっとスキップします。
// この前半で、カメラアングルを変えるアクションを定義している
id orbit1 = [CCOrbitCamera actionWithDuration:2 radius:1 deltaRadius:0 angleZ:0 deltaAngleZ:180 angleX:0 deltaAngleX:0];
id action1 = [CCSequence actions:
orbit1,
[orbit1 reverse],
nil ];
id orbit2 = [CCOrbitCamera actionWithDuration:2 radius:1 deltaRadius:0 angleZ:0 deltaAngleZ:180 angleX:-45 deltaAngleX:0];
id action2 = [CCSequence actions:
orbit2,
[orbit2 reverse],
nil ];
id orbit3 = [CCOrbitCamera actionWithDuration:2 radius:1 deltaRadius:0 angleZ:0 deltaAngleZ:180 angleX:90 deltaAngleX:0];
id action3 = [CCSequence actions:
orbit3,
[orbit3 reverse],
nil ];
// カメラアングルの変更アクションを実行
[kathia runAction:[CCRepeatForever actionWithAction:action1]];
[tamara runAction:[CCRepeatForever actionWithAction:action2]];
[grossini runAction:[CCRepeatForever actionWithAction:action3]];
// 右に100px下に100pxを3秒かけて移動するアクションを定義
id move = [CCMoveBy actionWithDuration:3 position:ccp(100,-100)];
// 上のアクションの逆(左に100px、上に100pxを3秒かけて移動するアクション)を定義
id move_back = [move reverse];
// 2つのアクションを連続実行するアクションシーケンスを定義
id seq = [CCSequence actions:move, move_back, nil];
// 上で定義したアクションシーケンスを永遠に実行するアクションシーケンスを定義
id rfe = [CCRepeatForever actionWithAction:seq];
// 定義したアクションシーケンスをスプライトへ適用(同じアクションを適用するためcopyしてautoreleaseしていることに注意)
[kathia runAction:rfe];
[tamara runAction:[[rfe copy] autorelease]];
[grossini runAction:[[rfe copy] autorelease]];
www.bshe.org (created by itassist.info)