swift - 如何禁用两个 SKSpriteNode 之间的接触?

标签 swift sprite-kit swift2 collision-detection skspritenode

出于某种原因,在下面的代码中,敌方细菌在相互接触时移动了食物(豌 bean )。我没有对它们进行编码来测试彼此的联系。有什么帮助吗?我正在努力使两种类型的 Sprite 不可能发生碰撞。

import SpriteKit
var apple=SKSpriteNode()

class GameScene: SKScene, SKPhysicsContactDelegate {
    var bg=SKSpriteNode()
    var peas=SKSpriteNode()
    var bacteria=SKSpriteNode()
    var bactys_Y=[SKSpriteNode]()
    var bactys_X=[SKSpriteNode]()
    var border=CGRect()
   // var gandolf=SKSpriteNode()
    enum ColliderType:UInt32{
        case Bug=1
        case Food=2
        case Enemy=4
        case Gandolf=8
    }

    override func didMoveToView(view: SKView) {
        border=CGRect(x: 0, y: self.frame.height, width: self.frame.width, height: self.frame.height-200)
        //gandolf.physicsBody=SKPhysicsBody(edgeLoopFromRect: border)
        let bactPic=SKTexture(imageNamed: "bacteria.png")
        /* Setup your scene here */
        //bg info
        self.physicsWorld.contactDelegate=self
        let bg_texture=SKTexture(imageNamed: "wood-texture-pattern.jpg")
        bg=SKSpriteNode(texture: bg_texture)
        bg.position=CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        bg.size.height=self.frame.height
        bg.size.width=self.frame.width
       // self.addChild(bg)
        //bug info
        let applePic=SKTexture(imageNamed:"bug.png")
        apple=SKSpriteNode(texture:applePic)
        apple.position=CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))

        print(self.frame.width)
       // let bactPic=SKTexture(imageNamed: "bacteria.png")
        //peas info
        let peas_texture=SKTexture(imageNamed: "peas.png")
        peas=SKSpriteNode(texture: peas_texture)
       // peas.position=CGPointMake(200,300)
        peas.physicsBody=SKPhysicsBody(circleOfRadius: peas_texture.size().width/4)
        apple.physicsBody=SKPhysicsBody(circleOfRadius:applePic.size().width/2)
        peas.physicsBody?.affectedByGravity=false
        apple.physicsBody?.affectedByGravity=false
        bg.zPosition=1
        apple.zPosition=2
        peas.zPosition=3
        apple.physicsBody!.categoryBitMask=ColliderType.Bug.rawValue
        apple.physicsBody!.contactTestBitMask=ColliderType.Food.rawValue
        peas.physicsBody!.categoryBitMask=ColliderType.Food.rawValue
        peas.physicsBody!.contactTestBitMask=ColliderType.Bug.rawValue
        bacteria.physicsBody=SKPhysicsBody(circleOfRadius: bactPic.size().width*0.06/2)
        bacteria.physicsBody!.categoryBitMask=ColliderType.Enemy.rawValue
        bacteria.physicsBody!.contactTestBitMask=ColliderType.Bug.rawValue
     //   gandolf.physicsBody?.categoryBitMask=ColliderType.Gandolf.rawValue
    //    gandolf.physicsBody?.categoryBitMask=ColliderType.Enemy.rawValue
        peas.setScale(0.4)
        apple.setScale(0.5)
        // bacteria info
      //  gandolf.color=UIColor.blackColor()
      //  self.addChild(gandolf)


        self.addChild(apple)
      //  self.addChild(peas)
        _=NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: Selector("spawnBacteria"), userInfo: nil, repeats: true)
        spawnBacteria()
        spawnFood()


    }
    func didBeginContact(contact: SKPhysicsContact) {
      //  peas.removeFromParent()
      //  spawnFood()
        let first=contact.bodyA
        let second=contact.bodyB
        if first.categoryBitMask==ColliderType.Bug.rawValue || second.categoryBitMask==ColliderType.Bug.rawValue{
            if first.categoryBitMask==ColliderType.Food.rawValue || second.categoryBitMask==ColliderType.Food.rawValue{
                peas.removeFromParent()
                spawnFood()
            }
        }
        if first.categoryBitMask==ColliderType.Bug.rawValue || second.categoryBitMask==ColliderType.Bug.rawValue{
            if first.categoryBitMask==ColliderType.Enemy.rawValue || second.categoryBitMask==ColliderType.Enemy.rawValue{
                print("DEATH")
            }
        }

    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
       /* Called when a touch begins */


    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
       // print("x: "+String(apple.position.x))
       // print("y: "+String(apple.position.y))
        for node:SKSpriteNode in bactys_X{
            if node.position.x<=0{
                print("collide")
                node.physicsBody?.velocity=CGVector(dx:0, dy:0)
                node.physicsBody?.applyImpulse(CGVector(dx:6, dy:0))
            }else if node.position.x>=self.frame.size.width-3{
                node.physicsBody?.velocity=CGVector(dx:0,dy:0)
                node.physicsBody?.applyImpulse(CGVector(dx:-6,dy:0))
            }
        }
        for node:SKSpriteNode in bactys_Y{
            if node.position.y>=self.frame.size.height-3{
                node.physicsBody?.velocity=CGVector(dx:0,dy:0)
                node.physicsBody?.applyImpulse(CGVector(dx:0,dy:-6))
            }else if node.position.y<=200{
                node.physicsBody?.velocity=CGVector(dx:0,dy:0)
                node.physicsBody?.applyImpulse(CGVector(dx:0,dy:6))
            }
        }
    }
    func spawnFood(){
        let peas_texture=SKTexture(imageNamed: "peas.png")
        peas=SKSpriteNode(texture: peas_texture)
        peas.physicsBody=SKPhysicsBody(circleOfRadius: peas_texture.size().width/4)
        peas.zPosition=3
        peas.physicsBody?.affectedByGravity=false
        peas.physicsBody?.categoryBitMask=ColliderType.Food.rawValue
        peas.physicsBody?.contactTestBitMask=ColliderType.Bug.rawValue
        let x=CGFloat(arc4random_uniform(UInt32(self.frame.size.width-40)+20))
        let y=CGFloat(arc4random_uniform(UInt32(self.frame.size.height-20-200))+200)
        //print(self.frame.size.width)
        print("height: "+String(self.frame.size.height))
        print("x: "+String(x))
        print("y: "+String(y))
        peas.position=CGPointMake(x,y)
        peas.setScale(0.4)
        self.addChild(peas)
    }
    func spawnBacteria(){
        let bactPic=SKTexture(imageNamed: "bacteria.png")
        bacteria=SKSpriteNode(texture: bactPic)
        bacteria.physicsBody=SKPhysicsBody(circleOfRadius: bactPic.size().width*0.06/2)
        bacteria.physicsBody?.affectedByGravity=false
        bacteria.physicsBody?.categoryBitMask=ColliderType.Enemy.rawValue
        bacteria.physicsBody?.contactTestBitMask=ColliderType.Bug.rawValue
       // bacteria.physicsBody?.contactTestBitMask=ColliderType.Gandolf.rawValue
        bacteria.zPosition=4
        let x=CGFloat(arc4random_uniform(UInt32(self.frame.size.width-40)+20))
        let y=CGFloat(arc4random_uniform(UInt32(self.frame.size.height-20-200))+200)
        bacteria.position=CGPointMake(x,y)
        bacteria.setScale(0.06)
        let decide=Int(arc4random_uniform(3))

        self.addChild(bacteria)
        if decide==0{
            //move left
           //  let move=SKAction.moveByX(-6, y: 0, duration: 0.1)
            // let start=SKAction.repeatActionForever(move)
             //bacteria.runAction(start)
            bacteria.physicsBody?.applyImpulse(CGVector(dx: -6,dy: 0))
            bactys_X.append(bacteria)

        }else if decide==1{
            //move right
          //  let move=SKAction.moveByX(6, y: 0, duration: 0.1)
            // let start=SKAction.repeatActionForever(move)
            // bacteria.runAction(start)
            bacteria.physicsBody?.applyImpulse(CGVector(dx: 6, dy: 0))
            bactys_X.append(bacteria)
        }else if decide==2{
            //move up
          //  let move=SKAction.moveByX(0, y: 6, duration: 0.1)
            // let start=SKAction.repeatActionForever(move)
            //  bacteria.runAction(start)
            bacteria.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 6))
            bactys_Y.append(bacteria)
        }else{
            //move down
           //  let move=SKAction.moveByX(0, y: -6, duration: 0.1)
            // let start=SKAction.repeatActionForever(move)
          //  bacteria.runAction(start)
            bacteria.physicsBody?.applyImpulse(CGVector(dx: 0,dy: -6))
            bactys_Y.append(bacteria)
        }


    }
}

最佳答案

默认情况下,物理体的碰撞位掩码设置为 0xFFFFFFFF(与所有对象交互),其动态属性设置为 true。查看您的代码,似乎您尚未设置它们,因此它使用默认值。

您必须设置这些属性才能达到您想要的结果

...physicsBody.dynamic = false
...physicsBody.collisionBitMask = //set to something

关于swift - 如何禁用两个 SKSpriteNode 之间的接触?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35640272/

相关文章:

json - SWIFT 中带有自定义参数的发布请求

Swift 4 - 垂直 UICollectionView 上的 UIRefreshControl 滞后

arrays - 在 Swift 中存储/访问大量数据

ios - 快速在通用类中实现协议(protocol)

ios - Swift - 将控制台日志重定向到文档文件夹

ios - Webview 中的应用程序传输安全问题

ios - 将 subview 添加到 UIViewController 使 UITablview subview 消失

ios - 添加一个 SKLabelNode 到 Scene Kit View

ios - SpriteKit 在运行带有大型 tilemap (JSTileMap) 的游戏时使用 95-100% CPU

ios - Swift 2.0 异常处理