所以我有以下代码,用didBegin:contact
var bulletCounter : Int = 0 //not declared inside didbegin just to be clear
nodesToRemove = [SKNode]() // not declared inside didbegin just to be clear
if body1.categoryBitMask == PhysicsCatagory.Bullet && body2.categoryBitMask == PhysicsCatagory.Enemy && (body2.node?.position.y)! < self.size.height{
//if the bullet has hit the enemy
bulletCounter += 1
nodesToRemove.append(body1.node!) // remove bullet always
switch bulletCounter {
case 2:
nodesToRemove.append(body2.node!) // remove enemy
bulletCounter = 0
addScore()
default:break
}
接下来是
override func didFinishUpdate()
{
nodesToRemove.forEach(){$0.removeFromParent()}
nodesToRemove = [SKNode]()
}
现在我的问题是,一旦玩家触及射手节点并使其持续10秒左右,我怎么能创造某种即时杀戮,1个子弹来杀死而不是2
答案 0 :(得分:3)
A&#39;一次性杀人&#39;很容易实现,因为没有必要记住目标已被击中并在每次击中时检查它。
var bulletCounter : Int = 0 //not declared inside didbegin just to be clear
nodesToRemove = [SKNode]() // not declared inside didbegin just to be clear
if body1.categoryBitMask == PhysicsCatagory.Bullet &&
body2.categoryBitMask == PhysicsCatagory.Enemy &&
(body2.node?.position.y)! < self.size.height {
//The bullet has hit the enemy, so remove both and update the score
body1.node.removeFromParent()
body2.node.removeFromParent()
addScore()
}
EDIT使用SKSpriteNode子类的灵活解决方案
好的 - 如果我们将子弹&amp;敌人,我们可以拥有能够做更多(或更少伤害)的子弹,拥有更多或更少健康的敌人等,所有这些都被各个节点整齐地跟踪。
以下是我们的新bullet
课程:
import Foundation
import SpriteKit
class Bullet: SKSpriteNode {
var damageInflicted = 2
var health = 1 { // Possibility for some bullets to be stronger
didSet {
if health == 0 {
removeFromParent()
}
}
}
这是我们的新敌人:
import Foundation
import SpriteKit
class Enemy: SKSpriteNode {
var health = 2 {
didSet {
if health == 0 {
removeFromParent()
}
}
var pointValue = 100 // Points for destroying
}
您的代码现在变为:
if body1.categoryBitMask == PhysicsCatagory.Bullet && body2.categoryBitMask == PhysicsCatagory.Enemy && (body2.node?.position.y)! < self.size.height{
// The bullet has hit the enemy
let enemyNode = contact.bodyA.categoryBitMask == PhysicsCatagory.Enemy ? contact.bodyA.node as! Enemy : contact.bodyB.node as! Enemy
let bulletNode = contact.bodyA.categoryBitMask == PhysicsCatagory.Bullet ? contact.bodyA.node as! Bullet : contact.bodyB.node as! Bullet
enemyNode.health -= bulletNode.damageInflicted
bulletNode.health -= 1
addScore(enemyNode.pointValue)
}
你可以更新单个子弹和敌人&#39; 1}},damageInflicted
和health
视需要。
子弹和敌人将初始化为:
scoreValues
和
let bullet = Bullet(imageNamed: "bullet")
代替:
let enemy = Enemy(imageNamed: "enemy1")
等。
返回SKNode且通常有let bullet = SKSpriteNode(imageNamed: "bullet")
的函数现在将改为... as SKSpriteNode
和as bullet
。