Iphone Cocos2d检测特定精灵上的触摸

Iphone Cocos2d检测特定精灵上的触摸,iphone,ios,objective-c,cocos2d-iphone,Iphone,Ios,Objective C,Cocos2d Iphone,我有一个精灵,我试图检测用户是否触摸它并发布一个NSLOG。我读过一些cocos2d关于在stackoverflow上检测精灵触摸的帖子,但我有点困惑,不太明白。任何帮助都将不胜感激。我将在下面发布我的精灵 chew = [CCSprite spriteWithFile:@"chew.png" rect:CGRectMake(0, 0, 152, 152)]; chew.position = ccp(100, 300); [self addChild:chew]; 算出 -(void)ccTo

我有一个精灵,我试图检测用户是否触摸它并发布一个NSLOG。我读过一些cocos2d关于在stackoverflow上检测精灵触摸的帖子,但我有点困惑,不太明白。任何帮助都将不胜感激。我将在下面发布我的精灵

chew = [CCSprite spriteWithFile:@"chew.png" rect:CGRectMake(0, 0, 152, 152)];
chew.position = ccp(100, 300);
[self addChild:chew];
算出

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches];
    UITouch* touch = [touches anyObject];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    if (CGRectContainsPoint( [ chew   boundingBox], location)) 
    {
        NSLog(@"touched");
    }
}

将标签值赋予你的灵魂,并在接触事件中比较该标签值

chew = [CCSprite spriteWithFile:@"chew.png" rect:CGRectMake(0, 0, 152, 152)];
chew.position = ccp(100, 300);
chew.tag=12;
[self addChild:chew];

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{
    CGPoint location = [self convertTouchToNodeSpace: touch];

    for (CCSprite *station in _objectList)
    {
        if (CGRectContainsPoint(station.boundingBox, location))
        {
             if(station.tag==12)
             {
                 DLog(@"Found Your sprite");
                 return YES;
             }
        }
    }
    return NO;
}
试试这个

- (BOOL)containsTouchLocation:(UITouch *)touch
{
    if (![self visible]) return NO;

    Boolean isTouch = CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
    return isTouch;
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    if ([self containsTouchLocation:touch] ) 
    {                      
        NSLog(@"Touch find");
        return YES;
    }
    else  
    {
        return NO;
    }
}

当然,在init中,必须设置self.isTouchEnabled=YES

你的问题是什么?请解释什么是_objectList!?在这种情况下
- (BOOL)containsTouchLocation:(UITouch *)touch
{
    if (![self visible]) return NO;

    Boolean isTouch = CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
    return isTouch;
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    if ([self containsTouchLocation:touch] ) 
    {                      
        NSLog(@"Touch find");
        return YES;
    }
    else  
    {
        return NO;
    }
}