使用SKSpriteNode physicsBody

时间:2017-12-21 02:03:37

标签: swift sprite-kit skspritenode skphysicsbody

我的SKSpriteKit应用程序中有一个单独的“Floor”类。当我第一次创建这个类时,我使用

在整个框架周围有一个障碍
  

self.physicsBody = SKPhysicsBody(edgeLoopF​​rom:self.frame)

但这实际上阻止了SKSpriteNodes从屏幕顶部进入,所以现在我想弄清楚如何创建框架墙的左,右和底部而不是顶部。这样我就可以避开屏幕底部的一个SKSpriteNodes(用户仍然可以看到)从离开显示屏时左右移动。我正在尝试下面建造好像它会起作用但是当我启动游戏时我看不到墙壁?我不太确定我做错了什么,我有

  

skView.showsPhysics = true

    import Foundation
import SpriteKit

    class Floor: SKNode {
        override init() {
            super.init()

            let leftWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 100, height: frame.height))
            leftWall.position = CGPoint(x: 100, y: 100)
            leftWall.physicsBody = SKPhysicsBody(rectangleOf: leftWall.size)
            leftWall.physicsBody!.isDynamic = false
            self.addChild(leftWall)

            let rightWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 100, height: frame.height))
            rightWall.position = CGPoint(x: 200, y: 200)
            rightWall.physicsBody = SKPhysicsBody(rectangleOf: rightWall.size)
            rightWall.physicsBody!.isDynamic = false
            self.addChild(rightWall)

            // Set the bit mask properties
            self.physicsBody?.categoryBitMask = balloonCategory
            self.physicsBody?.contactTestBitMask = nailDropCategory
            //self.physicsBody?.collisionBitMask = balloonCategory
        }

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

1 个答案:

答案 0 :(得分:2)

重力正在影响你的墙壁,所以它们永远都会被遗忘。

当你创建边循环时,你不必担心这个,因为边循环总是静态的,因为它没有volumne

现在您正在使用基于体积的身体,因此您需要考虑重力和其他影响身体的力量。

要让您的身体忽略这些力量,只需设置isDynamic = false

即可
let leftWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 100, height: frame.height))
leftWall.position = CGPoint(x: 100, y: 100)
leftWall.physicsBody = SKPhysicsBody(rectangleOf: leftWall.size)
leftWall.physicsBody!.isDynamic = false
self.addChild(leftWall)

let rightWall = SKSpriteNode(color: UIColor.brown, size: CGSize(width: 100, height: frame.height))
rightWall.position = CGPoint(x: 200, y: 200)
rightWall.physicsBody = SKPhysicsBody(rectangleOf: rightWall.size)
rightWall.physicsBody!.isDynamic = false
self.addChild(rightWall)