2011-11-15 16 views
0

私はタッチを検出できる拡張CCSpriteクラスを作成しようとしています。私はいくつかの調査を行い、このフォーラムスレッドhttp://www.cocos2d-iphone.org/forum/topic/3196(最後の投稿)でAnnyによって作成されたhttp://anny.fm/c/iphone/cocos2d/TouchableSprite/の例を発見しました。Cocos2dのタッチ可能なスプライト

私は私のクラスそうのようなセットアップを持って、これを使用する:等かどうかを検出するよう、

@implementation PianoKey 

-(id)initWithKey:(int)key { 
if((self = [super initWithFile:[NSString stringWithFormat:@"key_%i.png",key]])) { 

} 
return self; 
} 


-(BOOL) tsTouchBegan:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog(@"tsTouchBegan"); 
return YES; 
} 
-(void) tsTouchMoved:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog(@"tsTouchMoved"); 

} 
-(void) tsTouchEnded:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog(@"tsTouchEnded"); 

} 
-(void) tsTouchCancelled:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog(@"tsTouchCancelled"); 
} 

// 
// Utilities. Don't override these unless you really need to. 
// 

-(CGRect) rect { 
CGSize s = [self.texture contentSize]; 
return CGRectMake(-s.width/2, -s.height/2, s.width, s.height); 
} 

-(BOOL) didTouch: (UITouch*)touch { 
return CGRectContainsPoint([self rect], [self convertTouchToNodeSpaceAR: touch]); 
} 

// 
// The actual touch listener functions. Don't override these either. 
// 

-(BOOL) ccTouchBegan:(UITouch*)touch withEvent: (UIEvent*)event { 
    NSLog(@"attempting touch."); 
    if([self didTouch: touch]) { 
     return [self tsTouchBegan:touch withEvent: event]; 
    } 
return NO; 
} 
-(void) ccTouchMoved:  (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch])  [self tsTouchMoved:  touch withEvent: event]; } 
-(void) ccTouchEnded:  (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch])  [self tsTouchEnded:  touch withEvent: event]; } 
-(void) ccTouchCancelled: (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch])  [self tsTouchCancelled: touch withEvent: event]; } 

@end 

は、私は上記の方法を理解して...

ヘッダー

#import <Foundation/Foundation.h> 
#import "cocos2d.h" 

@interface PianoKey : CCSprite <CCTargetedTouchDelegate> { 

} 

-(BOOL) tsTouchBegan:  (UITouch*)touch withEvent: (UIEvent*)event; 
-(void) tsTouchMoved:  (UITouch*)touch withEvent: (UIEvent*)event; 
-(void) tsTouchEnded:  (UITouch*)touch withEvent: (UIEvent*)event; 
-(void) tsTouchCancelled: (UITouch*)touch withEvent: (UIEvent*)event; 

@end 

と実装タッチがスプライトの境界内にありましたが、なぜ私がキーをクリックしても応答が得られないということに固執しています。私はCCTargetdTouchDelegateを実装するのが初めてだから、それはそれと関係があるかもしれないと仮定します。

答えて

0

ターゲットのタッチデリゲートは必要ありません。タッチのNSSetを個々のccTouchXXXメッセージに分割するだけです。実装には、単にCCTargetedTouchHandlerの登録と登録解除が欠けています。それは任意のノードタイプで動作していないだけでCCLayerように、これは通常、フォーカス取得時に行われます。

-(void) onEnter 
{ 
    [super onEnter]; 
    [[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES]; 
} 

-(void) dealloc 
{ 
    [[TouchDispatcher sharedDispatcher] removeDelegate:self]; 
    [super dealloc]; 
} 

ところで、Kobold2Dはすでにタッチがノード上(スプライト、ラベルだったかどうかをテストすることができ、拡張CCNodeクラスを持っています、など)を確認してください:

[node containsTouch:uiTouch]; 

この作業をすべて行う必要はないと言ってください。 ;)

+1

ありがとうございました。うーん、私はKobold2Dをチェックしなければならないだろう。 – Alex

関連する問題