【发布时间】:2015-12-17 16:42:00
【问题描述】:
我似乎无法弄清楚这一点。我尝试了许多不同的东西,但似乎没有一个有效。使用我当前的代码,相机和英雄永远不会对齐,当我触摸屏幕时,场景似乎跳得很远。我想做的就是当我触摸屏幕时让英雄移动到触摸点并让相机跟随他。有没有办法将相机锁定到英雄精灵节点?
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
}
}
【问题讨论】:
-
你的意思是当你触摸屏幕时,
hero会移动到你触摸的位置,camera会跟随/对齐hero。结果,hero现在还出现在屏幕中间? -
是的,这正是我的意思。
标签: sprite-kit swift2 ios9 xcode7