SKSpriteNode Boundaries

Hello, I am making a fairly common begginer game. Where enemy nodes drop down from the top of the view. My only problem is they drop down outside the view, is there a way to set the x coordinates of the SKSpriteNodes to stay in the view its in? This is the code I have now... I have the " - 30 " with the intent of keeping it withing the width of the screen as it falls down. But it still appears outside of the screen...



func spawnEnemys() {

let enemy = SKSpriteNode(imageNamed: "Twister")

let minimumValue = self.size.width/8

let maximumValue = self.size.width - 30

let spawnPoint = UInt32(maximumValue - minimumValue)

enemy.position = CGPoint(x: CGFloat(arc4random_uniform(spawnPoint)), y: self.size.height)

let enemyAction = SKAction.moveToY(-300, duration: 3.0)

enemy.runAction(SKAction.repeatActionForever(enemyAction))

self.addChild(enemy)

I would do it like this:


func spawnEnemys() {
     let enemy = SKSpriteNode(imageNamed: "Twister")
     addChild(enemy)

     let spawnEdgePadding = frame.width * 0.1

     let spawnMinX = spawnEdgePadding
     let spawnMaxX = frame.width - spawnEdgePadding


     let spawnRandomX = CGFloat(arc4random_uniform(UInt32(spawnMaxX - spawnMinX))) + spawnMinX


     enemy.position = CGPoint(x: spawnRandomX, y: frame.height)
     enemy.runAction(SKAction.moveToY(-300, duration: 3.0))
}


Lemme know if it works - I wrote it up cold without checking if it works. I also moved the declaration of the action inside the runAction, for a bit more compactness. Also, I took the repeatForever out, because it's redundant in this case.

SKSpriteNode Boundaries
 
 
Q