How to prevent overlapping spawning in a quick sprite set?

If I try to spawn on enemies in 2 specific areas of the screen (top and bottom with a middle section where they cannot spawn) how can I prevent them from spawning on top or too close to each other Others.

My sprites are relatively small for the screen, and the only suggestion I found here is to create an array of possible positions, and every time you use one of those positions, take it off the list, but first of all I don't know what it looked like would, secondly, I have a lot of possible positions, because I am working with a 5x tall sprite and I want them to be able to respawn when this area becomes clear.

My method of choosing top or bottom just picks a random number 1 or 2, and depending on whether I have 2 functions that make them top or bottom.

I just need there to be no two objects in order to spawn a ball diameter apart. Any ideas how to incorporate this into my spawning?

edit:

  //Create your array and populate it with potential starting points
   var posArray = Array<CGPoint>()
   posArray.append((CGPoint(x: 1.0, y: 1.0))
   posArray.append((CGPoint(x: 1.0, y: 2.0))
   posArray.append((CGPoint(x: 1.0, y: 3.0))

   //Generate an enemy by rolling the dice and 
   //remove its start position from our queue
   let randPos = Int(arc4random()) % posArray.count
   posArray[randPos]
   posArray.removeAtIndex(randPos)

   ...

   //Play game and wait for enemy to die
   //Then repopulate the array with that enemy start position
   posArray.append(enemyDude.startPosition)

      

This is a recommendation I found, but it gives the "expected delimiter" errors, which I really don't know how to fix.

Indeed, I would have to make a huge array of possible positions through X and Y, covering all areas, or is there a better way to do this?

+3


source to share


1 answer


Not creating a node on top of another is just enough using intersectsNode(_ node: SKNode) -> Bool

.

As for spawning too close, that's a different story. The only way you can do this is to also have all your current nodes in an array, enumerate the array, and check each node position for the node spawning position. Depending on your parameters, you either spawn or don't create.




I am not familiar with Swift, so you will have to translate the code yourself.

-(void)testMethod {

    // an array with all your current nodes
    NSMutableArray *myArray = [NSMutableArray new];

    // the potential new spawn node with the proposed spawn position
    SKSpriteNode *mySpawnNode = [SKSpriteNode new];

    BOOL tooClose = NO;

    // enumerate your node array
    for(SKSpriteNode *object in myArray) {

        // get the absoulte x and y position distance differences of the spawn node
        // and the current node in the array
        // using absolute so you can check both positive and negative values later
        // in the IF statement
        float xPos = fabs(object.position.x - mySpawnNode.position.x);
        float yPos = fabs(object.position.y - mySpawnNode.position.y);

        // check if the spawn position is less than 10 for the x or y in relation
        // to the current node in the array
        if((xPos < 10) || (yPos < 10))
        tooClose = YES;
    }

    if(tooClose == NO) {
        // spawn node
    }
}

      

Note that the array must be a property and not declared in the scope that I have in this example.

+2


source







All Articles