2012-02-22 7 views
1

タッチした後にCCRect /スプライトのタッチを無効にする方法を教えてください。ワンタッチでCGRect/Spriteのタッチを無効にする方法

私はスプライトを設定するには、私のinitメソッドでき

:私は

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    CGPoint touchLocation = [touch locationInView:[touch view]]; 
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; 
    touchLocation = [self convertToNodeSpace:touchLocation]; 

    dinosaur1 = CGRectMake(dinosaur1_c.position.x - (dinosaur1_c.contentSize.width/2), dinosaur1_c.position.y - (dinosaur1_c.contentSize.height/2), dinosaur1_c.contentSize.width, dinosaur1_c.contentSize.height); 

    if(CGRectContainsPoint(dinosaur1, touchLocation)) 
    { 
     CCLOG(@"Tapped Dinosaur1_c!"); 
     PLAYSOUNDEFFECT(PUZZLE_SKULL); 

     // Code to disable touches?? 
     // Tried to resize CGRect in here to (0, 0, 1, 1) to get it away from the original sprite, but did not work. Still was able to tap on CGRect. 
     // Tried [[CCTouchDispatcher sharedDispatcher] setDispatchEvents:NO];, but disables ALL the sprites instead of just this one. 

    } 
} 

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"testAtlas_default.plist"]; 
      sceneSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"testAtlas_default.png"]; 

[self addChild:sceneSpriteBatchNode z:0]; 

dinosaur1_c = [CCSprite spriteWithSpriteFrameName:@"dinosaur1-c.png"]; 
[sceneSpriteBatchNode addChild:dinosaur1_c]; 

[dinosaur1_c setPosition:CGPointMake(245.0, winSize.height - 174.0)]; 

私は、その後にそのパラメータとして、スプライトの位置とサイズを使用してCGRectを作成しますサウンドを再生するためにスプライトを首尾よくタップすることはできますが、タッチした後でCGRectを無効にする方法はわかりません。上記のコードでコメントしたように、私はさまざまな方法を試しました。どんなアイデアやヒントもありがとう!

+0

あなたは 'ccTouchBegan:withEvent:'に 'NO'を返そうとしましたか? –

+0

解決済み!私はstackoverflowが私にできるようにすぐに私のソリューションを投稿します。私はかなり私のinitメソッドで - (BOOL)isTappedをNOに設定し、それがタップされたときにチェックすると、それも!= YESであるかどうかをチェックします。そして、その方法では、isTappedをYESに設定して、次回に来るときには、それが正しく通過するでしょう。 – rizzlerazzle

答えて

1

[OK]をするのに役立ちますので、私は問題を解決しました。誰かが不思議に思っていた場合は、私のヘッダーファイルに - (BOOL)isTappedを設定し、initメソッドでNOに設定します。

タッチポイントとCGRectとの衝突をチェックするとき、isTapped!= YES(まだタップされていないことを意味する)もチェックします。そのif文では、通常通りにすべての操作を行いますが、isTapped = YESを設定します。もう一度タップするとスキップします。以下は、私のコードは

.h file: 

BOOL isTapped; 

* 'sの間と.mファイル内で追加ビットである:

.M:探しのための

-(id)init 
{ 
    isTapped = NO; 
    // Rest of init method. 
} 

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    CGPoint touchLocation = [touch locationInView:[touch view]]; 
    touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation]; 
    touchLocation = [self convertToNodeSpace:touchLocation]; 

    dinosaur1 = CGRectMake(dinosaur1_c.position.x - (dinosaur1_c.contentSize.width/2), dinosaur1_c.position.y - (dinosaur1_c.contentSize.height/2), dinosaur1_c.contentSize.width, dinosaur1_c.contentSize.height); 

    if(CGRectContainsPoint(dinosaur1, touchLocation) **&& isTapped != YES**) 
    { 
     CCLOG(@"Tapped Dinosaur1_c!"); 
     PLAYSOUNDEFFECT(PUZZLE_SKULL); 

     **isTapped = YES;** 
    } 
    else 
    { 
     CCLog(@"Already Tapped!"); 
    } 
} 

ありがとう!

1

これはまた、あなたの

- (void)selectSpriteForTouch:(CGPoint)touchLocation { 
for (CCSprite *sprite in _projectiles) { 

if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) { 

    NSLog(@"sprite was touched"); 


    [sprite.parent removeChild:sprite cleanup:YES]; 

    [self removeChild:sprite.parent cleanup:YES]; 

} 
    } } 

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 
CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; 
[self selectSpriteForTouch:touchLocation]; 
NSLog(@"touch was _"); 
return TRUE; } 
関連する問題