[英]How can I create a finite, scrollable background in SpriteKit?
I'm looking to have a scrollable background in Sprite Kit.我希望在 Sprite Kit 中有一个可滚动的背景。 I've had a go with some of the other solutions available online, but they were implementing infinite scrolling backgrounds, and I haven't been able to adapt the code to my needs.我尝试了一些其他在线可用的解决方案,但它们正在实现无限滚动背景,我无法根据我的需要调整代码。
Here is some sample code which I've got to try and get the background moving (without the detection of reaching the end of the background) - but it's very choppy and not smooth at all.这是一些示例代码,我必须尝试让背景移动(没有检测到到达背景的末端) - 但它非常不稳定而且根本不平滑。
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
}
}
}
}
The image below demonstrates what I'm trying to achieve - I want the code to detect when you've reached the end of the background, and to prevent you from moving it any further.下图展示了我想要实现的目标 - 我希望代码能够检测到您何时到达背景的末尾,并防止您进一步移动它。
So, taking @KnightOfDragon's comment into account about needing to set maximum and minimum X coordinate values for the background, I was able to solve my own question.因此,考虑到@KnightOfDragon关于需要为背景设置最大和最小 X 坐标值的评论,我能够解决我自己的问题。 I already had swipe left/right recognisers in my code (for another purpose in my game), and I was able to reuse these to fulfil my needs.我的代码中已经有向左/向右滑动识别器(在我的游戏中用于另一个目的),我能够重用这些来满足我的需求。 Code is as follows:代码如下:
In didMove()
:在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)
And then these functions:然后这些功能:
@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)
}
}
Yes this means that the background moves a set amount each time you swipe, no matter how big the swipe is, but it is exactly what I required for my game.是的,这意味着每次滑动时背景都会移动一定量,无论滑动有多大,但这正是我游戏所需要的。 I hope this helps someone else who needs the same thing!我希望这可以帮助其他需要同样事情的人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.