簡體   English   中英

使用Xcode Swift SpriteKit消除錯誤之一時的SKSpriteNode沖突消除

[英]SKSpriteNode Collision Removal In Removing Wrong One With Xcode Swift SpriteKit

我正在做一個游戲,當角色降落在雲上時,雲淡出。 我輸入代碼來確定何時擊中它,在同一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)

您需要刪除實際進行聯系的PhysicsBody表示的雲。 因此bodyA是播放器或雲,然后bodyB是播放器或雲。 然后從物理實體獲取實際的SKSpriteNode。 可以通過SKPhysics主體上的node屬性來引用它。 因此,bodyA.node或bodyB.node。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM