触摸问题建议

发布于 2024-12-06 11:53:10 字数 2492 浏览 1 评论 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;
    }

}

Hi guys i have a issue with my code.
I have 6 sprites that moves from bottom and to the top. 3 of the sprites can be touched. When i touch that sprite counter will increate +1 and that sprite will be removed.
The problem I'm facing is that when i select that sprite the counter increase to 1, but if i select that sprite twice within half secs, counter increase to 2.
i can see on the first touch the sprite disappear, but why once it disappears it can still detect that sprite bounding box(if click within half sec).

How can i solve this problem?
I'm using cocos2d

P.S if i select the sprite after half sec no problem.

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔岸观火 2024-12-13 11:53:10

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

I think what you need to implement a ccTouchEnded function so that you can detect the touch has ended and avoid the duplicate touches.

执笔绘流年 2024-12-13 11:53:10

您可以尝试以下几件事。尝试将 [self removeChild:temp cleanup:YES] 放入 ccTouchesEnded: 方法中。我不确定这是否有效。

您可以做的另一件事是禁用触摸半秒钟。调用[self setIsTouchEnabled:NO],然后在ccTouchesEnded:延迟后将其设置为yes

希望这有帮助

There are a couple of things you could try. Try putting the [self removeChild:temp cleanup:YES] in the ccTouchesEnded: method. Im not sure that would work.

Another thing you could do is disable touch for half a second. Call [self setIsTouchEnabled:NO] and then set it to yes after a delay in ccTouchesEnded:

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文