2011-09-25 89 views
0

嗨,大家好我有我的代碼問題。 我有6個從底部移動到頂部的精靈。可以觸摸3個精靈。當我觸摸那個精靈計數器將增加+1,那個精靈將被移除。 我面臨的問題是,當我選擇精靈時,計數器增加到1,但是如果我在半秒內選擇了兩次精靈,計數器會增加到2. 我可以在第一次觸摸時看到精靈消失,但爲什麼一旦消失,它仍然可以檢測到精靈邊界框(如果在半秒內點擊)。觸摸問題的建議

我該如何解決這個問題? 我使用的是cocos2d

P.S如果我在半秒後選擇精靈沒問題。

//other code 

CGPoint gridu2 =ccp(80,-45); 
CGPoint gridu3 =ccp(80,-130); 
CGPoint gridu4 =ccp(80,-215); 
CGPoint gridu7 =ccp(240,-45); 
CGPoint gridu8 =ccp(240,-130); 
CGPoint gridu9 =ccp(240,-215); 

//left grid up 
id actionMoveUp2 = [CCMoveTo actionWithDuration:7 position:ccp(80,winSize.height + 215)]; 
id actionMoveUp3 = [CCMoveTo actionWithDuration:7 position:ccp(80,winSize.height + 130)]; 
id actionMoveUp4 = [CCMoveTo actionWithDuration:7 position:ccp(80,winSize.height + 45)]; 

//right grid down 
id actionMoveDown7 = [CCMoveTo actionWithDuration:7 position:ccp(240,winSize.height +255)]; 
id actionMoveDown8 = [CCMoveTo actionWithDuration:7 position:ccp(240,winSize.height +170)]; 
id actionMoveDown9 = [CCMoveTo actionWithDuration:7 position:ccp(240,winSize.height +85)]; 

correctColor1.position=gridu2; 
correctColor2.position=gridu3; 
correctColor3.position=gridu9; 
random4.position=gridu4; 
random5.position=gridu7; 
random6.position=gridu8; 

[correctColor1 runAction:actionMoveUp2]; 
[correctColor2 runAction:actionMoveUp3]; 
[correctColor3 runAction:actionMoveDown9]; 
[random4 runAction:actionMoveUp4]; 
[random5 runAction:actionMoveDown7]; 
[random6 runAction:actionMoveDown8]; 

[self addChild:correctColor1 z:10 tag:1]; 
[self addChild:correctColor2 z:10 tag:2]; 
[self addChild:correctColor3 z:10 tag:3]; 
[self addChild:random4 z:1 tag:14]; 
[self addChild:random5 z:1 tag:15]; 
[self addChild:random6 z:1 tag:16]; 

-(void)addToScore:(int)number 
{ 
score=score+number; 
[scoreLabel setString:[NSString stringWithFormat:@"%d",score]]; 
} 

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
CGSize winSize =[[CCDirector sharedDirector] winSize]; 
UITouch* myTouch = [touches anyObject]; 
CGPoint location = [myTouch locationInView: [myTouch view]]; 
location = [[CCDirector sharedDirector]convertToGL:location]; 

// to remove touched sprite 
int totalNumberOfItems=3; 
for (int y=1; y < totalNumberOfItems; y++){ 
    CCSprite *temp = (CCSprite*)[self getChildByTag:y]; 

    CGRect correctColor = [temp boundingBox]; 

    if (CGRectContainsPoint(correctColor, location)) { 
     NSLog(@"touched"); 
     [self removeChild:temp cleanup:YES ]; 
     [self addToScore:1]; 
     return; 
    } 

}

回答

1

我認爲你需要實現一個ccTouchEnded功能,這樣就可以檢測觸摸已經結束,並避免重複觸摸的東西。

0

有幾件事你可以嘗試。嘗試將[self removeChild:temp cleanup:YES]置於ccTouchesEnded:方法中。林不知道這將工作。

你可以做的另一件事是禁用半秒鐘的觸摸。呼叫[self setIsTouchEnabled:NO],然後將其設置爲yes延遲後ccTouchesEnded:

希望這有助於

+0

我發現了另一個解決方案。我用我添加了'NSUInteger tapCount = [myTouch tapCount]; 如果(tapCount == 1)' – user5198

+0

亞那更容易做..如果你保持你的手指而不是竊聽,它會工作嗎? – KDaker

+0

是的,工作正常 – user5198