简体   繁体   中英

How to create background that follows the character in SWIFT?

I am working on this project in Xcode, where I am making animations. I have perfectly working program where little character walks around the screen. If I press on screen, that is direction the character is going. What I want to do here is to add a background image and make it follow where character is going. Basically, I want character to be able to walk around big map while he does not go out of the image. Something like scrollView, but only in gamescene.

PS All the code below is working, I just want to add the background image that moves along.

Here is some code for my main gamescene

    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!)
        }
    }
}

You create two SKNodes - one for the character and one for the background and add them to each group separately.

Whenever you move the SKNode of the character, you replicate the same action on the background node.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM