How to create explosion effect using image frames sprite kit swift -


so created game have shoot @ objects. now, have imageset replicates object exploding. call images appear in sequence looks explosion after projectile hits object. images have called @ exact location of projectile hits object. have idea on how make happen? here code.

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 skspritenode, monster: secondbody.node 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) {     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() 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 projectile             let offset = touchlocation - projectile.position             // 4 - bail out if shooting down or backwards             if (offset.y < 0) { return }             // 5 - ok add - you've double checked position             addchild(projectile)             // 6 - direction of shoot             let direction = offset.normalized()             // 7 - make shoot far enough guaranteed off screen             let shootamount = direction * 1000             // 8 - add shoot amount current position             let realdest = shootamount + projectile.position             // 9 - create 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) } 

for explosion create skspritenode play frames mentioned:

1. you're going need images array of sktextures. said you've got images in image set easiest thing may create array using for loop, example:

// don't know how many images you've got, i'll use 10. var textures: [sktexture] = [] in 0..<10 {     let imagename = "explosion\(i)"     textures.append(sktexture(imagenamed: imagename)) } 

alternatively, recommend, create texture atlas of images. (for more information on texture atlases see here) create atlas, make folder extension .atlas , adding explosion images it. (then add project). here's extension wrote sprites out of texture atlas, ready animation:

extension sktextureatlas {     func texturearray() -> [sktexture] {         var texturenames = self.texturenames as! [string]          // need sorted because there's not guarantee         // textures in correct order.         texturenames.sort { $0 < $1 }         return texturenames.map { sktexture(imagenamed: $0) }     } } 

and here's how use it:

let atlas = sktextureatlas(named: "myatlas") let textures = atlas.texturearray() 

2. you've got textures need create skspritenode , animate it:

let explosion = skspritenode(texture: textures[0])  let timeperframe = // specific animation. let animationaction = skaction.animatewithtextures(textures, timeperframe: timeperframe) explosion.runaction(animationaction) 

3. add sprite scene , position correctly. add in correct place use contactpoint variable on skphysicscontact, after checking projectile hitting object.

func didbegincontact(contact: skphysicscontact) {     // other stuff...      explosion.position = contact.contactpoint     self.addchild(explosion) } 

hope helps!


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -