サンプルにたくさんあるんですけどコピーするときにはまったのでメモ
1.初期化(なくても動くんですけどー)
1 2 3 4 5 6 7 8 |
-(id) init { if ((self = [super init])) { self.isTouchEnabled = YES; } return self; } |
2.レイヤーを表示した際に、タッチイベントを有効にする
1 2 3 4 5 6 7 |
- (void)onEnter { // タッチを感知してselfに通知。優先順位は0。(ここの部分をinitに書いても動く) [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; //(ここまで) [super onEnter]; } |
3.レイヤーを非表示した際に、タッチイベントを無効にする
1 2 3 4 5 6 |
- (void)onExit { // タッチを感知するのをやめる。 [[CCTouchDispatcher sharedDispatcher] removeDelegate:self]; [super onExit]; } |
4.コールバックを定義
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { CCLOG(@"タッチを開始!!"); return YES; } -(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { CCLOG(@"タッチを完了!!"); } -(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { CCLOG(@"タッチしたまま"); } -(void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event { CCLOG(@"タッチをキャンセル"); } |
こんな感じで,iPhoneにタッチした時のイベントを書くことが出来ます。
ついでにコピーするときに”ccTouchBegan”を書いておかないと
タッチした瞬間に下記のエラーで落ちます。
*** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Layer#ccTouchBegan override me’
コピーするときはちゃんと下までコピーして
意味も調べましょう。(私ですけど…w)