swift - didBeginContact not called : this instance is unique to me -
so, still experimenting sprite kit first time ever, , test collision. so, searched around bit in apple's documentation, around stack overflow, online tutorials, , other forums. however, unable find tip or code makes doing work. so, here relevant pieces of code:
this code obstacle:
func createobstacle(){ var ball = skshapenode(circleofradius: 20) var width = uint32(self.frame.width) var random_number = arc4random_uniform(width) ball.position = cgpointmake(cgfloat(random_number), frame.height+20) ball.strokecolor = skcolor.blackcolor() ball.glowwidth = 1.0 ball.fillcolor = skcolor.darkgraycolor() ball.physicsbody = skphysicsbody(circleofradius: 20) ball.physicsbody!.affectedbygravity = true ball.physicsbody?.categorybitmask = 6 ball.physicsbody?.dynamic = true self.addchild(ball) }
this relevant code thing collide with:
let circle = skshapenode(circleofradius: 20) circle.physicsbody = skphysicsbody(circleofradius: 20) circle.fillcolor = skcolor.bluecolor() circle.strokecolor = skcolor.bluecolor() circle.glowwidth = 1.0 circle.physicsbody?.categorybitmask = 4 circle.physicsbody?.dynamic = true circle.physicsbody?.affectedbygravity = false
and code contact:
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 == 4 && secondbody.categorybitmask == 6) || (firstbody.categorybitmask == 6 && secondbody.categorybitmask == 4)){ println("hi") }else{println("no")} }
sadly, nothing being printed @ all, something's wrong. idea why doesn't work?
your class should have delegate skphysicscontactdelegate
.
class gamescene: skscene, skphysicscontactdelegate {
in didmovetoview
write this:
physicsworld.contactdelegate = self
edit
define categorybitmask
this
struct physicscategory { static let circlecategory : uint32 = 0b1 // 1 static let ballcategory : uint32 = 0b10 // 2 }
give categorybitmask
circle , ball
circle.physicsbody?.categorybitmask = physicscategory.circlecategory ball.physicsbody?.categorybitmask = physicscategory.ballcategory
then check contact this:
(func didbegincontact(contact: skphysicscontact) { if ((contact.bodya.categorybitmask == 0b1 && contact.bodyb.categorybitmask == 0b10 ) || ( contact.bodya.categorybitmask == 0b1 && contact.bodyb.categorybitmask == 0b1 )) println("contact") } }
sorry typos didnt used editor
Comments
Post a Comment