2015-02-11 87 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