如何使我的玩家站在地面上而不是跌倒?

时间:2019-09-01 00:22:16

标签: swift xcode game-development

我是Xcode游戏开发的初学者,我的玩家不断跌落在GameScene上的基础上。

在GameScene中,我仅添加了地面作为SKTexture和玩家,这也是SKTexture。还有一个physicsCategories结构。

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {
    struct physicsCategories {
        static let None: UInt32 = 0
        static let Player: UInt32 = 0b1
        static let Ground: UInt32 = 0b100 
    }

    override func didMove(to view: SKView) {
        self.backgroundColor = UIColor.blue
        self.anchorPoint = CGPoint(x: 0.0, y: 0.0)
        self.physicsWorld.contactDelegate = self
        self.physicsWorld.gravity = CGVector(dx: 0, dy: -2)

        let player = Player(scene: self)
        player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
        player.physicsBody?.categoryBitMask = physicsCategories.Player
        player.physicsBody?.contactTestBitMask = physicsCategories.Ground
        player.physicsBody?.collisionBitMask = physicsCategories.Ground
        player.physicsBody?.affectedByGravity = true
        player.physicsBody?.isDynamic = true
        addChild(player)
        createGrounds()
    }

    func didBegin(_ contact: SKPhysicsContact) {
        var body1 = SKPhysicsBody()
        var body2 = SKPhysicsBody()

        if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
            body1 = contact.bodyA
            body2 = contact.bodyB
        } else {
            body1 = contact.bodyB
            body2 = contact.bodyA
        }

        if (body1.categoryBitMask == physicsCategories.Player && body2.categoryBitMask == physicsCategories.Ground) {
            body1.node?.position.y = 80
        }
    }

    func createGrounds() {
        let backgroundTexture = SKTexture(imageNamed: "ground")

        for i in 0 ... 3 {
            let background = SKSpriteNode(texture: backgroundTexture)
            background.zPosition = 2
            background.anchorPoint = CGPoint(x: 0.0, y: 0.0)
            background.size = CGSize(width: (self.scene?.size.width)!, height: 250)
            background.position = CGPoint(x: (backgroundTexture.size().width * CGFloat(i)), y: -(self.frame.size.height/2))
            background.physicsBody = SKPhysicsBody(rectangleOf: background.size)
            background.physicsBod
            background.physicsBody?.isDynamic = false 
            background.physicsBody!.affectedByGravity = false
            background.physicsBody?.categoryBitMask = physicsCategories.Ground
            background.physicsBody?.contactTestBitMask = physicsCategories.Player
            background.physicsBody?.collisionBitMask = physicsCategories.Player
            background.physicsBody?.applyForce(CGVector(dx: 0, dy: 2))
            addChild(background)

            let moveLeft = SKAction.moveBy(x: -backgroundTexture.size().width, y: 0, duration: 4)
            let moveReset = SKAction.moveBy(x: backgroundTexture.size().width , y: 0, duration: 0)
            let moveLoop = SKAction.sequence([moveLeft, moveReset])
            let moveForever = SKAction.repeatForever(moveLoop)

            background.run(moveForever)
        }
    }
}

0 个答案:

没有答案