使用地图边界约束相机并固定y位置 - SpriteKit

时间:2016-09-26 15:29:08

标签: ios swift camera sprite-kit position

我目前正在制作一款游戏,该游戏具有垂直tileMap和屏幕底部的字符,只能在x轴上移动。目前为了使游戏看起来像是滚动我给了我的角色dx:500的速度让他沿着tileMap旅行。

当我运行模拟器时,相机会从所有内容的左下方开始,然后看起来它正在赶上我的场景内容。我该如何解决这个问题?

我尝试使用相机跟踪播放器,当他朝着地图的边缘移动时(在x轴上)并且它只能部分地工作。如何让摄像机一直跟随节点直到场景边缘并在到达终点时停止?我无法弄清楚如何让相机边界停在x轴上。

另外,我希望我的玩家在整个游戏中处于相同的y坐标,让他水平移动(屏幕上大约70%)。如何实现我的速度以将他移动到地图上,并使他保持恒定的y轴值?

class GameScene: SKScene, SKPhysicsContactDelegate {
var worldNode = SKNode()
var bgNode = SKNode()
var fgNode = SKNode()
var cameraNode: SKNode!
var background: SKNode!
var player: Character1Node!
var platform: SKTileMapNode!
var previousTranslateX: CGFloat = 0.0


override func didMove(to view: SKView) {
    setupNodes()
    self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    cameraNode.position = CGPoint(x: size.width/2, y: size.height/2)

    //Panning for dragging player
    let pan = UIPanGestureRecognizer(target: self, action: #selector(GameScene.dragPlayer(sender:)))
    self.view?.addGestureRecognizer(pan)

}

func dragPlayer(sender: UIPanGestureRecognizer) {

    //retrieve pan movement along the x-axis of the view since the gesture began
    let currentTranslateX = sender.translation(in: view!).x

    //calculate translation since last measurement
    let translateX = currentTranslateX - previousTranslateX

    //move shape
    let adjustment:CGFloat = 2.0
    player.parent!.position = CGPoint(x: player.parent!.position.x + (translateX*adjustment), y:player.parent!.position.y)


    //reset previous measuremnt
    if sender.state == .ended{
        previousTranslateX = 0
    } else {
        previousTranslateX = currentTranslateX
    }
}



func setupNodes(){

    //Connecting variables to scene
    worldNode = childNode(withName: "World")!
    bgNode = worldNode.childNode(withName: "Background")!
    background = bgNode.childNode(withName: "Overlay")!.copy() as! SKNode
    fgNode = worldNode.childNode(withName: "Foreground")!
    cameraNode = worldNode.childNode(withName: "Camera")!


    //Platform physics
    platform = fgNode.childNode(withName: "Level1Map") as! SKTileMapNode
    let bodySize = CGSize(width: 150, height: 190)
    platform.physicsBody = SKPhysicsBody(rectangleOf: bodySize)
    platform.physicsBody!.isDynamic = true
    platform.physicsBody!.affectedByGravity = false
    platform.physicsBody!.linearDamping = 0
    platform.physicsBody!.categoryBitMask = PhysicsCategory.Platform
    platform.physicsBody!.collisionBitMask = PhysicsCategory.None
    platform.physicsBody!.velocity = CGVector(dx: 0, dy: 0)

    enumerateChildNodes(withName: "//*", using: {node, _ in
        if let customNode = node as? CustomNodeEvents {
            customNode.didMoveToScene()
        }
    })

    player = platform.childNode(withName: "//character1") as! Character1Node

}

override func update(_ currentTime: TimeInterval) {
    let position = player.parent!.position

    //Platform Tiles
    let column = platform.tileColumnIndex(fromPosition: position)
    let row = platform.tileRowIndex(fromPosition: position)
    let objectTile = platform.tileDefinition(atColumn: column, row: row)
    //print(objectTile)

    if let _ = objectTile?.userData?.value(forKey: "tile2"){
        print("jumping tile")
    }

    if let _ = objectTile?.userData?.value(forKey: "tile1"){
        print("platform tile")
    }

    print(player.parent!.position)


}

override func didSimulatePhysics() {
    cameraNode.position = CGPoint(x: player.parent!.position.x, y: player.parent!.position.y)
    self.centerOnNode(node: cameraNode)
}

func centerOnNode(node: SKNode) {
    let cameraPositionInScene: CGPoint = node.scene!.convert(node.position, from: worldNode)
    node.parent!.run(SKAction.move(to: CGPoint(x:node.parent!.position.x - cameraPositionInScene.x, y:node.parent!.position.y - cameraPositionInScene.y), duration: 2.0))

}

1 个答案:

答案 0 :(得分:-1)

从GameScene.sks中移除相机节点以及代码中的引用。确保从GameScene.sks右侧属性检查器中的所选摄像机中删除“Camera”。用以下内容替换GameScene中所有cameraNode实例:

在其他变量声明附近添加:

var cameraNode: SKCameraNode!

在didMove中(查看:...)添加:

cameraNode = SKCameraNode()
camera = cameraNode
player.addChild(cameraNode)

这将导致cameraNode相对于玩家的位置。无论走到哪里,都会跟随玩家!