ios - 如何在一次触摸后禁用 CGRect/Sprite 上的触摸

标签 ios cocos2d-iphone sprite touches

如何在触摸 CCRect/sprite 后禁用其触摸?

我在 init 方法中设置了 Sprite :

[[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)];

然后,我使用 Sprite 的位置和大小作为其参数来创建一个 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.

    }
}

我能够成功点击 Sprite 以使其播放声音,但是我只是不知道如何在触摸后禁用 CGRect。我尝试了上面代码中注释的不同方法。任何想法或提示表示赞赏!

最佳答案

这也会对你有帮助

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

关于ios - 如何在一次触摸后禁用 CGRect/Sprite 上的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9387592/

相关文章:

ios - 如何将 NSDictionary 中的对象添加到 UIActionSheet UIButton 列表的 NSArray

ios - swift ios - 如何从 AppDelegate 在 ViewController 中运行函数

python - 无法在 pyglet 中绘制 () Sprite

android - OpenGL/Android 大 Sprite 四边形表现不佳

ios - 区分触摸计数SKSpriteNode

CSS Sprites——它们都可以工作,但只有一个

ios - 有没有办法在 Swift 中从 HealthKit 中提取用户的全名?

iphone - 在 Safari/GMail 中打开自定义 url 链接

ios - 为什么我在 Cocos2D 中的 32 位上下文周围有一条黑线?

iphone - iOS 将坐标从 iPhone 转换为 iPad 兼容性