2012-02-22 152 views
1

如何禁用觸摸後的CCRect/sprite觸摸?如何在一次觸摸後禁用CGRect/Sprite上的觸摸

我在init()方法來設置精靈:使用精靈作爲其參數的位置和大小

[[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:

-(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. 

    } 
} 

我能夠成功點擊這個精靈來讓它播放聲音,但是我無法弄清楚如何在被觸摸後禁用CGRect。我已經嘗試了上面代碼中評論的不同方法。任何想法或提示都表示讚賞!

+0

您是否嘗試在'ccTouchBegan:withEvent:'上返回'NO'? – 2012-02-22 02:50:32

+0

解決了!只要stackoverflow允許我發佈我的解決方案。我幾乎在我的init方法中設置了一個 - (BOOL)已跳到NO,並且在檢查它的什麼時候被點擊時,我也檢查它是否爲!= YES。在這種方法中,我設置爲YES,所以下一次出現時,它會通過。 – rizzlerazzle 2012-02-22 02:59:36

回答

1

行,所以我解決了這個問題。在任何人想知道的情況下,我在我的頭文件中設置了一個 - (BOOL)已打開,並在我的init方法中將其設置爲NO。

當我檢查與接觸點和CGRect的碰撞時,我還檢查是否isTapped!= YES(意味着它尚未被輕敲)。在這個if語句中,我會像通常那樣執行所有操作,但隨後會設置isTapped = YES。現在,當我再次點擊時它會跳過。下面是我的代碼添加位在*的

.h file: 

BOOL isTapped; 

之間,並在.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; }