I'm working on my game but I face a problem when I try spawn the enemy from the said I can spawn them only from the top and bottom of the scene I don't know why
This is my timer:
override func didMoveToView(view: SKView) {
initialzeGame()
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("spawnEnemy"), userInfo: nil, repeats: true)
}
And this is the rest:
func randomBetweenNumbers(firstNum: CGFloat, secondNum: CGFloat) -> CGFloat {
return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum + secondNum) + min(firstNum, secondNum)
}
func spawnEnemy() {
//supposed to pick random point within the screen width
let Pos = randomBetweenNumbers(0, secondNum: frame.height )
//let PosTwo = randomBetweenNumbers(0, secondNum: frame.height )
let enemy = SKSpriteNode(imageNamed: "zombie") //create a new enemy each time
enemy.position = CGPointMake(CGFloat(Pos), self.frame.size.width/16)
enemy.physicsBody = SKPhysicsBody(circleOfRadius: 7)
enemy.physicsBody?.affectedByGravity = true
enemy.physicsBody?.categoryBitMask = 0
enemy.physicsBody?.contactTestBitMask = 1
enemy.size = CGSize(width: 80, height: 70)
let enmyact = SKAction.moveTo(hero.position, duration: 2)
enemy.runAction(enmyact)
addChild(enemy)
}
enemy.physicsBody?.affectedByGravity = truein conjunction with this :enemy.runAction(enmyact)will produce very weird results. I am not sure how you didn't noticed that already by yourself ? Enemy has a dynamic body, affected by gravity, means, the physics world will apply certain force to this body (and pull it in certain direction). Also, at the same time, you are usingSKActionto move enemies where you want. This will break physics simulation, and you will have a buggy enemy movement. I guess, you want to make enemies unaffected by gravity. - Whirlwind