触摸问题建议



大家好,我的代码有问题。我有6个从底部到顶部移动的精灵。3个精灵可以被触摸。当我触碰那个精灵计数器时,它将增加+1,并且那个精灵将被移除。我面临的问题是,当我选择那个精灵时,计数器会增加到1,但如果我在半秒内选择那个精灵两次,计数器就会增加到2。我可以看到在第一次触碰时精灵消失了,但为什么一旦它消失,它仍然可以检测到精灵的边界框(如果在半秒内点击)。

我该如何解决这个问题?我使用cocos2d

p。如果我在半秒后选择精灵,没有问题。

//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;
    }

}

我认为你需要实现一个ccTouchEnded函数,这样你就可以检测触摸已经结束并避免重复触摸

您可以尝试以下几种方法。尝试将[self removeChild:temp cleanup:YES]放入ccTouchesEnded:方法中。我不确定那是否可行。

你可以做的另一件事是禁用触摸半秒。呼叫[self setIsTouchEnabled:NO],等待ccTouchesEnded:延时后设置为yes

希望能有所帮助

最新更新