2015-02-11 80 views
0

我試圖在我的應用程序中創建10個氣球。我創建一個隨機CGPoint來設置我的節點的位置,並檢查,如果已經存在一個節點在這一點上,我再試一次。NodesAtPoint找不到節點

我不知道爲什麼,氣球一直放在另一個氣球上。 我所做的任何想法都是錯誤的?

這裏是我的代碼:

-(void)gerarBaloes:(int)quantidade 
{ 
    balloons = [NSMutableArray new]; 
    u_int32_t maxHorizontal = 900; 
    u_int32_t minHorizontal = 100; 
    u_int32_t maxVertical = 650; 
    u_int32_t minVertical = 100; 
    for (int i=1; i<= quantidade; i++) { 
     CGPoint posicao = CGPointMake(arc4random_uniform(maxHorizontal - minHorizontal + 1) + minHorizontal, arc4random_uniform(maxVertical - minVertical + 1) + minVertical); 
     while (![self validPosition:posicao]) { 
      posicao = CGPointMake(arc4random_uniform(maxHorizontal - minHorizontal + 1) + minHorizontal, arc4random_uniform(maxVertical - minVertical + 1) + minVertical); 
     } 
     balao* nBalao = [[balao alloc]initWithPosition:posicao]; 
     SKLabelNode* numero = [[SKLabelNode alloc]initWithFontNamed:@"Arial Rounded MT Bold"]; 
     numero.text = [NSString stringWithFormat:@"%d",i]; 
     numero.zPosition = 1; 
     nBalao.body.zPosition = 0; 
     [nBalao addChild:numero]; 
     nBalao.body.name = @"balloon"; 
     [balloons addObject:nBalao]; 
     [self addChild:nBalao]; 
    } 
} 

這是我的驗證方法:

-(BOOL)validPosition:(CGPoint)position 
{ 
    NSArray* nodes = [self nodesAtPoint:position]; 
    NSLog(@"total de nodes %d",nodes.count); 
    if (nodes.count > 0) { 
     for (SKNode* n in nodes) { 
      if ([n isKindOfClass:[balao class]]) { 
       return NO; 
      } 
     } 
     return YES; 
    }else{ 
     return YES; 
    } 
} 

Balao.m類:

@implementation balao 

-(id)initWithPosition:(CGPoint)pos 
{ 
    if (self = [super init]) { 
     self.position = pos; 
     int n = arc4random_uniform(4); 
     _balloonAtlas = [SKTextureAtlas atlasNamed:[NSString stringWithFormat:@"balloon%d",n]]; 
     _explosionFrames = [NSMutableArray array]; 
     int numImages = _balloonAtlas.textureNames.count; 
     for (int i=1; i<= numImages; i++){ 
      NSString *textureName = [NSString stringWithFormat:@"balloon_%d",i]; 
      SKTexture *temp = [_balloonAtlas textureNamed:textureName]; 
      [_explosionFrames addObject:temp]; 
     } 
     SKTexture *textInicial = [_explosionFrames[0] copy]; 
     [_explosionFrames removeObjectAtIndex:0]; 
     self.body = [SKSpriteNode spriteNodeWithTexture:textInicial]; 
     [self addChild:_body]; 
    } 
    return self; 
} 

- (void)explodeBalloon 
{ 
    SKAction* explosao = [SKAction animateWithTextures:_explosionFrames timePerFrame:0.02f resize:NO restore:YES]; 
    [self.body runAction:explosao completion:^(void){ 
     [self removeFromParent]; 
    }]; 
    [self runAction:[SKAction playSoundFileNamed:@"popSound.mp3" waitForCompletion:NO]]; 
} 

這是怎樣的氣球出現:

Balloons

編輯: 我試着用CGRectCointainsPoint建議的方法,但它沒有工作。 :/

enter image description here

+0

從我所知道的情況來看,您的位置檢查只會阻止新氣球的中心位於現有氣球之內,實際氣球形狀仍然能夠重疊。 – 2015-02-11 16:35:28

+0

我該如何檢查整個氣球?所以它總是可以放置在沒有氣球的區域。 – 2015-02-11 16:41:00

回答

1

在給定點檢查nodes是不足以防止重疊。您需要檢查點是否落在另一個氣球的框架內。您可以修改函數這樣

-(BOOL)validPosition:(CGPoint)position 
{ 
    NSArray* nodes = [self children]; 
    if (nodes.count > 0) { 
     for (SKNode* n in nodes) { 
      if ([n isKindOfClass:[balao class]]) { 
       if (CGRectContainsPoint([n getBalloonFrame], position)) // Changed line 
       { 
        return NO 
       } 
      } 
     } 
     return YES; 
    } 
    return YES; 

} 

CGRectContainsPoint檢查一個給定的矩形是否包含一個點。

在balao類中定義了以下函數。還要注意上面代碼中已更改的行。

-(void)getBalloonFrame 
{ 
    return CGRectOffset(self.body.frame, self.frame.origin.x, self.frame.origin.y) 
} 
+0

我試過了,但它沒有工作。我上傳了發生了什麼的截圖。 – 2015-02-11 17:32:13

+0

可以顯示balao類的代碼嗎? – rakeshbs 2015-02-11 17:35:22

+0

我在帖子中加入了它。 – 2015-02-11 17:38:21