2011-09-25 88 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