2

I was wondering if anyone could answer my question. I have created 3 different enemy animations with texture atlas in Sprite-kit and I was wondering if there is a way that an enemy is randomly picked to move towards the player as soon as the game begins.

TextureAtlas = SKTextureAtlas(named: "zombies.atlas")
for i in 1...TextureAtlas.textureNames.count {
    let Z = "z\(i).png"
    zombieArray.append(SKTexture(imageNamed: Z))
}


TextureAtlas = SKTextureAtlas(named: "baldzomb.atlas")
for i in 1...TextureAtlas.textureNames.count {
    let bald = "baldzomb_\(i).png"
    baldArray.append(SKTexture(imageNamed: bald))
}

TextureAtlas = SKTextureAtlas(named: "crawler.atlas")
for i in 1...TextureAtlas.textureNames.count {
    let Name = "crawler_\(i).png"
    crawlerArray.append(SKTexture(imageNamed: Name))

}
sicvayne
  • 620
  • 1
  • 4
  • 15

1 Answers1

2

You can do it like this:

class GameScene: SKScene {


    let storage = [
        SKSpriteNode(color:.brown, size:CGSize(width: 50, height: 50)),
        SKSpriteNode(color:.white, size:CGSize(width: 50, height: 50)),
        SKSpriteNode(color:.black, size:CGSize(width: 50, height: 50)),
        ]

    func getRandomSprite(fromArray array:[SKSpriteNode])->SKSpriteNode{

        return array[Int(arc4random_uniform(UInt32(array.count)))]
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)

        let sprite = getRandomSprite(fromArray: storage)

        if let location = touches.first?.location(in: self){

            if let copy = sprite.copy() as? SKSpriteNode {
                //sprite can have only one parent, so I add a copy, just because of better example
                copy.position = location
                addChild(copy)
            }
        }
    } 
}

So basically you have an array of sprites/textures/whatever and you generate random index based on array length, and return a random element. You can make an extension as well, and do something like yourArray.random.

Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • 1
    @The1steven2 This answer is correct. It respond properly to the randomly picks any of these enemies. I don't understand why you have post the construction of the textureAtlas that should have no bearing on the random selection of enemies. What else are you trying to do? – Alessandro Ornano Jan 17 '17 at 08:15
  • @AlessandroOrnano, the reason why you see the textureAtlas is because I edited the title after my question was answered. – sicvayne Jan 17 '17 at 08:29
  • 1
    Well, you should change also your question because like this it's incomprehensible. Add the new request below your first request otherwise both question and user that answers before your changes are inconsistent – Alessandro Ornano Jan 17 '17 at 08:33
  • 1
    @The1steven2 Generally you shouldnt do that. You should rather ask a new question... – Whirlwind Jan 17 '17 at 08:38
  • got it, i will post a new question and hopefully all this will be a bit more clear – sicvayne Jan 17 '17 at 08:39
  • 1
    Well, but please re-change this question to the correct title. I could not understand what was happening. Thank you. And flag the answer or this thread remain open for no reasons – Alessandro Ornano Jan 17 '17 at 08:40
  • @The1steven2 About moving things towards other (moving) things : http://stackoverflow.com/questions/36230619/how-to-move-enemy-towards-a-moving-player/36235426#36235426 – Whirlwind Jan 17 '17 at 08:41
  • @The1steven2 As Alessandro pointed you may want to undo your edit to make this question useful for future readers. Also please considering accepting this answer if it helped you with your issue. You do that by clicking the check-mark next to the answer. – Whirlwind Jan 17 '17 at 08:50
  • @Whirlwind, already accepted it, sorry about all the trouble. I posted another question and hopefully that will be a bit more clear! :] – sicvayne Jan 17 '17 at 08:56