ios - Implementing collision detections -


basically game consists of basket player moves across screen, aim of game player catch balls falling top of screen. trying add collision detection between balls , basket, facing difficulties namely, implementing collision detection. new swift, sprite kit , app development, please help. appreciated. problem facing balls falling in centre of screen. line of code supposed execute when, ball hits basket , following ball should disappear, please new spritekit.

import spritekit  class gamescene: skscene {  var basket = skspritenode()  let actionmoveright = skaction.movebyx(50, y: 0, duration: 0.2) let actionmoveleft = skaction.movebyx(-50, y: 0, duration: 0.2) //let physicsbody = skphysicsbody(texture: , size: 3500)  override func didmovetoview(view: skview) {     /* setup scene here */      self.physicsworld.gravity = cgvectormake(0.0, -0.5)     self.backgroundcolor = skcolor.whitecolor()     basket = skspritenode(imagenamed: "basket")     basket.setscale(0.5)     basket.position = cgpointmake(self.size.width/2, self.size.height/8)     basket.size.height = 50     basket.size.width = 75     self.addchild(basket)      let updateaction = skaction.runblock {            var choice = arc4random_uniform(3)          switch choice {         case 1 :             var ball1 = skspritenode(imagenamed: "redball")             ball1.position = cgpointmake(self.size.width/3, self.size.height)             ball1.setscale(0.5)             ball1.size.height = 20             ball1.size.width = 30             ball1.physicsbody = skphysicsbody(circleofradius: ball1.size.height / 2.75)             ball1.physicsbody!.dynamic = true             self.addchild(ball1)             println("0")           case 0 :             var ball2 = skspritenode(imagenamed: "redball")             ball2.position = cgpointmake(self.size.width/5, self.size.height)             ball2.setscale(0.5)             ball2.size.height = 20             ball2.size.width = 30             ball2.physicsbody = skphysicsbody(circleofradius: ball2.size.height / 2.75)             ball2.physicsbody!.dynamic = true             self.addchild(ball2)             println("1")           case 2 :             var ball3 = skspritenode(imagenamed: "redball")             ball3.position = cgpointmake(self.size.width*4/5, self.size.height)             ball3.setscale(0.5)             ball3.size.height = 20             ball3.size.width = 30             ball3.physicsbody = skphysicsbody(circleofradius: ball3.size.height / 2.75)             ball3.physicsbody!.dynamic = true             self.addchild(ball3)             println("2")           default :             println("problem")          }        }      let waitduration : nstimeinterval = 1.0     let updateandwaitaction = skaction.sequence([updateaction,skaction.waitforduration(waitduration)])     let repeatforeveraction = skaction.repeatactionforever(updateandwaitaction)     self.runaction(repeatforeveraction) }  override func touchesbegan(touches: nsset, withevent event: uievent) {     /* called when touch begins */      touch: anyobject in touches {         let location = touch.locationinnode(self)         if location.x > basket.position.x {             if basket.position.x < self.frame.maxx {                 basket.runaction(actionmoveright)             }         }         else {             if basket.position.x > self.frame.minx {                 basket.runaction(actionmoveleft)             }         }     } }  override func update(currenttime: cftimeinterval) {   } 

}

for have code typically used in situations user taping something. need use bodya & bodyb , assign bitmasks nodes.

self.basket.physicsbody?.categorybitmask = collidertype.basket.rawvalue self.basket.physicsbody?.contacttestbitmask = collidertype.ball1.rawvalue self.basket.physicsbody?.collisionbitmask = collidertype.ball1.rawvalue self.basket.physicsbody?.contacttestbitmask = collidertype.ball2.rawvalue self.basket.physicsbody?.collisionbitmask = collidertype.ball2.rawvalue self.basket.physicsbody?.contacttestbitmask = collidertype.ball3.rawvalue self.basket.physicsbody?.collisionbitmask = collidertype.ball3.rawvalue 

and every ball too. , in func didbegincontact should xcode do, if have animation or something:

if (contact.bodya.categorybitmask == collidertype.ball1.rawvalue || contact.bodyb.categorybitmask == collidertype.ball1.rawvalue) {         yourgameoverfunc()     }     if (contact.bodya.categorybitmask == collidertype.ball2.rawvalue || contact.bodyb.categorybitmask == collidertype.ball2.rawvalue) {         yourgameoverfunc()     }     if (contact.bodya.categorybitmask == collidertype.ball3.rawvalue || contact.bodyb.categorybitmask == collidertype.ball3.rawvalue) {         yourgameoverfunc()     } 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -