gpt4 book ai didi

swift - 如何使用图像帧 sprite kit swift 创建爆炸效果

转载 作者:搜寻专家 更新时间:2023-10-31 22:50:38 28 4
gpt4 key购买 nike

所以我创建了这款游戏,您必须在其中射击物体。现在,我有一个复制对象爆炸的图像集。我想调用这些图像按顺序出现,这样它看起来就像弹丸击中物体后发生的爆炸。显然,必须在射弹击中物体的确切位置调用图像。有谁知道如何做到这一点?这是一些代码。

func projectileDidCollideWithMonster(projectile:SKSpriteNode, monster:SKSpriteNode) {
projectile.removeFromParent()
monster.removeFromParent()

playerScore = playerScore + 1
playerScoreUpdate()

if (playerScore > 100) {
let reveal = SKTransition.crossFadeWithDuration(0.3)
let gameOverScene = GameOverScene(size: self.size, won: true)
self.view?.presentScene(gameOverScene, transition: reveal)
}

}

func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if (firstBody.categoryBitMask & UInt32(laserCategory)) != 0 && (secondBody.categoryBitMask & UInt32(monsterCategory)) != 0 {
projectileDidCollideWithMonster(firstBody.node as SKSpriteNode, monster: secondBody.node as SKSpriteNode)
}
if playerScore > highScore() {
saveHighScore(playerScore)
println("New Highscore = " + highScore().description)
highScoreLabel.text = "Best score: \(highScore().description)"
} else {
println("HighScore = " + highScore().description ) // "HighScore = 100"
}
}


override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)

if node.name == "muteSound" {
if musicIsPlaying == true {
backgroundMusicPlayer.stop()
musicIsPlaying = false
} else if musicIsPlaying == false {
backgroundMusicPlayer.play()
musicIsPlaying = true
}
} else {
let touch = touches.anyObject() as UITouch
let touchLocation = touch.locationInNode(self)

let projectile = SKSpriteNode(imageNamed: "boom")
projectile.setScale(0.6)
projectile.position = player.position
projectile.physicsBody = SKPhysicsBody(circleOfRadius: projectile.size.width/2)
projectile.physicsBody?.dynamic = true
projectile.physicsBody?.categoryBitMask = UInt32(laserCategory)
projectile.physicsBody?.contactTestBitMask = UInt32(monsterCategory)
projectile.physicsBody?.collisionBitMask = 0
projectile.physicsBody?.usesPreciseCollisionDetection = true
// 3 - Determine offset of location to projectile
let offset = touchLocation - projectile.position
// 4 - Bail out if you are shooting down or backwards
if (offset.y < 0) { return }
// 5 - OK to add now - you've double checked position
addChild(projectile)
// 6 - Get the direction of where to shoot
let direction = offset.normalized()
// 7 - Make it shoot far enough to be guaranteed off screen
let shootAmount = direction * 1000
// 8 - Add the shoot amount to the current position
let realDest = shootAmount + projectile.position
// 9 - Create the actions
let actionMove = SKAction.moveTo(realDest, duration: 2.0)
let actionMoveDone = SKAction.removeFromParent()

if !isStarted {
start()
}else{
projectile.runAction(SKAction.sequence([actionMove, actionMoveDone]))

}
}
}
}

func addMonster() {
let monster = SKSpriteNode(imageNamed: "box")
monster.setScale(0.6)
monster.physicsBody = SKPhysicsBody(rectangleOfSize: monster.size)
monster.physicsBody?.dynamic = true
monster.physicsBody?.categoryBitMask = UInt32(monsterCategory)
monster.physicsBody?.contactTestBitMask = UInt32(laserCategory)
monster.physicsBody?.collisionBitMask = 0
monster.name = "box"

var random : CGFloat = CGFloat(arc4random_uniform(320))
monster.position = CGPointMake(random, self.frame.size.height + 10)
self.addChild(monster)
}

最佳答案

对于爆炸,您可以创建一个 SKSpriteNode 来播放您提到的帧:

1. 您将需要图像作为 SKTexture 数组。你说你在图像集中得到了图像,所以最简单的事情可能是使用 for 循环创建一个数组,例如:

// I don't know how many images you've got, so I'll use 10.
var textures: [SKTexture] = []
for i in 0..<10 {
let imageName = "explosion\(i)"
textures.append(SKTexture(imageNamed: imageName))
}

或者,我建议的是创建图像的纹理图集。 (有关纹理图集的更多信息,请参阅 here)要创建图集,请创建一个扩展名为 .atlas 的文件夹,并将所有爆炸图像添加到其中。 (然后将其添加到您的项目中)。这是我编写的一个扩展,用于从纹理图集中获取您的 Sprite ,为动画做好准备:

extension SKTextureAtlas {
func textureArray() -> [SKTexture] {
var textureNames = self.textureNames as! [String]

// They need to be sorted because there's not guarantee the
// textures will be in the correct order.
textureNames.sort { $0 < $1 }
return textureNames.map { SKTexture(imageNamed: $0) }
}
}

下面是如何使用它:

let atlas = SKTextureAtlas(named: "MyAtlas")
let textures = atlas.textureArray()

2. 现在您已经获得了纹理,您需要创建一个 SKSpriteNode 并为其设置动画:

let explosion = SKSpriteNode(texture: textures[0])

let timePerFrame = // this is specific to your animation.
let animationAction = SKAction.animateWithTextures(textures, timePerFrame: timePerFrame)
explosion.runAction(animationAction)

3. 将 Sprite 添加到场景中并正确定位。要将它添加到正确的位置,您可以在 SKPhysicsContact 上使用 contactPoint 变量,在检查它是射弹击中物体后。

func didBeginContact(contact: SKPhysicsContact) {
// Other stuff...

explosion.position = contact.contactPoint
self.addChild(explosion)
}

希望对您有所帮助!

关于swift - 如何使用图像帧 sprite kit swift 创建爆炸效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29807221/

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