对于相同的SKPhysicsBody,多次调用didBeginContact [英] didBeginContact is being called multiple times for the same SKPhysicsBody

查看:93
本文介绍了对于相同的SKPhysicsBody,多次调用didBeginContact的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 func didBeginContact(contact: SKPhysicsContact) {
    if ( contact.bodyA.categoryBitMask & BodyType.shield.rawValue ) == BodyType.shield.rawValue  {
        contact.bodyB.node?.removeFromParent()
        counter++
        println(counter)


    } else if ( contact.bodyB.categoryBitMask & BodyType.shield.rawValue ) == BodyType.shield.rawValue {
        contact.bodyA.node?.removeFromParent()
        counter++
        println(counter)
    }
}

一个物理体来自纹理 shield.physicsBody = SKPhysicsBody(纹理:shieldTexture,尺寸:shieldTexture.size())

另一个来自圆圈 sand.physicsBody = SKPhysicsBody(circleOfRadius:sand.size.width / 2)

当拖曳物体有时相互接触时 sand.physicsBody = SKPhysicsBody(circleOfRadius:sand.size.width / 2)
被调用m多久时间。我如何得到它只为每个对象调用一次,即使我一接触它就从父对象中删除它。

When the tow objects contact each other sometimes sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.size.width/2) gets called multiple times. How do i get it to only get called once for each object even though i remove it from the parent as soon as it contacts.

推荐答案

我已经想出如何只需要调用一次 func didBeginContact(contact:SKPhysicsContact)。这允许物理体具有纹理 SKPhysicsBody(texture:size :) 来计算一次碰撞,即使在现实中(由于纹理的物理体的性质),这个函数将是多次调用。

I have figured out how to get func didBeginContact(contact: SKPhysicsContact) to only be called once. This allows physics bodies with a texture SKPhysicsBody(texture: size:) to count collisions once even though in reality (because of the nature of the texture's physics body) this function will be called multiple times.

步骤1:

为SKSpriteNode创建一个名称属性(我们将使用ball for这个例子)
并将其设置为唯一名称。我们可以通过创建一个int来实现这个目的

Create a name property for the SKSpriteNode (we will use ball for this example) and set it equal to a unique name. We can do this by creating a int

var number = 0 

ball.name = "ball \(number)"

这允许创建对象的唯一名称evertime。

This allows for a unique name evertime that object is created.

第2步:

创建一个数组来保存这些数据,将球追加到数组中,增加数字

Create a array to hold these , append the ball to the array, increment the number

    var array: [String] = []
    var number = 0 

ball.name = "ball \(number)" 
array.append(ball.name!)
number ++

步骤3:现在在 func didBeginContact(contact:SKPhysicsContact)中查明该名称是否在数组中。如果它是增加分数,则删除节点,并从阵列中删除名称。如果名称不在数组中则不做任何事情。

Step 3: Now in the func didBeginContact(contact: SKPhysicsContact) find out if the name is in the array. If it is increment the score, remove the node, and remove the name from the array. If the name is not in the array don't do anything.

从数组中删除名称使我们现在只计算一次函数调用。

Removing the name from the array allows us now to only count the function call once.

func didBeginContact(contact: SKPhysicsContact) {
    if ( contact.bodyA.categoryBitMask & BodyType.shield.rawValue ) == BodyType.shield.rawValue  {
        var name = contact.bodyB.node?.name!
        let index = find(array, name!)

        if contains(array, name!) {
            score++
            contact.bodyB.node?.removeFromParent()
            array.removeAtIndex(index!)
        }
    } 
}

这篇关于对于相同的SKPhysicsBody,多次调用didBeginContact的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆