我在SpriteKit游戏中遇到爆炸粒子的定位问题。当导弹与敌人发生碰撞时,我称之为“功能爆炸”,但爆炸不是被杀死的敌人所在,而是在屏幕中间。我设置了“爆炸。位置以匹配敌人。位置”但仍然发生爆炸发生在屏幕中间。我怎样才能改变这一点,使爆炸位置与被杀敌人的位置相同?
func spawnExplosion() {
let explosionEmmiter = Bundle.main.path(forResource:
"ParticleExplosion", ofType: "sks")
let explosion = NSKeyedUnarchiver.unarchiveObject(withFile:
explosionEmmiter as! String) as! SKEmitterNode
explosion.position = CGPoint(x: enemy.position.x, y:
enemy.position.y)
explosion.zPosition = 3
explosion.targetNode = self
self.addChild(explosion)
let wait = SKAction.wait(forDuration: 0.5)
let removeExplosion = SKAction.run {
explosion.removeFromParent()
}
self.run(SKAction.sequence([wait, removeExplosion]))
}
func didBegin(_ contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.node?.name == "Enemy" {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.node?.name == "Enemy" && secondBody.node?.name ==
"Missile" {
spawnExplosion()
}
func createEnemiesLeft() {
let enemyTexture1 = SKTexture(imageNamed: "Enemy1")
let enemyTexture2 = SKTexture(imageNamed: "Enemy2")
let enemyTexture3 = SKTexture(imageNamed: "Enemy3")
let enemyTexture4 = SKTexture(imageNamed: "Enemy4")
let enemyTexture5 = SKTexture(imageNamed: "Enemy5")
let enemyTexture6 = SKTexture(imageNamed: "Enemy6")
let enemyTexture7 = SKTexture(imageNamed: "Enemy7")
let enemyTexture8 = SKTexture(imageNamed: "Enemy8")
let enemyTexture9 = SKTexture(imageNamed: "Enemy9")
let enemyTexture10 = SKTexture(imageNamed: "Enemy10")
let animation = SKAction.animate(with: [enemyTexture1,
enemyTexture2, enemyTexture3, enemyTexture4, enemyTexture5,
enemyTexture6, enemyTexture7, enemyTexture8, enemyTexture9,
enemyTexture10], timePerFrame: 0.1)
let makeEnemyRun = SKAction.repeatForever(animation)
let enemy = SKSpriteNode(texture: enemyTexture1)
enemy.name = "Enemy"
enemy.setScale(0.1)
enemy.anchorPoint = CGPoint(x: 0.5, y: 0.5)
enemy.physicsBody = SKPhysicsBody(circleOfRadius: enemy.size.height
/ 2)
enemy.physicsBody?.categoryBitMask = ColliderType.Enemy
enemy.physicsBody?.affectedByGravity = false
enemy.zPosition = 3
enemy.position.y = self.frame.height + 100
enemy.position.x = CGFloat.randomBetweenNumbers(firstNum: -347.5,
secondNum: -85)
enemy.run(makeEnemyRun)
self.addChild(enemy)
let destination = self.frame.height * 0.5
let move = SKAction.moveTo(y: -destination, duration:
TimeInterval(10))
let remove = SKAction.removeFromParent()
enemy.run(SKAction.sequence([move, remove]), withKey:
"MoveEnemiesLeft")
}
func spawnEnemiesLeft() {
let spawn = SKAction.run({ () -> Void in
self.createEnemiesLeft()
})
let delay = SKAction.wait(forDuration: TimeInterval(1))
let sequence = SKAction.sequence([spawn, delay])
self.run(SKAction.repeatForever(sequence), withKey:
"SpawnEnemiesLeft")
}
func createEnemiesRight() {
let enemyTexture1 = SKTexture(imageNamed: "Enemy1")
let enemyTexture2 = SKTexture(imageNamed: "Enemy2")
let enemyTexture3 = SKTexture(imageNamed: "Enemy3")
let enemyTexture4 = SKTexture(imageNamed: "Enemy4")
let enemyTexture5 = SKTexture(imageNamed: "Enemy5")
let enemyTexture6 = SKTexture(imageNamed: "Enemy6")
let enemyTexture7 = SKTexture(imageNamed: "Enemy7")
let enemyTexture8 = SKTexture(imageNamed: "Enemy8")
let enemyTexture9 = SKTexture(imageNamed: "Enemy9")
let enemyTexture10 = SKTexture(imageNamed: "Enemy10")
let animation = SKAction.animate(with: [enemyTexture1,
enemyTexture2, enemyTexture3, enemyTexture4, enemyTexture5,
enemyTexture6, enemyTexture7, enemyTexture8, enemyTexture9,
enemyTexture10], timePerFrame: 0.1)
let makeEnemyRun = SKAction.repeatForever(animation)
let enemy = SKSpriteNode(texture: enemyTexture1)
enemy.name = "Enemy"
enemy.setScale(0.1)
enemy.anchorPoint = CGPoint(x: 0.5, y: 0.5)
enemy.physicsBody = SKPhysicsBody(circleOfRadius: enemy.size.height
/ 2)
enemy.physicsBody?.categoryBitMask = ColliderType.Enemy
enemy.physicsBody?.affectedByGravity = false
enemy.zPosition = 3
enemy.position.y = self.frame.height + 100
enemy.position.x = CGFloat.randomBetweenNumbers(firstNum: 85,
secondNum: 347.5)
enemy.run(makeEnemyRun)
self.addChild(enemy)
let destination = self.frame.height * 0.5
let move = SKAction.moveTo(y: -destination, duration:
TimeInterval(10))
let remove = SKAction.removeFromParent()
enemy.run(SKAction.sequence([move, remove]), withKey:
"MoveEnemiesRight")
}
func spawnEnemiesRight() {
let spawn = SKAction.run({ () -> Void in
self.createEnemiesRight()
})
let delay = SKAction.wait(forDuration: TimeInterval(1))
let sequence = SKAction.sequence([spawn, delay])
self.run(SKAction.repeatForever(sequence), withKey:
"SpawnEnemiesRight")
}
}
答案 0 :(得分:0)
好的,你正在创造很多敌人,你必须得到碰撞位置。这样:
func spawnExplosion(pos:CGPoint) {
let explosionEmmiter = Bundle.main.path(forResource:
"ParticleExplosion", ofType: "sks")
let explosion = NSKeyedUnarchiver.unarchiveObject(withFile:
explosionEmmiter as! String) as! SKEmitterNode
explosion.position = pos
explosion.zPosition = 3
explosion.targetNode = self
self.addChild(explosion)
let wait = SKAction.wait(forDuration: 0.5)
let removeExplosion = SKAction.run {
explosion.removeFromParent()
}
self.run(SKAction.sequence([wait, removeExplosion]))
}
func didBegin(_ contact: SKPhysicsContact) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if contact.bodyA.node?.name == "Enemy" {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if firstBody.node?.name == "Enemy" && secondBody.node?.name ==
"Missile" {
spawnExplosion(pos: firstBody.node.position)
}