我正在研究我的游戏,但是当我尝试从敌人中产生敌人时我遇到了问题我只能从场景的顶部和底部产生它们我不知道为什么
这是我的计时器:
override func didMoveToView(view: SKView) {
initialzeGame()
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("spawnEnemy"), userInfo: nil, repeats: true)
}
剩下的就是这个:
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)
}
答案 0 :(得分:0)
反转CGPointMake
中的参数。
enemy.position = CGPointMake(self.frame.size.width/16, CGFloat(Pos))