The*_*Art 5 ios sprite-kit swift
所以我有一个基础游戏设置,可以在下面的bitbucket链接找到:
我目前很难理解如何翻译相对于场景布局的相机节点.
目标是让摄像机跟随播放器,直到达到由场景大小定义的角落边界.在此特定测试场景设置中,场景大小为1000x1000,相机比例为1.
下面的代码用于在新位置设置为跟随角色时修改摄像机的位置:
var cameraPosition: CGPoint {
get {
return CGPoint(x: camera!.position.x, y: camera!.position.y)
}
set {
let cameraScale = CGFloat(1)
let sceneScale = CGFloat(1)//CGFloat(1 - 0.44 + 0.05 /*possible rounding error adjustment*/)
// let viewAspectRatio = CGRectGetWidth(view!.frame)/CGRectGetHeight(view!.frame)
let newPositionValue = double2(x: Double(newValue.x * sceneScale), y: Double(newValue.y * sceneScale))
let scaledSceneSize = CGSize(width: size.width * sceneScale , height: size.height * sceneScale)
//// scaledSceneSize.height = scaledSceneSize.height / viewAspectRatio
let cameraSize = view!.bounds.size
let scaledCameraSize = CGSize(width: cameraSize.width * cameraScale, height: cameraSize.height * cameraScale)
let minX = 0//-scaledSceneSize.width * anchorPoint.x + scaledCameraSize.width / 2
let minY = -219//-scaledSceneSize.height * anchorPoint.y + scaledCameraSize.height / 2
let minValues = double2(x: Double(minX), y: Double(minY))
let maxX = 0//(scaledSceneSize.width * anchorPoint.x - scaledCameraSize.width / 2) //size.width - cameraSize.width / 2
let maxY = 219//(scaledSceneSize.height * anchorPoint.y - scaledCameraSize.height / 2) //- cameraSize.height / 2
let maxValues = double2(x: Double(maxX), y: Double(maxY))
let clampedPosition = clamp(newPositionValue, min: minValues, max: maxValues)
camera!.position = CGPoint(x: (clampedPosition.x / Double(sceneScale)), y: (clampedPosition.y / Double(sceneScale)))
}
}
Run Code Online (Sandbox Code Playgroud)
目前有适合所需场景大小的硬核值,我不确定如何通过比例获得这些结果.默认情况下,比例为:
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
Run Code Online (Sandbox Code Playgroud)
如果不知道在规模上有翻译,默认情况下,我希望边界为maximumSceneDimensionXValue - cameraSize.width/2 largestSceneDimensionYValue - cameraSize.height/2
作为一个高级的例子.有人能帮助我获得这个翻译吗?
VS在相机中出现黑色背景溢出:
我没有使用你的代码。我制作了一个示例项目并使其正常工作。
这是我的代码
import SpriteKit
class GameScene: SKScene {
let world = SKSpriteNode(imageNamed: "world.jpg")
let player = SKSpriteNode(color: SKColor.greenColor(), size: CGSizeMake(10, 10))
var cam: SKCameraNode!
override init(size: CGSize) {
super.init(size: size)
print(world.size)
addChild(world)
addChild(player)
world.zPosition = 1
player.zPosition = 2
cam = SKCameraNode()
self.camera = cam
addChild(cam)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
player.position = location
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
player.position = location
}
}
func clampCamera(){
func clamp(inout input: CGFloat, num1: CGFloat, num2: CGFloat) {
if input < num1 {
input = num1
}
else if input > num2 {
input = num2
}
}
let lBoundary = -world.size.width/2 + size.width/2
let rBoundary = world.size.width/2 - size.width/2
let bBoundary = -world.size.height/2 + size.height/2
let tBoundary = world.size.height/2 - size.height/2
clamp(&camera!.position.x, num1: lBoundary, num2: rBoundary)
clamp(&camera!.position.y, num1: bBoundary, num2: tBoundary)
}
override func update(currentTime: NSTimeInterval) {
camera!.position = player.position
clampCamera()
}
}
Run Code Online (Sandbox Code Playgroud)
这是我用作“世界”的相同图像 https://i.stack.imgur.com/zmDb9.jpg
归档时间: |
|
查看次数: |
1665 次 |
最近记录: |