2

触摸后如何禁用 CCRect / 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。我尝试了上面代码中注释的不同方法。任何想法或提示表示赞赏!

4

2 回答 2

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;  }
于 2012-02-22T04:27:56.917 回答
1

好的,所以我解决了这个问题。如果有人想知道,我在头文件中设置了一个 -(BOOL)isTapped 并在我的 init 方法中将其设置为 NO。

当我检查与接触点和 CGRect 的冲突时,我还会检查是否 isTapped != YES(意味着它还没有被点击)。在那个 if 语句中,我像往常一样执行所有操作,然后设置 isTapped = YES。现在当我再次点击时它会跳过。下面是我的代码,在 * 之间添加了位

.h file:

BOOL isTapped;

在 .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!");
    }
}

感谢您的关注!

于 2012-02-22T09:39:56.163 回答