2017-05-12 1 views
2

카메라로 플레이어를 움직일 수는 있지만 원하는대로 작동하지 않습니다. 내가 설정 한 단어 경계 :지연이있는 스프라이트 키트 카메라 부드럽게 움직입니다.

func keepPlayerInBounds() { 
    if player.position.x < frame.minX + player.size.width/2 { 
     player.position.x = frame.minX + player.size.width/2 
    } 

    if player.position.x > frame.maxX + player.size.width/2 { 
     player.position.x = frame.maxX + player.size.width/2 
    } 
} 

그래서 나는 최대로에 있어야하며 worldNode에 X 플레이어의 위치를 ​​혼합 카메라의 최대 및 최소 X가 필요합니다. 그리고 플레이어가 오른쪽이나 왼쪽으로 움직이면서 딜레이로 부드럽게 움직입니다.

override func didFinishUpdate() 
    cam.position.x = player.position.x 
} 

또는 : 내가 설정하려고했다

override func didFinishUpdate() { 
    let move = SKAction.moveTo(x: player.position.x, duration: 0.5) 
    cam.run(move) 
} 

을하지만 필요한 것보다 더 많은 두통을 제공합니다.

문제는 카메라에 왼쪽 및 오른쪽 최대 위치를 설정하고 버그없이 지연시키면서 이동하는 것입니다. 나는 대답을 찾기 위해 거의 3 주를 보냈지 만 여전히 아무것도 얻지 못했습니다. 감사!

+0

"부드러운 움직임"으로 무엇을 이해합니까? 어떤 버그 또는 두통이 발생합니까? – Marc

+0

빨리 움직이거나 약간의 장애가 있습니다. –

답변

1

경계에서 플레이를 유지하는 쉬운 방법은 플레이어에게 물리 본문을 설정하고 경계면에 물리 물리 체를 설정하는 것입니다.

var player: SKSpriteNode! //player variable 
var map: SKSpriteNode! //this simple node called map is a variable that represents the bounds area, for the example i made it the size of the scene (inside GameScene.sks) 

override func didMove(to view: SKView) { 
    player = childNode(withName: "player") as! SKSpriteNode // initializing the player from the scene file. 
    player.physicsBody = SKPhysicsBody(rectangleOf: player.size) // initializing player physics body(in this example the player is a simple rectangle). 

    map = childNode(withName: "map") as! SKSpriteNode // initializing the map from the scene file. 
    map.physicsBody = SKPhysicsBody(edgeLoopFrom: map.frame) // instead of assigning a physics body to the scene it self, we created the map variable and assign the physics body to it, the edgeLoopFrom physics body is a static volume-less body, so the player will not be able to pass through. 

    setupCamera() // for the second part of your question we create this method and call it right here in viewDidLoad(). 
} 

update() 메서드에서 카메라 위치를 계속 업데이트하는 대신 카메라 제약 조건을 추가하기 만하면됩니다. (장면 파일에 카메라를 추가했습니다.)

func setupCamera() { 
    guard let camera = camera, let view = view else { return } // make sure we have a camera and a view or else return 

    let zeroDistance = SKRange(constantValue: 0) 
    let playerConstraint = SKConstraint.distance(zeroDistance, 
               to: player) // as the name suggest this is a simple constraint for the player node. 
    //next part of the method will assign a second constraint to the camera which will prevent the camera from showing the dark area of the scene in case the player will go to the edge. you don't have to add this part but it is recommended. 
    let xInset = min(view.bounds.width/2 * camera.xScale, 
        map.frame.width/2) 
    let yInset = min(view.bounds.height/2 * camera.yScale, 
        map.frame.height/2) 

    let constraintRect = map.frame.insetBy(dx: xInset, 
                dy: yInset) 

    let xRange = SKRange(lowerLimit: constraintRect.minX, 
         upperLimit: constraintRect.maxX) 
    let yRange = SKRange(lowerLimit: constraintRect.minY, 
         upperLimit: constraintRect.maxY) 

    let edgeConstraint = SKConstraint.positionX(xRange, y: yRange) 
    edgeConstraint.referenceNode = map 

    camera.constraints = [playerConstraint, edgeConstraint] //finally we add the constraints we created to the camera, notice that the edge constraint goes last because it has a higher priority. 
}