swift - Collision detection between sprites won't work -
i have 3 moving sprites. 1 ball, 1 line, , 1 triangle. when ball hits line, want score++ , spawn ball addball() function. when ball hits triangle, want end game.
below code. ball passes right through both ball , triangle without collision. can point me in right direction?
import spritekit class gamescene: skscene, skphysicscontactdelegate { var center = skspritenode() var center2 = skspritenode() var centerline = skspritenode() var bg = skspritenode() var bigcircle = skspritenode() let counterclockwise = skaction.rotatebyangle(cgfloat(3.14), duration:1) let clockwise = skaction.rotatebyangle(cgfloat(-3.14), duration:1) var spin = skaction() var score = 0 var setcenter = skaction.rotatebyangle(cgfloat(-1.75), duration:1) var setcenter2 = skaction.rotatebyangle(cgfloat(1.75), duration:1) struct physicscategory { static let none : uint32 = 0 static let : uint32 = uint32.max static let ball : uint32 = 0x1 // 1 static let triangle : uint32 = 0x1 << 1 // 2 static let line : uint32 = 0x1 << 2 // 4 } override func didmovetoview(view: skview) { let value = uiinterfaceorientation.landscapeleft.rawvalue uidevice.currentdevice().setvalue(value, forkey: "orientation") //background var bgtexture = sktexture(imagenamed: "images/bg.png") bg = skspritenode(texture:bgtexture) bg.position = cgpoint(x: cgrectgetmidx(self.frame), y: cgrectgetmidy(self.frame)) bg.zposition = -4 self.addchild(center) //center circle var bigcircletexture = sktexture(imagenamed: "images/bigcircle.png") bigcircle = skspritenode(texture:bigcircletexture) bigcircle.position = cgpoint(x: cgrectgetmidx(self.frame), y: cgrectgetmidy(self.frame)) self.addchild(bigcircle) bigcircle.zposition = -3 //center triangle var centertexture = sktexture(imagenamed: "center.png") center = skspritenode(texture:centertexture) center.position = cgpoint(x: cgrectgetmidx(self.frame), y: cgrectgetmidy(self.frame)) center.zposition = -1 center.physicsbody = skphysicsbody(rectangleofsize: center.size) center.physicsbody?.categorybitmask = physicscategory.triangle self.addchild(center) center.runaction(setcenter) center.removeallactions() spin = clockwise center.runaction(skaction.repeatactionforever(spin)) //center triangle 2 var centertexture2 = sktexture(imagenamed: "center.png") center2 = skspritenode(texture:centertexture) center2.position = cgpoint(x: cgrectgetmidx(self.frame), y: cgrectgetmidy(self.frame)) center2.zposition = -1 center2.physicsbody = skphysicsbody(rectangleofsize: center2.size) center2.physicsbody?.categorybitmask = physicscategory.triangle self.addchild(center) center2.runaction(setcenter2) center2.removeallactions() spin = clockwise center.runaction(skaction.repeatactionforever(spin)) //center line var centerlinetexture = sktexture(imagenamed: "centerline.png") centerline = skspritenode(texture:centerlinetexture) centerline.position = cgpoint(x: cgrectgetmidx(self.frame), y: cgrectgetmidy(self.frame) + center.size.height) centerline.zposition = -2 centerline.physicsbody = skphysicsbody(rectangleofsize: centerline.size) centerline.physicsbody?.categorybitmask = physicscategory.line self.addchild(centerline) spin = clockwise centerline.runaction(skaction.repeatactionforever(spin)) //create balls //ball settings func randomcgfloat() -> cgfloat { return cgfloat(double(arc4random())/double(uint32.max) ) } var ball = skspritenode(imagenamed: "newball.png") var randomball = arc4random_uniform(4) + 1 var moveball = skaction.moveto(cgpointmake(self.size.width/2,self.size.height/2), duration:3.0) ball.physicsbody = skphysicsbody(circleofradius: ball.size.height / 2) ball.physicsbody?.dynamic = false ball.physicsbody?.allowsrotation = false ball.physicsbody?.categorybitmask = physicscategory.ball ball.physicsbody?.collisionbitmask = physicscategory.triangle ball.physicsbody?.contacttestbitmask = physicscategory.triangle ball.physicsbody?.collisionbitmask = physicscategory.line //initial ball if score == 0 { ball.position = cgpointmake(self.size.width/2, self.size.height) self.addchild(ball) ball.runaction(moveball) } //spawning , moving balls func addball() { if randomball == 1 { ball.position = cgpointmake(self.size.width/2, self.size.height) self.addchild(ball) ball.runaction(moveball) } else if randomball == 2 { ball.position = cgpointmake(self.size.width, self.size.height/2) self.addchild(ball) ball.runaction(moveball) } else if randomball == 3{ ball.position = cgpointmake(self.size.width/2, -self.size.height) self.addchild(ball) ball.runaction(moveball) } else if randomball == 4 { ball.position = cgpointmake(self.size.width - self.size.width, self.size.height/2) self.addchild(ball) ball.runaction(moveball) } } func didbegincontact(contact: skphysicscontact) { if contact.bodya.categorybitmask == physicscategory.line { score++ println("\(score)") } else if contact.bodya.categorybitmask == physicscategory.triangle { println("lost") } } } override func touchesbegan(touches: nsset, withevent event: uievent) { /* called when touch begins */ if spin == clockwise { center.removeallactions() centerline.removeallactions() spin = counterclockwise } else { center.removeallactions() centerline.removeallactions() spin = clockwise } center.runaction(skaction.repeatactionforever(spin)) centerline.runaction(skaction.repeatactionforever(spin)) } override func update(currenttime: cftimeinterval) { /* called before each frame rendered */ } }
first, need set skphysicsbody
each object want interact physics (gravity, collision, ...). not case line , triangle.
after that, group (category bitmask) aren't set properly. indeed, 0 << 3
isn't right. might want declare category bitmask :
struct physicscategory { static let none : uint32 = 0 static let : uint32 = uint32.max static let ball : uint32 = 0b1 // 1 static let triangle : uint32 = 0b10 // 2 static let line : uint32 = 0b100 // 4 }
or :
struct physicscategory { static let none : uint32 = 0 static let : uint32 = uint32.max static let ball : uint32 = 0x1 // 1 static let triangle : uint32 = 0x1 << 1 // 2 static let line : uint32 = 0x1 << 2 // 4 }
here table i've made another question, might category bitmask : http://goo.gl/7d8egy
Comments
Post a Comment