如何迅速产生敌舰

时间:2019-03-16 23:37:28

标签: swift sprite-kit

我目前正在尝试创建一个太空飞船游戏,您要射击从屏幕顶部到屏幕底部朝向您的敌人,并且还要避免敌人撞向您。我的敌人没有产卵。

这是我的生成敌人代码:

func spawnEnemy(){

    let randomXStart = random(min:gameArea.minX, max: gameArea.maxX)
    let randomXEnd = random(min: gameArea.minX, max:gameArea.maxX)

    let spawnPoint = CGPoint(x: randomXStart, y: self.size.height * 1.2)
    let endPoint = CGPoint(x: randomXEnd, y: -self.size.height * 0.2)

    let enemy = SKSpriteNode(imageNamed: "enemyShip")
    enemy.setScale(1)
    enemy.position = spawnPoint
    enemy.zPosition = 2
    self.addChild(enemy)

    let moveEnemy = SKAction.move(to: endPoint, duration: 1.5)
    let deleteEnemy = SKAction.removeFromParent()
    let enemySequence = SKAction.sequence([moveEnemy, deleteEnemy])
    enemy.run(enemySequence)


    let dx = endPoint.x - spawnPoint.x
    let dy = endPoint.y - spawnPoint.y
    let amountToRotate = atan2(dy, dx)
    enemy.zRotation = amountToRotate

}

1 个答案:

答案 0 :(得分:0)

您的敌人是生成的,因为您没有设置size值,所以看不到它。默认值为(width: 0, height: 0)

enemy.size = CGSize(width: 100, height: 100)

另外,检查enemy对象是否产生的另一种方法是查看节点计数,以查看调用spawnEnemy函数后节点计数是否增加。

为此,您需要拥有view.showsNodeCount = true