SOHOゆいちのサイト
cocos2dのサンプルを勉強がてら読みといていくメモです。テストコードのActionTestを読んでいってみます。全体の流れは他のテストと同じみたいなので個別の部分を読んでみます。
cocos2dサンプル:ActionTestの内容(1)の続きです。
指定回数アクションを繰り返すテスト
// 90度まで1秒で回転するアクションを定義 id act1 = [CCRotateTo actionWithDuration:1 angle:90]; // 0度まで1秒で回転するアクションを定義 id act2 = [CCRotateTo actionWithDuration:1 angle:0]; // 2つのアクションを連続実行するアクションシーケンスを定義 id seq = [CCSequence actions:act1, act2, nil]; // シーケンスを永遠に繰り返すアクションシーケンスを定義 id rep1 = [CCRepeatForever actionWithAction:seq]; // シーケンスを10回実行して終了するアクションシーケンスを定義 id rep2 = [CCRepeat actionWithAction:[[seq copy] autorelease] times:10]; // それぞれのアクションをスプライトへ適用 [tamara runAction:rep1]; [kathia runAction:rep2];
ActionRotateToRepeatとほぼ同じ(角度とスピードが違う)
// −20度、20度へそれぞれ0.5秒で回転するアクションシーケンスを定義
id seq = [CCSequence actions:
[CCRotateTo actionWithDuration:0.5f angle:-20],
[CCRotateTo actionWithDuration:0.5f angle:20],
nil];
// 定義したアクションシーケンスを10回繰り返すアクションシーケンスを定義
id rep1 = [CCRepeat actionWithAction:seq times:10];
// 定義したアクションシーケンスを永遠に繰り返すアクションシーケンスを定義
id rep2 = [CCRepeatForever actionWithAction: [[seq copy] autorelease] ];
// それぞれのスプライトへアクションシーケンスをセット
[tamara runAction:rep1];
[kathia runAction:rep2];
ここまででも、何度か登場しましたが、アクションを逆向きに実行するアクションのテスト
// 高さ50pxで2秒間に4回のジャンプで300px横に移動するアクションを定義 id jump = [CCJumpBy actionWithDuration:2 position:ccp(300,0) height:50 jumps:4]; // 定義したアクションを実行後、逆向き // (高さ50pxで2秒間に4回のジャンプで-300px横に移動するアクション)を定義 id action = [CCSequence actions: jump, [jump reverse], nil]; // アクションをスプライトへセット [grossini runAction:action];
指定した時間停止するアクションのテスト、アクションとアクションの間に時間を儲けたい時などに利用する。
// 右方向に1秒間に150px移動するアクションを定義 id move = [CCMoveBy actionWithDuration:1 position:ccp(150,0)]; // 定義したアクションの後、2秒停止して、再度アクションを実行するアクションシーケンスを定義 id action = [CCSequence actions: move, [CCDelayTime actionWithDuration:2], move, nil]; // 定義したアクションシーケンスをセット [grossini runAction:action];
ActionReverseクラスに似ているが、アクションではなくアクションシーケンスをreverseするテスト
// 1秒間に250px横に移動するアクションを定義 id move1 = [CCMoveBy actionWithDuration:1 position:ccp(250,0)]; // 1秒間に上に50px移動するアクションを定義 id move2 = [CCMoveBy actionWithDuration:1 position:ccp(0,50)]; // 定義したシーケンスとreverseを組み合わせたアクションシーケンスを定義 id seq = [CCSequence actions: move1, move2, [move1 reverse], nil]; // 定義したアクションシーケンスと、アクションシーケンスをreverseしたアクションシーケンスを定義 id action = [CCSequence actions: seq, [seq reverse], nil]; // 定義したアクションシーケンスをスプライトへセット [grossini runAction:action];
ActionReverseSequenceに似ているが、CCToggleVisibility、CCHideアクションへreverseを適用するテスト
// 移動のシーケンスを定義 id move1 = [CCMoveBy actionWithDuration:1 position:ccp(250,0)]; id move2 = [CCMoveBy actionWithDuration:1 position:ccp(0,50)]; // 実行するたびに表示非表示を切り替えるアクションを定義 id tog1 = [CCToggleVisibility action]; id tog2 = [CCToggleVisibility action]; // 定義したアクションを組み合わせたアクションシーケンスを定義 id seq = [CCSequence actions: move1, tog1, move2, tog2, [move1 reverse], nil]; // さらに、reverseとCCRepeatを適用(ここまで来るとちょっと入り組んでますね、ざっと流れを追うと) // move1で右へ1秒で250px移動 // tog1で非表示に // move2で消えた状態で上に50px移動 // tog2で表示に // ここから[move1 reverse] // tog2で非表示に // move2で消えた状態で下に50px移動 // tog1で表示に // move1で左に250px移動 // これを3回繰り返す id action = [CCRepeat actionWithAction:[CCSequence actions: seq, [seq reverse], nil] times:3]; // 作成したアクションシーケンスをセット [kathia runAction:action]; // こちらはCCHideを使った例、ここでは触れていないですがCCHideの逆の当然CCShowも用意されています。 // 移動のアクションを定義 id move_tamara = [CCMoveBy actionWithDuration:1 position:ccp(100,0)]; id move_tamara2 = [CCMoveBy actionWithDuration:1 position:ccp(50,0)]; // 非表示にするアクションを定義 id hide = [CCHide action]; // 組み合わせてアクションシーケンスと、そのreverseを定義 id seq_tamara = [CCSequence actions: move_tamara, hide, move_tamara2, nil]; id seq_back = [seq_tamara reverse]; // 同じくアクションシーケンスをセット // 一応追いかけると // move_tamaraで1秒間で右に100px移動 // hideで非表示に // move_tamara2で非表示状態で上に50px移動 // ここからseq_back // move_tamara2で非表示状態で下方向に50px移動 // hideで表示に(reverseかかっているため) // move_tamaraで左に100px移動 [tamara runAction: [CCSequence actions: seq_tamara, seq_back, nil]];
いやー、長い、続きは次回
www.bshe.org (created by itassist.info)