gpt4 book ai didi

ios - 如何在 SpriteKit 中创建一个有限的、可滚动的背景?

转载 作者:可可西里 更新时间:2023-11-01 01:09:21 26 4
gpt4 key购买 nike

我希望在 Sprite Kit 中有一个可滚动的背景。我尝试了一些 other在线提供解决方案,但他们正在实现无限滚动背景,我无法根据我的需要调整代码。

这里是一些示例代码,我必须尝试让背景移动(没有检测到到达背景的尽头)——但它非常不稳定,一点也不平滑。

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touchLocation = touches.first?.location(in: self), let node = nodes(at: touchLocation).first {
if node.name != nil {
if node.name == "background" {
background.position = touchLocation
}
}
}
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touchLocation = touches.first?.location(in: self), let node = nodes(at: touchLocation).first {
if node.name != nil {
if node.name == "background" {
background.position = touchLocation
}
}
}
}

下图展示了我正在努力实现的目标 - 我希望代码能够检测到您何时到达背景的尽头,并防止您进一步移动它。

Concept for what I'm trying to achieve

最佳答案

所以,取 @KnightOfDragon's考虑到需要为背景设置最大和最小 X 坐标值,我能够解决我自己的问题。我的代码中已经有向左/向右滑动的识别器(用于我的游戏中的另一个目的),并且我能够重用它们来满足我的需要。代码如下:

didMove() 中:

swipeRightRec.addTarget(self, action: #selector(self.swipedRight) )
swipeRightRec.direction = .right
self.view!.addGestureRecognizer(swipeRightRec)

swipeLeftRec.addTarget(self, action: #selector(self.swipedLeft) )
swipeLeftRec.direction = .left
self.view!.addGestureRecognizer(swipeLeftRec)

然后是这些函数:

@objc func swipedRight() {
if background.position.x + 250 > maxBackgroundX {
let moveAction = SKAction.moveTo(x: maxBackgroundX, duration: 0.3)
background.run(moveAction)
} else {
let moveAction = SKAction.moveTo(x: background.position.x + 250, duration: 0.3)
background.run(moveAction)
}
}

@objc func swipedLeft() {
if background.position.x - 250 < minBackgroundX {
let moveAction = SKAction.moveTo(x: minBackgroundX, duration: 0.3)
background.run(moveAction)
} else {
let moveAction = SKAction.moveTo(x: background.position.x - 250, duration: 0.3)
background.run(moveAction)
}
}

是的,这意味着每次滑动时背景都会移动一定量,无论滑动有多大,但这正是我的游戏所需要的。我希望这可以帮助其他需要同样东西的人!

关于ios - 如何在 SpriteKit 中创建一个有限的、可滚动的背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48989424/

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