如何创建跟随SWIFT中角色的背景?

如何创建跟随SWIFT中角色的背景?,swift,animation,background,sprite-kit,Swift,Animation,Background,Sprite Kit,我在Xcode中从事这个项目,我在那里制作动画。我有一个完美的工作程序,小角色在屏幕上走动。如果我按下屏幕上的键,这就是角色前进的方向。我想在这里做的是添加一个背景图像,让它跟随角色的方向。基本上,我希望角色能够在不离开图像的情况下在大地图上走动。类似于scrollView,但仅在游戏场景中 另外,下面的所有代码都正常工作,我只想添加移动的背景图像 下面是我的主要游戏场景的一些代码 override func didMoveToView(view: SKView) { /* Se

我在Xcode中从事这个项目,我在那里制作动画。我有一个完美的工作程序,小角色在屏幕上走动。如果我按下屏幕上的键,这就是角色前进的方向。我想在这里做的是添加一个背景图像,让它跟随角色的方向。基本上,我希望角色能够在不离开图像的情况下在大地图上走动。类似于scrollView,但仅在游戏场景中

另外,下面的所有代码都正常工作,我只想添加移动的背景图像

下面是我的主要游戏场景的一些代码

    override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    // the hero is initialized with a texture
    hero = Player(named: "hero_walk_down_0")
    hero.position = CGPoint(x: self.frame.midX, y: self.frame.midY)
    self.addChild(hero)
    if Settings.sharedInstance.virtualPad {
        controller = ControllerNode(position: CGPoint(x:0, y: 0))
        self.addChild(controller)
    }
    backgroundColor = SKColor.blueColor()
}


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        if Settings.sharedInstance.virtualPad == false {
            hero.destination = location
        }
    }
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let previousLocation = touch.previousLocationInNode(self)
        if Settings.sharedInstance.virtualPad == false {
            hero.destination = location
        }
    }
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    touchesEndedOrCancelled(touches, withEvent: event)
}

override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
    touchesEndedOrCancelled(touches, withEvent: event)
}

func touchesEndedOrCancelled(touches: NSSet, withEvent event: UIEvent) {
    // when a touch is ended, remove it from the list
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let previousLocation = touch.previousLocationInNode(self)
        if Settings.sharedInstance.virtualPad == false {
            hero.destination = nil
        }
    }
}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */
    if Settings.sharedInstance.virtualPad {
        hero.walk(self.controller!.pressedDirections())
    }
    else {
        if hero.destination != nil {
            hero.walkTowards(hero.destination!)
        }
    }
}
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
//英雄用纹理初始化
英雄=玩家(名为:“英雄\u步行\u下降\u 0”)
hero.position=CGPoint(x:self.frame.midX,y:self.frame.midY)
self.addChild(英雄)
if Settings.sharedInstance.virtualPad{
控制器=控制器节点(位置:CGPoint(x:0,y:0))
self.addChild(控制器)
}
backgroundColor=SKColor.blueColor()
}
覆盖func touchesBegined(触摸:设置,withEvent事件:UIEvent){
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
如果Settings.sharedInstance.virtualPad==false{
hero.destination=位置
}
}
}
覆盖功能触摸移动(触摸:设置,withEvent事件:UIEvent){
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
让previousLocation=touch.previousLocationInNode(自身)
如果Settings.sharedInstance.virtualPad==false{
hero.destination=位置
}
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent){
ToucheSendor取消(Touchs,withEvent:event)
}
覆盖func touchesCancelled(touchs:Set!,withEvent:UIEvent!){
ToucheSendor取消(Touchs,withEvent:event)
}
func touchesEndedOrCancelled(触摸:NSSet,带事件:UIEvent){
//触摸结束后,将其从列表中删除
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
让previousLocation=touch.previousLocationInNode(自身)
如果Settings.sharedInstance.virtualPad==false{
hero.destination=nil
}
}
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
if Settings.sharedInstance.virtualPad{
hero.walk(self.controller!.pressedDirections())
}
否则{
如果hero.destination!=零{
英雄。走向(英雄。目的地!)
}
}
}

创建两个SKNode—一个用于角色,一个用于背景,并将它们分别添加到每个组中


无论何时移动角色的SKNode,都会在背景节点上复制相同的操作。

如果将背景图像添加到SKScene(self)而不是另一个节点,它将始终存在。