使用Xcode Swift SpriteKit删除错误的SKSpriteNode碰撞

时间:2015-04-25 01:18:54

标签: swift sprite-kit

我正在制作一个游戏,当角色登陆云时,云渐弱。我输入代码来确定它何时被击中,在同一个SKSpriteNode下注册了多个云,但是当它落在云上时,错误的云正在消失,这是最近添加的SKSpriteNode被删除,而不是一个与之相撞的人。

有没有办法做到这一点所以只删除角色碰撞的那个,而不是最早产生的角色?这是代码:

func didBeginContact(contact: SKPhysicsContact)
{



    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    switch(contactMask)
    {





    case BodyType.PersonCategory.rawValue | BodyType.CloudCategory.rawValue:

        let CheckDelay = delay(0.02)
            {

                    self.Cloud.runAction(self.FadeAway)

        }

    default:
        return





Person.physicsBody?.usesPreciseCollisionDetection = true
Person.size = CGSizeMake(self.frame.size.width / 25, self.frame.size.height / 16.25)
Person.physicsBody = SKPhysicsBody(rectangleOfSize: Person.size)
Person.physicsBody?.restitution = 0
Person.physicsBody?.friction = 0
Person.physicsBody?.allowsRotation = false
Person.physicsBody?.affectedByGravity = true
Person.physicsBody?.dynamic = true
Person.physicsBody?.linearDamping = 0
Person.zPosition = 5
Person.physicsBody?.categoryBitMask = BodyType.PersonCategory.rawValue
Person.physicsBody?.contactTestBitMask = BodyType.CloudCategory.rawValue
Person.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) * 1.7)
self.addChild(Person)


Cloud = SKSpriteNode(texture: NormalCloudTexture)
Cloud.zPosition = 7
Cloud.physicsBody?.usesPreciseCollisionDetection = true
Cloud.physicsBody?.affectedByGravity = false
Cloud.physicsBody?.allowsRotation = false
Cloud.size = CGSizeMake(self.frame.size.width / 8.05, self.frame.size.height / 40)
Cloud.physicsBody = SKPhysicsBody(rectangleOfSize: Cloud.size)
Cloud.physicsBody?.friction = 0
Cloud.physicsBody?.restitution = 0
Cloud.physicsBody?.dynamic = false
Cloud.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) / 7.60)
addChild(Cloud)

1 个答案:

答案 0 :(得分:0)

您需要删除实际进行联系的physicsBody所代表的云。所以bodyA要么是玩家,要么是云,然后bodyB要么是玩家,要么是云。然后从物理主体获取实际的SKSpriteNode。它可以通过SKPhysics主体上的node属性引用。所以bodyA.node或bodyB.node。