не в состоянии применить импульс к SKPhysicsbody - PullRequest
0 голосов
/ 18 марта 2019

так что я работаю над своего рода боковым скроллером, первая игра с Xcode и swift, но вот что у меня есть:

    import SpriteKit
import GameplayKit

let jumpButton = SKSpriteNode(imageNamed: "jumpButton")
var jumpButtonPosition = CGPoint(x: 500, y: -250)
var selectedButton = "nodeName"

let background1 = SKSpriteNode(imageNamed: "Woods_Background")
let background2 = SKSpriteNode(imageNamed: "Woods_Background")
let background3 = SKSpriteNode(imageNamed: "Woods_Background")

let player = SKSpriteNode(imageNamed: "player")
var playerColor = UIColor.red
var playerSize = CGSize(width: 50, height: 100)
var playerPhysicsBody = player.physicsBody
let playerJumpVector = CGVector(dx: 0, dy: 50)

let ground = SKSpriteNode()

var camPosition = CGPoint()
var playerPosition = camPosition
let cam = SKCameraNode()

var anchorpoint = CGPoint(x: 0.5, y: 0.5)
let endPoint = CGPoint(x: 100000, y: 0)

class GameScene: SKScene {

    override func didMove(to view: SKView) {

        super.didMove(to: view)
        self.camera = cam
        self.addChild(cam)

        spawnPlayer()
        spawnBackground()
        spawnJumpButton()
    }

    func spawnGround(){
        scene?.addChild(ground)
        ground.size = CGSize(width: size.width * 1, height: size.height * 0.1)
        ground.position = CGPoint(x: size.width * 0.5, y: size.height * 0.05)
        ground.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: size.height, height: size.height * 0.1))
        ground.physicsBody?.isDynamic = false
    }

    func spawnPlayer() {
        scene?.addChild(player)
        player.position = camPosition
        player.zPosition = 1
        player.size = playerSize
        player.physicsBody = playerPhysicsBody
        playerPhysicsBody?.affectedByGravity = false
        playerPhysicsBody?.mass = 50
    }

    func spawnJumpButton() {
        scene?.addChild(jumpButton)
        jumpButton.position = jumpButtonPosition
        jumpButton.zPosition = 0.5
        jumpButton.size = CGSize(width: 100, height: 100)
        jumpButton.name = "jumpButton"
        jumpButton.isUserInteractionEnabled = false
    }

    func jumpPlayer() {
        playerPhysicsBody?.applyImpulse(playerJumpVector)
    }

    func spawnBackground() {
        scene?.addChild(background1)
        background1.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        background1.position.x = 0
        background1.zPosition = 0
        background1.size = CGSize(width: 3240, height: 1080)

        scene?.addChild(background2)
        background2.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        background2.position.x = 3240
        background2.zPosition = 0
        background2.size = CGSize(width: 3240, height: 1080)

        scene?.addChild(background3)
        background3.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        background3.position.x = 6480
        background3.zPosition = 0
        background3.size = CGSize(width: 3240, height: 1080)
    }

    override func update(_ currentTime: TimeInterval) {

        if cam.position.x > background2.position.x {
            background1.position.x = background3.position.x + 3240
        }
        if cam.position.x > background3.position.x {
            background2.position.x = background1.position.x + 3240
        }
        if cam.position.x > background1.position.x {
            background3.position.x = background2.position.x + 3240
        }
        camPosition.x = playerPosition.x
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            if let touch = touches.first {
                if selectedButton == "nodeName" {
                    if jumpButton.contains(touch.location(in: self)) {
                       jumpPlayer()
                    }
                }
            }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        selectedButton = "nodeName"
    }
}

Я пытаюсь заставить игрока прыгать с помощью кнопки, когдащелкнул игрок должен прыгать около 20 пикселей в воздухе.То же самое относится и к бегущему движению игрока, он не может заставить его работать вечно без анимации, с которой вы не можете делать другие вещи одновременно.

1 Ответ

0 голосов
/ 18 марта 2019

player.physicalBody - ноль, и его нужно обрабатывать!

   func spawnPlayer() {
    scene?.addChild(player)
    player.position = camPosition
    player.zPosition = 1
    player.size = playerSize

    //Here adding two lines
    player.physicsBody =  SKPhysicsBody()
    playerPhysicsBody?.linearDamping = 1

     playerPhysicsBody?.affectedByGravity = false
    playerPhysicsBody?.mass = 50
     }


     func jumpPlayer() {
    //Here changing one line
    playerPhysicsBody?.applyImpulse(playerJumpVector)
     }
...