I want to choose from 4 enemies using random and present it on scene. For that purpose I've made this:

func enemyPicker() -> SKSpriteNode {
var enemyArray = [mouse, robot, drone, block, bird]
var countArray = UInt32(enemyArray.count)
var pickOneEneny = arc4random_uniform(countArray)
var randomElement = Int(pickOneEnemy)

return enemyArray.randomElement

But Xcode says to me that SKSpriteNode does not have a member named randomElement. And it surely doesn't, but how would I say to my function that I need it to pick and assign that random Int to an actual enemy from array?

My SpriteNodes are declared like this. Example:

var mouse = SKSpriteNode() 
let mouseAtlas = SKTextureAtlas(named: "mouse")
var mouseArray = [SKTexture]()

mouse = SKSpriteNode(texture: mouseArray[0]);
self.mouse.position = CGPointMake(CGRectGetMaxX(self.frame), CGRectGetMidY(self.frame) - 138)
self.mouse.size = CGSizeMake(self.mouse.size.width, self.mouse.size.height + mouse.size.height / 2)

self.mouse.name = "mouse"

I also tried to change -> SKSpriteNode to SKTexture, String and "< T >" and had not any luck with it.


Your problem is in line return enemyArray.randomElement. This sentence tries to access a property called randomElement that your enemyArray has, but you want to get the object inside enemyArray that has the randomElement position, right? Try with this:

return enemyArray[randomElement]

More information about arrays in swift can be found here.


Your Answer

