<留意事項>
- CCLayer → CCNodeクラス置き換え時、継承元クラスを変更するだけではタッチイベントを受け取ることが出来ずハマったんで留意事項としてまとめておきます。
1.initメソッド:コンテンツサイズ設定
・CCNodeクラス生成後は「self.contentSize」がwidth=0,height=0の状態。
・初期状態ではタッチイベントが領域外のため発生しない。
・コンテンツサイズ設定として画面全体を設定するとタッチイベント取得可能。
2.initメソッド:タッチイベント有効化
・タッチイベントを明示的に有効にする。
3.touchBeganメソッドは必須
・touchMovedイベントのみ使う場合でもtouchBeganメソッドを記述する必要あり。
・touchBeganメソッドを書かないとtouchMovedイベントを取得できない。
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface TitleEffectLayer : CCNode {
}
@end
#import "TitleEffectLayer.h"
@implementation TitleEffectLayer
// ----- イニシャライザ --------------------------------------------
-(id) init
{
if( (self = [super init]) ) {
// コンテンツサイズ設定
self.contentSize = [[CCDirector sharedDirector] viewSize];
// タッチイベント有効化
self.userInteractionEnabled = YES;
}
return self;
}
//----- アクションメソッド -----------------------------------------
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [touch locationInNode:self];
DEBUG_NSLog(@"touchBegan[BEGIN](x=%f , y=%f , cnt=%d)",touchLocation.x,touchLocation.y,touch.tapCount);
}
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [touch locationInNode:self];
DEBUG_NSLog(@"touchMoved[MOVE](x=%f , y=%f , cnt=%d)",touchLocation.x,touchLocation.y,touch.tapCount);
}
@end
ではまた〜
0 件のコメント:
コメントを投稿