cocos2d CCSprite не получает координаты - PullRequest
0 голосов
/ 23 марта 2011

решено!

, чтобы получить координаты фрейма спрайтов, используйте sprite.boundingBox.origin.x;

Hello!
Я реализую простой код, но не могу понять его поведение:

anewSprite = [CCSprite spriteWithFile:@"grossini.png"];
anewSprite.position = ccp(80, 80);
[self addChild:anewSprite];


anotherSprite = [CCSprite spriteWithFile:@"grossini.png"];
anotherSprite.position = ccp(300, 80);
[self addChild:anotherSprite];



-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint point= [touch locationInView:[touch view]];
    point = [[CCDirector sharedDirector] convertToGL: point];

    NSLog(@"point x:%f y:%f", point.x, point.y);
    if (CGRectContainsPoint(anewSprite.textureRect, point)){
        NSLog(@"contains point");
    } else {
        NSLog(@"does not contain");
    }
    return  TRUE;
}



-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
    CGPoint point= [touch locationInView:[touch view]];
    point = [[CCDirector sharedDirector] convertToGL: point];

    anewSprite.position = point;
    if (CGRectContainsRect(anewSprite.textureRect, anotherSprite.textureRect) == TRUE) {
        NSLog(@"Intersects");
    }
}

Проблема заключается в следующем:

NSLog(@"anotherSpriteTextureRectOrigin X:%f Y:%f", enemy1.textureRect.origin.x, enemy1.textureRect.origin.y);
NSLog(@"anewSpriteTextureRectOrigin X:%f Y:%f", anewSprite.textureRect.origin.x, anewSprite.textureRect.origin.y);

показывает:
anotherSpriteTextureRectOrigin X: 0,000000 Y: 0,000000
anewSpriteTextureRectOrigin X: 0,000000 Y: 0,000000

Заранее спасибо!

1 Ответ

1 голос
/ 30 марта 2011

Чтобы проверить, пересекается ли один спрайт / содержит другой, вместо sprite.frame.rect используется sprite.boundingBox

Например:

if (CGRectContainsRect(sprite1.boundingBox, sprite2.boundingBox)) {
     NSLog(@"Contains");
} 
...