我写了子弹类,声明了需要编写的所有内容,并且我从setupEnemy方法获取了有关敌人及其位置的信息,但子弹从未产生。
func firedBullet(){
let enemy = spawnEnemy()
let bullet = SKSpriteNode(imageNamed: "enemyBullet")
bullet.name = "enemyBul"
bullet.setScale(1)
bullet.position = CGPoint(x:enemy.position.x,y:enemy.position.y)
bullet.zPosition = 1
bullet.physicsBody = SKPhysicsBody(rectangleOf: bullet.size)
bullet.physicsBody!.affectedByGravity = true
bullet.physicsBody!.isDynamic = false
bullet.physicsBody!.categoryBitMask = 3
bullet.physicsBody!.collisionBitMask = 0
bullet.physicsBody!.contactTestBitMask = 2
self.addChild(bullet)
let moveBullet = SKAction.moveTo(y: self.size.height + 100, duration: 1)
let deleteBullet = SKAction.removeFromParent()
let bulletSequence = SKAction.sequence([moveBullet,deleteBullet])
bullet.run(bulletSequence)
}
这是我的敌人,没有错误弹出,指出需要解决的任何编码错误,但子弹从未被敌人朝玩家发射。
各种敌人:[SKSpriteNode] = []
func spawnEnemy() -> SKSpriteNode{
let enemy = SKSpriteNode(imageNamed: "enemyShip")
enemy.setScale(1)
enemy.zPosition = 2
enemy.name = "Enemy"
enemy.physicsBody = SKPhysicsBody(rectangleOf: enemy.size)
enemy.physicsBody!.affectedByGravity = false
enemy.physicsBody!.categoryBitMask = 2
enemy.physicsBody!.collisionBitMask = 0
enemy.physicsBody!.contactTestBitMask = 1 | 3
return enemy
}
func setupEnemy() {
let enemy = spawnEnemy()
let randomXStart = random(min: gameArea.minX, max: gameArea.maxX)
let randomXEnd = random(min: gameArea.minX, max: gameArea.maxX)
let startPoint = CGPoint(x: randomXStart, y: self.size.height * 1.2)
let endPoint = CGPoint(x: randomXEnd, y: -self.size.height * 0.2)
enemy.position = startPoint
let moveEnemy = SKAction.move(to: endPoint, duration: 1.5)
let deleteEnemy = SKAction.removeFromParent()
let enemySequence = SKAction.sequence([moveEnemy, deleteEnemy])
if currentGameState == gameState.inGame{
enemy.run(enemySequence)
}
let dx = endPoint.x - startPoint.x
let dy = endPoint.y - startPoint.y
let amountToRotate = atan2(dy,dx)
enemy.zRotation = amountToRotate
self.addChild(enemy)
enemies.append(enemy)
}