SpriteKit SKNode不会与边缘发生碰撞

时间:2015-09-26 05:22:13

标签: ios swift sprite-kit skspritenode skphysicsbody

我正在使用Swift在SpriteKit中制作游戏。我不能为我的生活弄清楚为什么我的节点不会碰撞并从屏幕反弹,这是SKSpriteNode的默认行为。

这是我在三个不同文件中的所有代码:

GameViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    let skView = view as! SKView
    skView.showsFPS = true
    skView.showsNodeCount = true

    let battleScene = CSLevel()
    battleScene.size = view.frame.size
    battleScene.scaleMode = .AspectFill

    skView.presentScene(battleScene)
}

CSLevel:

//sorry here's the real code     
class CSLevel: SKScene, SKPhysicsContactDelegate {

    var myWorld: SKNode!
    var leader: CSCharacter?

    override init(size: CGSize) {
        super.init(size: size)

        setUpScene()
        setUpCharacters()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func didMoveToView(view: SKView) {
        super.didMoveToView(view)


    }

    func setUpScene() {
        //Take care of setting up the world and bring in property list

        self.anchorPoint = CGPoint(x: 0.5, y: 0.5) //0,0 to 1,1

        myWorld = SKNode()
        self.addChild(myWorld)

        let map = SKSpriteNode(imageNamed: "map")
        map.position = CGPoint(x: 0, y: 0)

        myWorld.addChild(map)

        myWorld.xScale = 1
        myWorld.yScale = 1

        //Setup physics
        self.physicsWorld.gravity = CGVector(dx: 0, dy: -1)
        self.physicsWorld.contactDelegate = self

        myWorld.physicsBody = SKPhysicsBody(edgeLoopFromRect: screenFrame)
        myWorld.physicsBody?.categoryBitMask = wallCategory
    }

    func setUpCharacters() {
        leader = CSCharacter()
        myWorld.addChild(leader!)
    }

    override func update(currentTime: NSTimeInterval) {

    }

    func didBeginContact(contact: SKPhysicsContact) {
        let firstBody = contact.bodyA
        let secondBody = contact.bodyB

        if firstBody.categoryBitMask == wallCategory || secondBody.categoryBitMask == wallCategory {

        }
    }
}

CSCharacter:

class CSCharacter: SKNode {

    var character: SKSpriteNode! //this will be the actual image you see of the character

    override init() {
        super.init()

        character = SKSpriteNode(imageNamed: "character")
        self.addChild(character)

        self.physicsBody = SKPhysicsBody(circleOfRadius: character.frame.size.width / 2)
        self.physicsBody?.dynamic = true
        self.physicsBody?.restitution = 5
        self.physicsBody?.allowsRotation = true

        self.xScale = 2
        self.yScale = 2

        self.physicsBody?.categoryBitMask = playerCategory
        self.physicsBody?.collisionBitMask = wallCategory
        self.physicsBody?.contactTestBitMask = wallCategory
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

这是一个错误还是什么?运行Xcode beta 7.1。我真的按照教程进行操作,并且它的行为方式与该人的行为方式不同,即使我输入的功能也是他打字的功能。任何帮助或澄清赞赏!!!

1 个答案:

答案 0 :(得分:0)

这里#(希望这有帮助)

尝试在“skview.showsNodeCount = true”下面添加“skView.showsPhysics = true” 这将自动跟踪物理机构周围的线条。 当你运行你的应用程序时,你应该在你的边缘循环所在的skscene周围看到一条细蓝线。 如果不是,您可能需要添加一个参数“let battleScene = CSLevel()”。 所以“让battleScene = CSLevel()”现在是“让battleScene = CSLevel(size:skView.frame.size)”。

换句话说,由于未指定边循环的大小,您的节点可能会通过边循环落下