ios - How do I detect the collision of two SKShapeNodes in Swift without affecting their physics? -
log: here
please excuse me if stupid, horrid question, have been stuck on , can't find answers anywhere. have few nodes: leftmaze, rightmaze, , player. trying detect when player collides of other 2 nodes.
keep in mind physics want apply 2 maze nodes gravity. other that, want them pass through player. also, feel free give me constructive criticism on code new , want better! thank much!
override func didmove(to view: skview) { backgroundcolor = skcolor.white // player config // player.xscale = 0.25 player.yscale = 0.25 player.position = cgpoint(x:0,y:0 - (self.frame.height/4)) player.physicsbody = skphysicsbody(circleofradius: max(player.size.width / 2, player.size.height / 2)) player.physicsbody!.affectedbygravity = false player.physicsbody!.collisionbitmask = 0 // spawning game objects // addchild(player) // debug // print(size.width, " / " ,size.height) print(cgpoint(x: size.width * 0.5 ,y: size.height * 0.1)) timer = timer.scheduledtimer(timeinterval: 5, target: self, selector: #selector(createmaze),userinfo:nil, repeats: true) } func didbegin(_ contact: skphysicscontact) { if contact.bodya.node == player && contact.bodyb.node == leftmaze{ print("left collision!") } } func createmaze(){ /*-------- right maze init --------*/ // how big right part of maze let randomnumber = arc4random_uniform(uint32(self.frame.width * 0.66)) // how far place right part of maze right part of screen let distanceright = self.frame.maxx-cgfloat(randomnumber) // definition of right maze rightmaze.path = uibezierpath(roundedrect: cgrect(x: 0, y: 0, width: int(randomnumber), height: mazeheight), cornerradius: 0).cgpath rightmaze.position = cgpoint(x: cgfloat(distanceright), y: frame.maxy) rightmaze.fillcolor = uicolor.black //adding physics body , gravity rightmaze.physicsbody = skphysicsbody(rectangleof: cgsize(width: int(randomnumber), height: mazeheight)) rightmaze.physicsbody!.affectedbygravity = true rightmaze.physicsbody!.collisionbitmask = 0 /*-------- end of right maze init --------*/ /*-------- left maze init --------*/ // place left part of maze let distanceleft = self.frame.minx // definition of left maze leftmaze.path = uibezierpath(roundedrect: cgrect(x: 0, y: 0, width: int(self.frame.width-cgfloat(randomnumber)-(player.size.width+20)), height: mazeheight), cornerradius: 0).cgpath leftmaze.position = cgpoint(x: cgfloat(distanceleft), y: frame.maxy) leftmaze.fillcolor = uicolor.black //adding physics body , gravity leftmaze.physicsbody = skphysicsbody(rectangleof: cgsize(width: int(self.frame.width-cgfloat(randomnumber)-(player.size.width+20)), height: mazeheight)) leftmaze.physicsbody!.affectedbygravity = true leftmaze.physicsbody!.collisionbitmask = 0 /*-------- end of left maze init --------*/ addchild(rightmaze) addchild(leftmaze)
you have no contacttestbitmask
on sprites, didbegin
never called.
Comments
Post a Comment