swift3 - Swift 3 SpriteKit Detecting Contacts -
i'm new swift , have been trying figure out how use bitmasks , didbegin(_ contact: skphysicscontact) detect when 2 spaceships touch each other. can't seem figure out how.
here have far:
import spritekit import gameplaykit class gamescene: skscene, skphysicscontactdelegate { let puffercategory: uint32 = 1 << 0; let enemycategory: uint32 = 1 << 1; var spaceship1: skspritenode! var spaceship2: skspritenode! override func didmove(to view: skview) { spaceship1 = skspritenode(imagenamed: "spaceship"); spaceship1.setscale(cgfloat(0.1)) spaceship1.position = cgpoint(x: self.frame.width / 2, y: (self.frame.height / 2)); spaceship1.name = "spaceship1"; spaceship1.physicsbody = skphysicsbody(circleofradius: spaceship1.size.width / 2); spaceship1.physicsbody?.isdynamic = true // apply gravity, friction, , collision spaceship1.physicsbody?.affectedbygravity = false; spaceship1.physicsbody?.allowsrotation = false spaceship1.physicsbody = skphysicsbody(circleofradius: spaceship1.size.width / 2); spaceship1.physicsbody?.categorybitmask = puffercategory spaceship1.physicsbody?.contacttestbitmask = enemycategory spaceship2 = skspritenode(imagenamed: "spaceship"); spaceship2.setscale(cgfloat(0.1)) spaceship2.position = cgpoint(x: self.frame.width / 2, y: (spaceship1.position.y + 300)); spaceship2.name = "puffer2"; spaceship2.physicsbody = skphysicsbody(circleofradius: spaceship2.size.width / 2); spaceship2.physicsbody?.isdynamic = true // apply gravity, friction, , collision spaceship2.physicsbody?.affectedbygravity = false; spaceship2.physicsbody?.allowsrotation = false spaceship2.physicsbody = skphysicsbody(circleofradius: spaceship2.size.width / 2); spaceship2.physicsbody?.categorybitmask = enemycategory spaceship2.physicsbody?.contacttestbitmask = puffercategory addchild(spaceship1) addchild(spaceship2) } func didbegin(_ contact: skphysicscontact) { print("contact") } override func update(_ currenttime: timeinterval) { spaceship1.physicsbody?.affectedbygravity = false; } }
thanks in advance!
i figured out!
here's code :)
import spritekit import gameplaykit class gamescene: skscene, skphysicscontactdelegate { let enemy2category: uint32 = 1 let enemycategory: uint32 = 2 var spaceship1: skspritenode! var spaceship2: skspritenode! override func didmove(to view: skview) { self.physicsworld.contactdelegate = self spaceship1 = skspritenode(imagenamed: "spaceship"); spaceship1.setscale(cgfloat(0.1)) spaceship1.position = cgpoint(x: self.frame.width / 2, y: (self.frame.height / 2)); spaceship1.name = "spaceship1"; spaceship1.physicsbody = skphysicsbody(circleofradius: spaceship1.size.width / 2); spaceship1.physicsbody?.isdynamic = true // apply gravity, friction, , collision spaceship1.physicsbody?.affectedbygravity = false; spaceship1.physicsbody?.allowsrotation = false spaceship1.physicsbody = skphysicsbody(circleofradius: spaceship1.size.width / 2); spaceship1.physicsbody?.categorybitmask = enemy2category spaceship1.physicsbody?.collisionbitmask = enemycategory spaceship1.physicsbody?.contacttestbitmask = enemycategory spaceship2 = skspritenode(imagenamed: "spaceship"); spaceship2.setscale(cgfloat(0.1)) spaceship2.position = cgpoint(x: self.frame.width / 2, y: (spaceship1.position.y + 300)); spaceship2.name = "spaceship2"; spaceship2.physicsbody = skphysicsbody(circleofradius: spaceship2.size.width / 2); spaceship2.physicsbody?.isdynamic = true // apply gravity, friction, , collision spaceship2.physicsbody?.affectedbygravity = false; spaceship2.physicsbody?.allowsrotation = false spaceship2.physicsbody = skphysicsbody(circleofradius: spaceship2.size.width / 2); spaceship2.physicsbody?.categorybitmask = enemycategory spaceship2.physicsbody?.collisionbitmask = enemy2category spaceship2.physicsbody?.contacttestbitmask = enemy2category addchild(spaceship1) addchild(spaceship2) } func didbegin(_ contact: skphysicscontact) { print("contact") } override func update(_ currenttime: timeinterval) { spaceship1.physicsbody?.affectedbygravity = false; } }
i used video help: https://www.youtube.com/watch?v=43hzb4nmqfw
Comments
Post a Comment