swift - Ball collision and SKSpriteNode(s) collision not detected? -


i can’t find or solution problem. have 4 skspritenodes named: bottomgoalgreen, topgoalgreen, bottomgoalblue, , topgoalblue. have ball skspritenode named ball. first question/problem when have ball collide with, example, topgoalgreen or bottomgoalgreen, want topgoalgreen removed bottomgoalgreen , topgoalblue , bottomgoalblue appear , vice versa. other problem ball , collision. have 2 skaction.movetoy ball can move , down screen. wondering if skactions culprit why collision not happen. hope improved question. if not, try again clarify.

import foundation import spritekit import uikit  struct physicscatagory {  static let bottomgoalgreen : uint32 = 1 static let topgoalgreen : uint32 = 2 static let bottomgoalblue : uint32 = 4 static let topgoalblue : uint32 = 8 static let ball : uint32 = 16  }  class gameplayscene: skscene, skphysicscontactdelegate {  var topgoalgreen = skspritenode(imagenamed: "green goal (top).png") var bottomgoalgreen = skspritenode(imagenamed: "green goal (bottom).png") var topgoalblue = skspritenode(imagenamed: "blue goal (top).png") var bottomgoalblue = skspritenode(imagenamed: "blue goal (bottom).png") var ball = skspritenode(imagenamed: "green ball.png")     override func didmovetoview(view: skview) {     //setup scene      physicsworld.gravity = cgvector.zerovector     physicsworld.gravity = cgvectormake(0, 0)     physicsworld.contactdelegate = self       self.scene?.backgroundcolor = uicolor.blackcolor()     self.scene?.size = cgsize(width: 640, height: 1136)        //top goal green code     topgoalgreen.position = cgpoint (x: self.size.width * 0.5, y: self.size.width * 1.52)     topgoalgreen.physicsbody = skphysicsbody(rectangleofsize: topgoalgreen.size)     topgoalgreen.size = cgsize (width: 300, height: 309)      topgoalgreen.physicsbody?.dynamic = false     topgoalgreen.physicsbody?.categorybitmask = physicscatagory.topgoalgreen     topgoalgreen.physicsbody?.collisionbitmask = 0     topgoalgreen.physicsbody?.contacttestbitmask = physicscatagory.ball     self.addchild(topgoalgreen)       //bottom goal code     bottomgoalgreen.position = cgpoint (x: self.size.width * 0.5, y: self.size.width * 0.252)     bottomgoalgreen.size = cgsize (width: 300, height: 309)      bottomgoalgreen.physicsbody?.dynamic = false     bottomgoalgreen.physicsbody?.categorybitmask = physicscatagory.bottomgoalgreen     bottomgoalgreen.physicsbody?.contacttestbitmask = physicscatagory.ball      self.addchild(bottomgoalgreen)       //ball code     ball.position = cgpoint (x: self.size.width * 0.5, y: self.size.width * 0.9)     ball.physicsbody = skphysicsbody(circleofradius: ball.frame.size.width / 2)     ball.size = cgsize (width: 80, height: 82)      ball.physicsbody?.dynamic = true     ball.physicsbody?.categorybitmask = physicscatagory.ball     ball.physicsbody?.collisionbitmask = 0     ball.physicsbody?.contacttestbitmask = physicscatagory.topgoalgreen      ball.physicsbody?.categorybitmask = physicscatagory.bottomgoalgreen     ball.physicsbody?.collisionbitmask = physicscatagory.bottomgoalgreen     ball.physicsbody?.contacttestbitmask = physicscatagory.bottomgoalgreen      let moveballup = skaction.movetoy(1040, duration: 2)     let moveballdown = skaction.movetoy(90, duration: 2)     let moveupanddown = skaction.sequence([moveballup, moveballdown])     let moveupanddownforever = skaction.repeatactionforever(moveupanddown)     ball.runaction(moveupanddownforever)       self.addchild(ball)   }   func didbegincontact(contact: skphysicscontact) {      var firstbody : skphysicsbody = contact.bodya     var secondbody : skphysicsbody = contact.bodyb      if (((firstbody.categorybitmask == physicscatagory.topgoalgreen) && (secondbody.categorybitmask == physicscatagory.ball)) ||         ((firstbody.categorybitmask == physicscatagory.ball) && (secondbody.categorybitmask == physicscatagory.topgoalgreen))){              collisionwithball(firstbody.node as! skspritenode, ball: secondbody.node as! skspritenode)              nslog("collision!")     }  }  func collisionwithball(topgoalgreen : skspritenode, ball : skspritenode) {   }  override func update(currenttime: cftimeinterval) {     /* called before each frame rendered */  }  } 

i figured out missing. missing skphysicsbody both goals , ball. other commenters trying me.

//top goal green code     topgoalgreen.position = cgpoint (x: self.size.width * 0.5, y: self.size.width * 1.67)     topgoalgreen.size = cgsize (width: 400, height: 80)     topgoalgreen.physicsbody = skphysicsbody(rectangleofsize: cgsize (width: 10, height: 10))     topgoalgreen.physicsbody?.dynamic = false     topgoalgreen.physicsbody?.categorybitmask = physicscatagory.topgoalgreen     topgoalgreen.physicsbody?.contacttestbitmask = physicscatagory.ball     self.addchild(topgoalgreen) 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -