如何使用镜框精灵套件swift创建爆炸效果
所以我创造了这个游戏,你必须向物体射击。现在,我有一个图像集,可以复制爆炸的物体。我想把这些图像称为一个序列,这样它看起来像是在射弹击中物体后发生的爆炸。显然,必须在弹丸撞击物体的确切位置调用图像。有人知道如何做到这一点吗?下面是一些代码如何使用镜框精灵套件swift创建爆炸效果,swift,Swift,所以我创造了这个游戏,你必须向物体射击。现在,我有一个图像集,可以复制爆炸的物体。我想把这些图像称为一个序列,这样它看起来像是在射弹击中物体后发生的爆炸。显然,必须在弹丸撞击物体的确切位置调用图像。有人知道如何做到这一点吗?下面是一些代码 func projectileDidCollideWithMonster(projectile:SKSpriteNode, monster:SKSpriteNode) { projectile.removeFromParent() monste
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)
}
func projectlediddiclikewithmonster(投射物:SKSpriteNode,怪物:SKSpriteNode){
projective.removeFromParent()
monster.removeFromParent()
playerScore=playerScore+1
PlayerCoreUpdate()
如果(playerScore>100){
let reveal=SKTransition.crossFadeWithDuration(0.3)
let gameOverScene=gameOverScene(大小:self.size,赢得:true)
self.view?.presentScene(游戏场景、过渡:显示)
}
}
func didBeginContact(联系人:SKPhysicsContact){
var firstBody:SKPhysicsBody
var第二个主体:SKPhysicsBody
如果contact.bodyA.categoryBitMaskhighScore(){
saveHighScore(playerScore)
println(“新建Highscore=“+Highscore().说明”)
highScoreLabel.text=“最佳分数:\(highScore().description)”
}否则{
println(“HighScore=“+HighScore().description)/“HighScore=100”
}
}
覆盖func TOUCESBERTED(触摸:NSSet,withEvent事件:UIEvent){
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
设node=self.nodeAtPoint(位置)
如果node.name==“muteSound”{
如果musicIsPlaying==真{
BackgroundMusicLayer.stop()
音乐演奏=错误
}否则,如果MusicPlaying==false{
背景音乐播放器。播放()
音乐演奏=真
}
}否则{
让touch=touch.anyObject()作为UITouch
让touchLocation=touch.locationInNode(自身)
let抛射物=SKSpriteNode(图像名为:“boom”)
射弹.设置刻度(0.6)
投射物位置=玩家位置
射弹.physicsBody=SKPhysicsBody(圆形半径:射弹.尺寸.宽度/2)
射弹.physicsBody?.dynamic=真
射弹.physicsBody?.categoryBitMask=UInt32(激光分类)
射弹.physicsBody?.contactTestBitMask=UInt32(怪物类别)
射弹.physicsBody?.collisionBitMask=0
射弹.physicsBody?.usesPreciseCollisionDetection=true
//3-确定位置对射弹的偏移
let offset=接触位置-投射物位置
//4-如果你正在向下射击或向后射击,则进行保释
if(offset.y<0){return}
//5-现在可以添加了-您已经仔细检查了位置
addChild(射弹)
//6-确定拍摄地点的方向
let direction=offset.normalized()
//7-拍摄到足够远的距离,保证在屏幕外拍摄
数量=方向*1000
//8-将放炮量添加到当前位置
让realDest=射弹数量+射弹位置
//9-创建操作
let actionMove=SKAction.moveTo(realDest,持续时间:2.0)
让actionMoveDone=SKAction.removeFromParent()
如果!开始了{
开始()
}否则{
投射物.runAction(SKAction.sequence([actionMove,actionMoveDone]))
}
}
}
}
func addMonster(){
让怪物=SKSpriteNode(图像名为:“盒子”)
怪物。设置刻度(0.6)
monster.physicsBody=SKPhysicsBody(矩形尺寸:monster.size)
monster.physicsBody?.dynamic=true
monster.physicsBody?.categoryBitMask=UInt32(monster类别)
monster.physicsBody?.contactTestBitMask=UInt32(激光分类)
monster.physicsBody?.collisionBitMask=0
monster.name=“box”
var random:CGFloat=CGFloat(arc4random_统一(320))
monster.position=CGPointMake(随机,self.frame.size.height+10)
self.addChild(怪物)
}
您可以创建一个播放您提到的帧的SKSpriteNode
:
1。您需要图像作为SKTexture
s的数组。您说过您在图像集中有图像,因此最简单的方法可能是使用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))
}
下面是如何使用它:
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.将精灵添加到场景中并正确定位。要将其添加到正确的位置,您可以使用skphysiccontact
上的contactPoint
变量,检查是否是射弹击中物体
func didBeginContact(contact: SKPhysicsContact) {
// Other stuff...
explosion.position = contact.contactPoint
self.addChild(explosion)
}
希望有帮助
func didBeginContact(contact: SKPhysicsContact) {
// Other stuff...
explosion.position = contact.contactPoint
self.addChild(explosion)
}