Random x-axis positions - (Sprite Kit, IOS)

I am currently facing a problem.

What I'm trying to achieve.

Mating sprites randomly along another x axis and the same y axis, but sprites do not intersect.

Question

Sprites intersect and in some cases spawn more than once in one place.

Your suggestions have been largely received.

what am i doing now

    for (int spawn = 0; spawn < 10; spawn++) {
            SKSpriteNode *obstacle = [self createEnemyRed];
            obstacle.anchorPoint = CGPointMake(0.5, 0.5 );
            obstacle.position = CGPointMake(RandomFloatRange( 0, self.size.width),
                                            bg.size.height/2 );

            obstacle.name = @"obstacle";
            [bg addChild:obstacle];

        }
+4
source share
1 answer

Based on the small amount of code that you showed, it's hard to give you an answer that will suit your needs, but consider using some of these suggestions.

  • Add randomly generated sprites to NSMutableArray.

  • SKNode intersectsNode:, , . , .

  • , , .

+3

Source: https://habr.com/ru/post/1538412/


All Articles