gpt4 book ai didi

swift - 如何创建跟随 SWIFT 字符的背景?

转载 作者:行者123 更新时间:2023-11-28 09:08:41 27 4
gpt4 key购买 nike

我正在 Xcode 中处理这个项目,我正在其中制作动画。我有完美的工作程序,小角色在屏幕上走来走去。如果我在屏幕上按下,那就是角色前进的方向。我想在这里做的是添加背景图像并使其跟随角色的去向。基本上,我希望角色能够在不超出图像的情况下在大 map 上走动。类似于 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!)
}
}
}

最佳答案

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

无论何时移动角色的 SKNode,都会在后台节点上复制相同的 Action 。

关于swift - 如何创建跟随 SWIFT 字符的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30104460/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com