0

我似乎无法让 2 个精灵相互联系,因为当应用程序运行时,其中一个精灵没有联系,只是通过了它。我不确定有什么问题。有人可以帮助我。

import SpriteKit
import GameplayKit

enum BodyType:UInt32{

    case caveMan = 1
    case Zombie = 2
}

class GameScene: SKScene, SKPhysicsContactDelegate {

    var caveMan:SKSpriteNode = SKSpriteNode ()
    let swipeRightRec = UISwipeGestureRecognizer ()
    var Zombie:SKSpriteNode = SKSpriteNode ()

    override func didMove(to view: SKView) {

        self.physicsWorld.contactDelegate = self

        swipeRightRec.addTarget(self, action: #selector (GameScene.swipedRight ))
        swipeRightRec.direction = .right
        self.view!.addGestureRecognizer(swipeRightRec)

        if let somePlayer:SKSpriteNode = self.childNode(withName: "caveMan") as? SKSpriteNode {
            caveMan = somePlayer
            caveMan.physicsBody?.affectedByGravity = true
            caveMan.physicsBody?.isDynamic = false
            caveMan.physicsBody?.categoryBitMask = BodyType.caveMan.rawValue
            caveMan.physicsBody?.collisionBitMask = BodyType.Zombie.rawValue
            caveMan.physicsBody?.contactTestBitMask = BodyType.Zombie.rawValue
        }

        if let somePlayer:SKSpriteNode = self.childNode(withName: "Zombie") as? SKSpriteNode {
            Zombie = somePlayer
            Zombie.physicsBody?.affectedByGravity = false
            Zombie.physicsBody?.isDynamic = false
            Zombie.physicsBody?.categoryBitMask = BodyType.Zombie.rawValue
            Zombie.physicsBody?.collisionBitMask = BodyType.caveMan.rawValue
            Zombie.physicsBody?.contactTestBitMask = BodyType.caveMan.rawValue   
        }
    }

    @objc func swipedRight() {

        print("went right")   
        moveDown()   
    }

    func moveDown() {

        let walkAnimation:SKAction = SKAction(named: "Running")!
        let walk:SKAction = SKAction.moveBy(x: 90, y: 0, duration: 1)
        let group:SKAction = SKAction.group([walkAnimation, walk])

        caveMan.run(group)  
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        for t in touches {
            self.touchDown(atPoint: t.location(in: self))
            break
        }
    }

    func didBegin(_ contact: SKPhysicsContact) {

        if (contact.bodyA.categoryBitMask == BodyType.caveMan.rawValue && contact.bodyB.categoryBitMask == BodyType.Zombie.rawValue) {

            print ("touched a Zombie")
        } else if (contact.bodyB.categoryBitMask == BodyType.caveMan.rawValue && contact.bodyA.categoryBitMask == BodyType.Zombie.rawValue) {

            print ("touched a Zombie")
        }
    }
}
4

1 回答 1

0

请参阅:caveMan.physicsBody? 这意味着如果存在主体,则允许设置值。它被称为可选绑定,或者在其他语言中称为短路。

您需要创建物理体,它不是免费提供给您的。

截至目前,这是您的代码的样子:

    caveMan = somePlayer
    nil.affectedByGravity = true
    nil.isDynamic = false
    nil.categoryBitMask = BodyType.caveMan.rawValue
    nil.collisionBitMask = BodyType.Zombie.rawValue
    nil.contactTestBitMask = BodyType.Zombie.rawValue

你想要做的是:

    caveMan = somePlayer
    caveMan.physicsBody = SKPhysicsBody(rectangleOf: caveMan.size)
    caveMan.physicsBody!.affectedByGravity = true
    caveMan.physicsBody!.isDynamic = false
    caveMan.physicsBody!.categoryBitMask = BodyType.caveMan.rawValue
    caveMan.physicsBody!.collisionBitMask = BodyType.Zombie.rawValue
    caveMan.physicsBody!.contactTestBitMask = BodyType.Zombie.rawValue
于 2017-11-20T17:34:18.493 回答