我无法让飞船和敌人2发生碰撞,它在视觉上或调试区域均未发生碰撞的任何迹象。 两者都是通过屏幕触摸来控制的,而其他则不是。
宇宙飞船将与敌人和金币相撞。敌人2将与敌人碰撞。 我在底部标出了我认为是造成此问题的两个部分。
编码的新手,因此在视觉帮助下进行详尽的解释将不胜感激。
override func didEvaluateActions() {
checkCollisions()
}
func spaceshipHit(goldCoin: SKSpriteNode) {
// added for score jan 13
goldcoinCount += 1
goldCoin.removeFromParent()
}
func spaceshipHit2(enemy: SKSpriteNode) {
enemy.removeFromParent()
}
func spaceshipHit(enemy: SKSpriteNode) {
invincible = true
// added for score attempt jan 13
lives -= 1
print(lives)
enemy.removeFromParent()
let blinkTimes = 10.0
let duration = 3.0
let blinkAction = SKAction.customAction(withDuration: duration) { node, elapsedTime in
let slice = duration / blinkTimes
let remainder = Double(elapsedTime).truncatingRemainder(
dividingBy: slice)
node.isHidden = remainder > slice / 2
}
let setHidden = SKAction.run() { [weak self] in
self?.spaceship.isHidden = false
self?.invincible = false
}
spaceship.run(SKAction.sequence([blinkAction, setHidden]))
}
这是我认为未激活的代码块。
func spaceshipHit(enemy2: SKSpriteNode) {
invincible = true
lives -= 1
print(lives)
let blinkTimes = 10.0
let duration = 3.0
let blinkAction = SKAction.customAction(withDuration: duration) { node, elapsedTime in
let slice = duration / blinkTimes
let remainder = Double(elapsedTime).truncatingRemainder(
dividingBy: slice)
node.isHidden = remainder > slice / 2
}
let setHidden = SKAction.run() { [weak self] in
self?.spaceship.isHidden = false
self?.invincible = false
}
spaceship.run(SKAction.sequence([blinkAction, setHidden]))
}
func checkCollisions() {
var hitGoldCoins: [SKSpriteNode] = []
enumerateChildNodes(withName: "goldCoin") { node, _ in
let goldCoin = node as! SKSpriteNode
if goldCoin.frame.intersects(self.spaceship.frame) {
hitGoldCoins.append(goldCoin)
}
}
for goldCoin in hitGoldCoins {
spaceshipHit(goldCoin: goldCoin)
}
if invincible {
return
}
var hitEnemiesToEnemies: [SKSpriteNode] = []
enumerateChildNodes(withName: "enemy") { node, _ in
let enemy = node as! SKSpriteNode
if node.frame.insetBy(dx: 20, dy: 20).intersects(
self.enemy2.frame) {
hitEnemiesToEnemies.append(enemy)
}
}
for enemy in hitEnemiesToEnemies {
spaceshipHit2(enemy:enemy)
}
var hitEnemies: [SKSpriteNode] = []
enumerateChildNodes(withName: "enemy") { node, _ in
let enemy = node as! SKSpriteNode
if node.frame.insetBy(dx: 20, dy: 20).intersects(
self.spaceship.frame) {
hitEnemies.append(enemy)
}
}
for enemy in hitEnemies {
spaceshipHit(enemy: enemy)
}
if invincible {
return
}
这是另一个可疑的部分。
var hitEnemies2: [SKSpriteNode] = []
enumerateChildNodes(withName: "enemy2") { node, _ in
let enemy2 = node as! SKSpriteNode
if enemy2.frame.intersects(
self.spaceship.frame) {
hitEnemies2.append(enemy2)
}
}
// this is the section that trys to get the ship to hit shadow enemy
/*var hitEnemies2: [SKSpriteNode] = []
enumerateChildNodes(withName: "enemy2") { node, _ in
let enemy2 = node as! SKSpriteNode
if node.frame.insetBy(dx: 20, dy: 20).intersects(
self.spaceship.frame) {
hitEnemies2.append(enemy2)
}
}
*/
for enemy2 in hitEnemies2 {
spaceshipHit(enemy2: enemy2)
}
}
答案 0 :(得分:0)
根据我所看到的,您永远不会将“ enemy2”分配给敌人的名字,所以enumerateChildNodes(withName: "enemy2") { node, _ in
let enemy2 = node as! SKSpriteNode
if enemy2.frame.intersects(
self.spaceship.frame) {
hitEnemies2.append(enemy2)
}
}
应为空,表示未命中任何节点。