Ios Spritekit-未调用DidBeginContact
我注意到这个问题已经发生在很多人身上。我确保有Ios Spritekit-未调用DidBeginContact,ios,swift,sprite-kit,skphysicscontact,Ios,Swift,Sprite Kit,Skphysicscontact,我注意到这个问题已经发生在很多人身上。我确保有self.physicsWorld.contactDelegate=self 在didMove函数中,但它仍然不起作用。这是我的密码: class PoolTableScene: SKScene, SKPhysicsContactDelegate { struct PhysicsCatagory { static let None : UInt32 = 0 //0 static let OrangeBall : UInt32
self.physicsWorld.contactDelegate=self
在didMove函数中,但它仍然不起作用。这是我的密码:
class PoolTableScene: SKScene, SKPhysicsContactDelegate {
struct PhysicsCatagory {
static let None : UInt32 = 0 //0
static let OrangeBall : UInt32 = 0b1 //1
static let BlueBall : UInt32 = 0b10 //2
static let PokeBall : UInt32 = 0b100 //3
static let Border : UInt32 = 0b1000 //4
static let All : UInt32 = UInt32.max
}
let ballPoke = SKSpriteNode(imageNamed:"pokeBall")
let ballBlue = SKSpriteNode(imageNamed:"blueBall")
let ballOrange = SKSpriteNode(imageNamed: "orangeBall")
//var lastTouch: CGPoint? = nil
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVector(dx: 0, dy: -9.8)
//used to set gravity
//creates body for sprite that will lock the objects to a specific area
let sceneBody = SKPhysicsBody(edgeLoopFrom: self.frame)
sceneBody.friction = 0 //This will make the ball bounce naturally off the edges of the scenBody
self.physicsBody = sceneBody // make physics only affect whats in sceneBody - NEED PHYSICS BODY UNLESS IT WON'T BE AFFECTED BY PHYSICS
self.physicsBody?.categoryBitMask = PhysicsCatagory.Border
self.physicsBody?.collisionBitMask = PhysicsCatagory.BlueBall | PhysicsCatagory.OrangeBall | PhysicsCatagory.PokeBall
self.physicsBody?.contactTestBitMask = PhysicsCatagory.None
ballPoke.name = "ballPoke"
ballPoke.size = CGSize(width: 50, height: 50)
ballPoke.anchorPoint = CGPoint(x:0.5, y:0.5)
ballPoke.position = CGPoint(x: self.frame.size.width*0.25, y:self.frame.size.height/2)
ballPoke.zPosition = 100
ballPoke.physicsBody = SKPhysicsBody(circleOfRadius: 25)//need this so the ball can be affected by physics
ballPoke.physicsBody?.affectedByGravity = true //ball will be affected by gravity determined by the scene's physics
ballPoke.physicsBody?.restitution = 1 // sets bounciness of ball
ballPoke.physicsBody?.linearDamping = 0 //used to set how air resistence will affect ball
ballPoke.physicsBody?.categoryBitMask = PhysicsCatagory.PokeBall
ballPoke.physicsBody?.collisionBitMask = PhysicsCatagory.BlueBall | PhysicsCatagory.Border
ballPoke.physicsBody?.contactTestBitMask = PhysicsCatagory.OrangeBall
self.addChild(ballPoke)
ballBlue.name = "ballBlue"
ballBlue.size = CGSize(width: 50, height: 50)
ballBlue.anchorPoint = CGPoint(x:0.5, y:0.5)
ballBlue.position = CGPoint(x: self.frame.size.width*0.50, y:self.frame.size.height/3)
ballBlue.zPosition = 100
ballBlue.physicsBody = SKPhysicsBody(circleOfRadius: 25)//need this so the ball can be affected by physics
ballBlue.physicsBody?.affectedByGravity = true //ball will be affected by gravity determined by the scene's physics
ballBlue.physicsBody?.restitution = 1 // sets bounciness of ball
ballBlue.physicsBody?.linearDamping = 0 //used to set how air resistence will affect ball
ballBlue.physicsBody?.categoryBitMask = PhysicsCatagory.BlueBall
ballBlue.physicsBody?.collisionBitMask = PhysicsCatagory.OrangeBall | PhysicsCatagory.PokeBall | PhysicsCatagory.Border
ballBlue.physicsBody?.contactTestBitMask = PhysicsCatagory.None
self.addChild(ballBlue)
ballOrange.name = "ballOrange"
ballOrange.size = CGSize(width: 50, height: 50)
ballOrange.anchorPoint = CGPoint(x:0.5, y:0.5)
ballOrange.position = CGPoint(x: self.frame.size.width*0.75, y:self.frame.size.height/2)
ballOrange.zPosition = 100
ballOrange.physicsBody = SKPhysicsBody(circleOfRadius: 25)//need this so the ball can be affected by physics
ballOrange.physicsBody?.affectedByGravity = true //ball will be affected by gravity determined by the scene's physics
ballOrange.physicsBody?.restitution = 1 // sets bounciness of ball
ballOrange.physicsBody?.linearDamping = 0 //used to set how air resistence will affect ball
ballOrange.physicsBody?.categoryBitMask = PhysicsCatagory.OrangeBall
ballOrange.physicsBody?.collisionBitMask = PhysicsCatagory.BlueBall | PhysicsCatagory.Border
ballOrange.physicsBody?.contactTestBitMask = PhysicsCatagory.PokeBall
self.addChild(ballOrange)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//Finds the position where user touches screen
for touch: AnyObject in touches {
let positionOfTouch = touch.location(in: self)
//drags ball to where user touches screen
let dragBallAction = SKAction.move(to: CGPoint(x: positionOfTouch.x, y: positionOfTouch.y), duration: 0.5)
ballOrange.run(dragBallAction)
}
}
func didBeginContact(contact: SKPhysicsContact) {
print("contact")
var contactBody1: SKPhysicsBody
var contactBody2: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
contactBody1 = contact.bodyA
contactBody2 = contact.bodyB
}
else// else almost never gets called but still add it to function
{
contactBody1 = contact.bodyB
contactBody2 = contact.bodyA
}
//this uses the catagories set up in above struct (1 = Square, 2 = Ball)
if ((contactBody1.categoryBitMask == 1) && (contactBody2.categoryBitMask == 3)) {
//if the ball contact the square, remove the ball
contactBody2.node!.removeFromParent()
}
}
}
类PoolTableScene:SKScene,skphysiccontactdelegate{
结构物理分类{
静态let None:UInt32=0//0
静态let橙色球:UInt32=0b1//1
静态let蓝球:UInt32=0b10//2
静态let PokeBall:UInt32=0b100//3
静态let边框:UInt32=0b1000//4
静态let All:UInt32=UInt32.max
}
让ballPoke=SKSpriteNode(图像名为:“pokeBall”)
让ballBlue=SKSpriteNode(图像名为:“蓝球”)
让ballOrange=SKSpriteNode(图像名为:“橙球”)
//var lastTouch:CGPoint?=nil
覆盖func didMove(到视图:SKView){
self.physicsWorld.contactDelegate=self
self.physicsWorld.gravity=CGVector(dx:0,dy:-9.8)
//用于设置重力
//为将对象锁定到特定区域的精灵创建主体
让sceneBody=SKPhysicsBody(edgeLoopFrom:self.frame)
sceneBody.friction=0//这将使球从主体边缘自然反弹
self.physicsBody=sceneBody//使物理只影响sceneBody中的内容-需要物理体,除非它不受物理影响
self.physicsBody?.categoryBitMask=physicCatagory.Border
self.physicsBody?.collisionBitMask=physicCatagory.BlueBall | physicCatagory.OrangeBall | physicCatagory.PokeBall
self.physicsBody?.contactTestBitMask=physicCatagory.None
ballPoke.name=“ballPoke”
ballPoke.size=CGSize(宽:50,高:50)
ballPoke.anchorPoint=CGPoint(x:0.5,y:0.5)
ballPoke.position=CGPoint(x:self.frame.size.width*0.25,y:self.frame.size.height/2)
ballPoke.zPosition=100
ballPoke.physicsBody=SKPhysicsBody(circleOfRadius:25)//需要此选项,以便球可以受到物理的影响
ballPoke.physicsBody?.affectedByGravity=true//球将受到由场景物理确定的重力的影响
ballPoke.physicsBody?.restitution=1//设置球的弹性
ballPoke.physicsBody?.linearDamping=0//用于设置空气阻力对球的影响
ballPoke.physicsBody?.categoryBitMask=PhysicCatagory.PokeBall
ballPoke.physicsBody?.collisionBitMask=PhysicCatagory.BlueBall | PhysicCatagory.Border
ballPoke.physicsBody?.contactTestBitMask=PhysicCatagory.OrangeBall
self.addChild(ballPoke)
ballBlue.name=“ballBlue”
ballBlue.size=CGSize(宽:50,高:50)
ballBlue.ancorpoint=CGPoint(x:0.5,y:0.5)
ballBlue.position=CGPoint(x:self.frame.size.width*0.50,y:self.frame.size.height/3)
ballBlue.zPosition=100
ballBlue.physicsBody=SKPhysicsBody(circleOfRadius:25)//需要此选项,以便球可以受物理影响
ballBlue.physicsBody?.affectedByGravity=true//球将受到由场景物理确定的重力的影响
ballBlue.physicsBody?.restitution=1//设置球的弹性
ballBlue.physicsBody?.linearDamping=0//用于设置空气阻力对球的影响
ballBlue.physicsBody?.categoryBitMask=PhysicCatagory.BlueBall
ballBlue.physicsBody?.collisionBitMask=PhysicCatagory.OrangeBall | PhysicCatagory.PokeBall | PhysicCatagory.Border
ballBlue.physicsBody?.contactTestBitMask=PhysicCatagory.None
self.addChild(ballBlue)
ballOrange.name=“ballOrange”
ballOrange.size=CGSize(宽:50,高:50)
ballOrange.anchorPoint=CGPoint(x:0.5,y:0.5)
ballOrange.position=CGPoint(x:self.frame.size.width*0.75,y:self.frame.size.height/2)
BALLAGRANGE.zPosition=100
ballOrange.physicsBody=SKPhysicsBody(circleOfRadius:25)//需要此选项,以便球可以受物理影响
ballOrange.physicsBody?.affectedByGravity=true//球将受到由场景物理确定的重力的影响
ballOrange.physicsBody?.restitution=1//设置球的弹性
ballOrange.physicsBody?.linearDamping=0//用于设置空气阻力对球的影响
ballOrange.physicsBody?.categoryBitMask=PhysicCatagory.OrangeBall
ballOrange.physicsBody?.collisionBitMask=PhysicCatagory.BlueBall | PhysicCatagory.Border
ballOrange.physicsBody?.contactTestBitMask=PhysicCatagory.PokeBall
self.addChild(范围)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
//查找用户触摸屏幕的位置
用于触摸:触摸中的任何对象{
let positionOfTouch=触摸位置(in:self)
//将球拖动到用户触摸屏幕的位置
让dragBallAction=SKAction.move(到:CGPoint(x:positionOfTouch.x,y:positionOfTouch.y),持续时间:0.5)
ballOrange.run(dragBallAction)
}
}
func didBeginContact(联系人:SKPhysicsContact){
打印(“联系人”)
var ContactBody 1:SKPhysicsBody
var ContactBody 2:SKPhysicsBody
如果contact.bodyA.categoryBitMask
我也有同样的问题,因为我不太熟悉ContactA和ContactB的工作原理,所以我给每个对象取了一个名称,并检查这两个对象是否会像这样碰撞
func didMoveToView(view: SKView) {
objectA = self.childNodeWithName("ObjectA") as! SKSpriteNode!
objectB = self.childNodeWithName("ObjectB") as! SKSpriteNode!
}
func didBeginContact(contact: SKPhysicsContact) {
let contactA: SKPhysicsBody = contact.bodyA
let contactB: SKPhysicsBody = contact.bodyB
let nodeA = contactA.node as! SKSpriteNode
let nodeB = contactB.node as! SKSpriteNode
if nodeA.name == "ObjectA" && nodeB.name == "ObjectB" {
//do something
}
}
检测碰撞的属性是接触测试位掩码,这就是为什么它只是通过我认为碰撞位掩码和接触测试位掩码相互混淆 接触测试是在碰撞位掩码允许身体通过每个oth时检测接触的测试
Optional("shape_blueSquare") collides with Optional("Screen_edge")
Optional("shape_blueSquare") collides with Optional("shape_redCircle")
Optional("shape_blueSquare") collides with Optional("shape_purpleSquare")
Optional("shape_blueSquare") collides with Optional("shape_greenRect")
Optional("shape_redCircle") collides with Optional("Screen_edge")
Optional("shape_redCircle") collides with Optional("shape_blueSquare")
Optional("shape_redCircle") notifies when contacting Optional("shape_purpleSquare")
Optional("shape_redCircle") collides with Optional("shape_greenRect")
Optional("shape_redCircle") notifies when contacting Optional("shape_greenRect")
Optional("shape_purpleSquare") collides with Optional("Screen_edge")
Optional("shape_purpleSquare") collides with Optional("shape_greenRect")
Category for Optional("shape_greenRect") does not appear to be set correctly as 4294967295
ptional("shape_greenRect") collides with Optional("Screen_edge")
Optional("shape_yellowTriangle") collides with Optional("Screen_edge")
Optional("shape_yellowTriangle") notifies when contacting Optional("shape_redCircle")
Optional("shape_yellowTriangle") collides with Optional("shape_greenRect")
Optional("shape_yellowTriangle") notifies when contacting Optional("shape_greenRect")