gpt4 book ai didi

swift - SpriteKit 粒子文件位置

转载 作者:行者123 更新时间:2023-11-28 08:03:20 25 4
gpt4 key购买 nike

我的 SpriteKit 游戏中爆炸粒子的定位有问题。当导弹与敌人相撞时,我称之为“func 爆炸”,但爆炸不是在被杀死的敌人所在的地方,而是在屏幕中间。我已将“explosion.position”设置为与“enemy.position”相匹配,但爆炸仍然发生在屏幕中间。我该如何更改它以使爆炸位置与被杀死的敌人的位置相同?

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")
}


}

最佳答案

好的,你正在创造很多敌人,你必须获得碰撞位置。所以:

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)
}

关于swift - SpriteKit 粒子文件位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45920679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com