简体   繁体   中英

Create Wall / Barrier Using SKSpriteNode physicsBody

I have a separate "Floor" class in my SKSpriteKit application. When I first created this class, I had a barrier around the whole frame using

self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)

but that was actually stopping SKSpriteNodes from entering from the top of the screen so now I'm trying to figure out how I can create a left, right and bottom of the frame wall but not the top. This is so I can avoid one of my SKSpriteNodes at the bottom of the screen (still viewable by users) that moves left and right from leaving the display. I'm trying below which is building as if it's going to work but then when I start up the game I don't see the the walls? I'm not too sure what I'm doing wrong, I have

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")
        }
    }

Gravity is affecting your walls, so they are forever falling into oblivion.

When you created your edge loop, you did not need to worry about this because edge loops are always static because it has no volumne

Now you are using volume based bodies, so you need to account for things like gravity and other forces affecting your bodies.

To have your body ignore these forces, simply set 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM