获取 SKCameraNode 以跟随英雄 Spritenode
Get SKCameraNode to follow a hero Spritenode
我好像想不通。我尝试了很多不同的方法,其中 none 似乎有效。使用我当前的代码,相机和英雄永远不会排成一行,当我触摸屏幕时,场景似乎跳得很远。我想要做的就是当我触摸屏幕时让英雄移动到触摸点并让相机跟随他。有什么方法可以将相机锁定到英雄 spritenode 吗?
import SpriteKit
let tileMap = JSTileMap(named: "level2.tmx")
let hero = SKSpriteNode(imageNamed: "hero")
let theCamera: SKCameraNode = SKCameraNode()
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.anchorPoint = CGPoint(x: 0, y: 0)
self.position = CGPoint(x: 0, y: 0)
hero.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
hero.xScale = 0.5
hero.yScale = 0.5
hero.zPosition = 2
tileMap.zPosition = 1
tileMap.position = CGPoint(x: 0, y: 0)
self.addChild(tileMap)
self.addChild(hero)
self.addChild(theCamera)
self.camera = theCamera
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
let action = SKAction.moveTo(location, duration: 1)
hero.runAction(action)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
self.camera?.position = hero.position
}
}
之所以看到画面跳得很远是因为scene.size
不等于屏幕尺寸。我猜你可能会像这样初始化你的第一个场景:
// GameViewController.swift
if let scene = GameScene(fileNamed:"GameScene") {...}
该代码默认加载大小为1024*768的GameScene.sks。但是由于您以编程方式添加 SKSpriteNode
,因此您可以通过这种方式初始化场景以适应屏幕大小:
// GameViewController.swift
// Only remove if statement and modify
let scene = GameScene(size: view.bounds.size) ...
这将解决您遇到的大部分问题。此外,我建议使用 SKAction
:
移动相机节点
override func update(currentTime: CFTimeInterval) {
let action = SKAction.moveTo(hero.position, duration: 0.25)
theCamera.runAction(action)
}
最后一件事,添加此行以在开始时将相机与您的英雄对齐:
self.camera?.position = hero.position
我好像想不通。我尝试了很多不同的方法,其中 none 似乎有效。使用我当前的代码,相机和英雄永远不会排成一行,当我触摸屏幕时,场景似乎跳得很远。我想要做的就是当我触摸屏幕时让英雄移动到触摸点并让相机跟随他。有什么方法可以将相机锁定到英雄 spritenode 吗?
import SpriteKit
let tileMap = JSTileMap(named: "level2.tmx")
let hero = SKSpriteNode(imageNamed: "hero")
let theCamera: SKCameraNode = SKCameraNode()
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.anchorPoint = CGPoint(x: 0, y: 0)
self.position = CGPoint(x: 0, y: 0)
hero.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
hero.xScale = 0.5
hero.yScale = 0.5
hero.zPosition = 2
tileMap.zPosition = 1
tileMap.position = CGPoint(x: 0, y: 0)
self.addChild(tileMap)
self.addChild(hero)
self.addChild(theCamera)
self.camera = theCamera
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
let action = SKAction.moveTo(location, duration: 1)
hero.runAction(action)
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
self.camera?.position = hero.position
}
}
之所以看到画面跳得很远是因为scene.size
不等于屏幕尺寸。我猜你可能会像这样初始化你的第一个场景:
// GameViewController.swift
if let scene = GameScene(fileNamed:"GameScene") {...}
该代码默认加载大小为1024*768的GameScene.sks。但是由于您以编程方式添加 SKSpriteNode
,因此您可以通过这种方式初始化场景以适应屏幕大小:
// GameViewController.swift
// Only remove if statement and modify
let scene = GameScene(size: view.bounds.size) ...
这将解决您遇到的大部分问题。此外,我建议使用 SKAction
:
override func update(currentTime: CFTimeInterval) {
let action = SKAction.moveTo(hero.position, duration: 0.25)
theCamera.runAction(action)
}
最后一件事,添加此行以在开始时将相机与您的英雄对齐:
self.camera?.position = hero.position