collision detection - How to detect contact regarding sprite texture -
i have bullet should fired @ block. bullet has 6 different random textures mimic different bullets. , block has 3 different textures chosen randomly there 3 different blocks. want specify in code if bullet texture red, , block texture red score should increase, if bullet red , block green game over. don't know how tell game in didbegincontact.
by have this: in gamescene & didmovetoview:
struct physicscategory { static let none : uint32 = 0 static let : uint32 = uint32.max static let cgyblock : uint32 = 0b1 static let bullet : uint32 = 0b10 } bullet.physicsbody = skphysicsbody(texture: bullet.texture, size: self.bullet.size) bullet.physicsbody?.categorybitmask = physicscategory.bullet bullet.physicsbody?.contacttestbitmask = physicscategory.cgyblock bullet.physicsbody?.collisionbitmask = physicscategory.none bullet.physicsbody?.affectedbygravity = false bullet.physicsbody?.usesprecisecollisiondetection = true
in didbegincontact:
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 & physicscategory.cgyblock != 0) && (secondbody.categorybitmask & physicscategory.bullet != 0)) //and here suppose need implement somehow // && (bullet.texture = "redbullet") && (cgyblock.texture = "greenblock" || "blackblock") { gameover() }
but know not work. tried make switch statement inside curly brackets , didn't work either. how implement that?
update: how block made:
var cgyblock = skspritenode() let cgyarray = ["cyanbox", "greenbox", "yellowbox"] func addcgyline () { cgyblock = skspritenode(imagenamed: "cyanbox") var randomcgy = int(arc4random_uniform(3)) cgyblock.texture = sktexture(imagenamed: cgyarray[randomcgy]) cgyblock.physicsbody = skphysicsbody(texture: cgyblock.texture, size: cgyblock.size) cgyblock.physicsbody?.dynamic = true cgyblock.physicsbody?.categorybitmask = physicscategory.cgyblock cgyblock.physicsbody?.contacttestbitmask = physicscategory.bullet cgyblock.physicsbody?.collisionbitmask = physicscategory.none cgyblock.position = cgpointmake(size.width + cgyblock.size.width/2, cgrectgetmidy(self.frame) + 60) addchild(cgyblock) let actionmove = skaction.moveto(cgpoint(x: -cgyblock.size.width/2, y: cgrectgetmidy(self.frame) + 60), duration: 3) let actiondone = skaction.removefromparent() cgyblock.runaction(skaction.sequence([actionmove, actiondone])) skactiontimingmode.easeout }
and runaction in didmovetoview.
bullets:
var cannon = skspritenode(imagenamed: "cannon") var bulletincannon = skspritenode() var bullet = skspritenode() let bulletarray = ["redbullet","magentabullet", "bluebullet", "cyanbullet", "greenbullet", "yellowbullet"] //didmovetoview: var randombullet = int(arc4random_uniform(6)) bulletincannon = skspritenode(imagenamed: bulletarray[randombullet]) bulletincannon.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidy(self.frame)) addchild(bulletincannon) //touchesended: var randombullet = int(arc4random_uniform(6)) bullet = skspritenode(texture: bulletincannon.texture) bullet.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidy(self.frame)) bullet.name = bulletarray[randombullet] bullet.physicsbody = skphysicsbody(texture: bullet.texture, size: self.bullet.size) bullet.physicsbody?.dynamic = true bullet.physicsbody?.categorybitmask = physicscategory.bullet bullet.physicsbody?.contacttestbitmask = physicscategory.cgyblock bullet.physicsbody?.collisionbitmask = physicscategory.none bullet.physicsbody?.affectedbygravity = false bullet.physicsbody?.usesprecisecollisiondetection = true addchild(bullet) bulletincannon.texture = sktexture(imagenamed: bulletarray[randombullet])
few ways go :
you can use node's userdata property.
bullet.userdata = ["type" : "white"]
to access it:
println(bullet.userdata?["type"])
you can create custom bullet class subclass of skspritenode , create property called "type", , in didbegincontact access property.
class bullet: skspritenode { var type:string = "" init(type:string) { self.type = type //later accessing bulletnode.type //this simple example give basic idea can do. //in real app should implement kind of security check avoid wrong type let texture = sktexture(imagenamed: type) super.init(texture: texture, color: nil, size: texture.size()) } required init(coder adecoder: nscoder) { fatalerror("init(coder:) has not been implemented") } }
you can use
bullet.name
property well, , on creation set appropriately based on bullet/block colour. later in didbegincontact checkbullet.name
find out bullet type. same goes blocks.func spawnbulletwithtype(type:string) -> skspritenode{ //set texture based on type //you can pass here white_bullet let atlas = sktextureatlas(named: "myatlas") //here, if passed white_bullet string, spritekit search texture called white_bullet.png let bullet = skspritenode(texture:atlas.texturenamed(type)) bullet.name = type // name white_bullet, , search in didbegincontact bullet.physicsbody = skphysicsbody(texture: bullet.texture, size: bullet.size) bullet.physicsbody?.categorybitmask = physicscategory.bullet bullet.physicsbody?.contacttestbitmask = physicscategory.cgyblock bullet.physicsbody?.collisionbitmask = physicscategory.none bullet.physicsbody?.affectedbygravity = false bullet.physicsbody?.usesprecisecollisiondetection = true return bullet }
edit:
based on recent comments go this:
let bulletarray = ["redbullet","magentabullet", "bluebullet", "cyanbullet", "greenbullet", "yellowbullet"] //didmovetoview: var randombullet = int(arc4random_uniform(6)) let bullettype = bulletarray[randombullet] bulletincannon.name = bullettype bulletincannon = skspritenode(imagenamed: bullettype ) bulletincannon.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidy(self.frame)) addchild(bulletincannon) //touchesended: var randombullet = int(arc4random_uniform(6)) bullet = skspritenode(texture: bulletincannon.texture) bullet.name = bulletincannon.name bullet.position = cgpointmake(cgrectgetmidx(self.frame), cgrectgetmidy(self.frame)) bullet.physicsbody = skphysicsbody(texture: bullet.texture, size: self.bullet.size) bullet.physicsbody?.dynamic = true bullet.physicsbody?.categorybitmask = physicscategory.bullet bullet.physicsbody?.contacttestbitmask = physicscategory.cgyblock bullet.physicsbody?.collisionbitmask = physicscategory.none bullet.physicsbody?.affectedbygravity = false bullet.physicsbody?.usesprecisecollisiondetection = true addchild(bullet) let bullettype = bulletarray[randombullet] bulletincannon.texture = sktexture(imagenamed: bullettype) bulletincannon.name = bullettype
Comments
Post a Comment