所以我在上一个问题的@Fluidity的帮助下添加了这个最近的代码(以保持屏幕边框中的敌人节点),现在我的问题是,使用这个代码,太多的敌人正在倒下,他们&# 39;不要像玩家之前那样与玩家射击的子弹或玩家节点本身发生碰撞。这是一些代码:
func createEnemy() {
let wait:SKAction = SKAction.wait(forDuration: 1.25)
//1.0 = 1.35
let block:SKAction = SKAction.run(launchEnemy)
let seq:SKAction = SKAction.sequence( [ wait, block ])
let repeated:SKAction = SKAction.repeatForever(seq)
self.run(repeated, withKey:"enemyLaunchAction")
self.run(seq, withKey:"EnemySpawnAction")
}
func launchEnemy() {
let EnemyMissile:EnemyClass = EnemyClass()
EnemyMissile.createEnemy("Enemy")
addChild(EnemyMissile)
let enemy = EnemyMissile.EnemyNode
// let randomX = arc4random_uniform( UInt32(screenWidth))
var randomX = CGFloat(arc4random_uniform(UInt32(self.size.width))) // This is not a perfect rando algo.
// Because our random x was from 0 through screenwidth, but .position works
// on -halfWidth through +halfWidth
randomX -= (self.frame.size.width / 2)
// Because spawning on the border will mean that the enemy is HALF out of view:
/*
right border: |
enemy @ frame.maxX: x
- offset: x|
- size.w/2 x |
*/
enemy.position = CGPoint( x: CGFloat(randomX) - (screenWidth / 2), y: screenHeight + 50)
let action = SKAction.move(by: CGVector(dx: 0, dy: -400 + speedScore), duration: 5.0)
enemy.run(SKAction.repeatForever(action))
increaseSpeed()
print("enemy spawned!")
self.run(action, withKey:"LaunchEnemyAction")
let offset = CGFloat(5) // pixels, so enemy will have some spacing between borders.
if randomX > self.frame.maxX - offset - (enemy.size.width / 2) {
randomX = self.frame.maxX - offset - (enemy.size.width / 2)
}
else if randomX < self.frame.minX + offset + (enemy.size.width / 2) {
randomX = self.frame.minX + offset + (enemy.size.width / 2)
}
enemy.position.x = randomX
}
func keepEnemyInBounds() {
// A better way to do this would be to have a dictionary of enemies:
for node in self.children {
guard node.name == "enemy" else { continue }
let enemy = node as! SKSpriteNode
if enemy.position.x > frame.maxX - (enemy.size.width / 2) {
enemy.position.x = frame.maxX - (enemy.size.width / 2)
}
else if enemy.position.x < frame.minX + (enemy.size.width / 2) {
enemy.position.x = frame.minX + (enemy.size.width / 2)
}
}
}
override func update(_ currentTime: TimeInterval) {
launchEnemy()
}
override func didFinishUpdate() {
keepEnemyInBounds()
}
联络听证代码:
func didBegin(_ contact: SKPhysicsContact) {
_ = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if (contact.bodyA.categoryBitMask == BodyType.enemy.rawValue && contact.bodyB.categoryBitMask == BodyType.bullet.rawValue) {
if let missile = contact.bodyA.node! as? EnemyClass {
enemyMissileAndBullet(missile)
}
contact.bodyB.node?.name = "removeNode"
} else if ( contact.bodyA.categoryBitMask == BodyType.bullet.rawValue && contact.bodyB.categoryBitMask == BodyType.enemy.rawValue) {
if let missile = contact.bodyB.node! as? EnemyClass {
enemyMissileAndBullet(missile)
}
contact.bodyA.node?.name = "removeNode"
}
if ( contact.bodyA.categoryBitMask == BodyType.player.rawValue && contact.bodyB.categoryBitMask == BodyType.enemy.rawValue) {
createExplosion(contact.bodyB.node!.position , image:"explosion2")
contact.bodyB.node?.name = "removeNode"
updateScore(1)
subtractHealth()
playSound("shipExplosion")
} else if (contact.bodyA.categoryBitMask == BodyType.enemy.rawValue && contact.bodyB.categoryBitMask == BodyType.player.rawValue) {
createExplosion(contact.bodyA.node!.position , image:"explosion2")
contact.bodyA.node?.name = "removeNode"
updateScore(1)
subtractHealth()
playSound("shipExplosion")
}
}
编辑:
联系人监听器已清理:需要播放器&lt; missle&lt;敌人
func didBegin(_ contact: SKPhysicsContact) {
let bodyA = contact.bodyA.categoryBitMask >= contact.bodyA.categoryBitMask ? contact.bodyA : contact.bodyB
let bodyB = contact.bodyA.categoryBitMask < contact.bodyA.categoryBitMask ? contact.bodyA : contact.bodyB
if contact.bodyA.categoryBitMask == BodyType.bullet.rawValue && contact.bodyB.categoryBitMask == BodyType.enemy.rawValue {
if let missile = contact.bodyA.node! as? EnemyClass {
enemyMissileAndBullet(missile)
}
contact.bodyB.node?.name = "removeNode"
}
else if contact.bodyA.categoryBitMask == BodyType.player.rawValue && contact.bodyB.categoryBitMask == BodyType.enemy.rawValue {
createExplosion(bodyB.node!.position , image:"explosion2")
bodyB.node?.name = "removeNode"
updateScore(1)
subtractHealth()
playSound("shipExplosion")
}
}