Ios 制作垂直移动的背景
我是Spritekit的初学者,我尝试使背景垂直移动Ios 制作垂直移动的背景,ios,swift,swift3,sprite-kit,Ios,Swift,Swift3,Sprite Kit,我是Spritekit的初学者,我尝试使背景垂直移动 我知道怎么做 这是控制器中的什么 class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let scene = GameScene(size: view.bounds.size) let skView = view as! SKView
我知道怎么做 这是控制器中的什么
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene(size: view.bounds.size)
let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.ignoresSiblingOrder = true
scene.scaleMode = .resizeFill
skView.presentScene(scene)
}
这就是现场
class GameScene: SKScene {
var background = SKSpriteNode()
override func didMove(to view: SKView) {
let backgroundTexture = SKTexture(imageNamed: "bbbgg.png")
let shiftBackground = SKAction.moveBy(x: 0, y: -backgroundTexture.size().height, duration: 5)
let replaceBackground = SKAction.moveBy(x: 0, y:backgroundTexture.size().height, duration: 0)
let movingAndReplacingBackground = SKAction.repeatForever(SKAction.sequence([shiftBackground,replaceBackground]))
for i in 1...3{
background=SKSpriteNode(texture:backgroundTexture)
background.position = CGPoint(x: 0, y: 0)
background.size.height = self.frame.height
background.run(movingAndReplacingBackground)
self.addChild(background)
}
}
}
问题是背景水平移动,到达屏幕末尾后消失,然后再次从屏幕开始移动
我希望它垂直工作而不消失背景水平移动,因为moveBy(shiftBackground,replaceBackground)的声明在x轴上工作(水平)。您需要在y轴上工作(垂直) 在第一行,背景从它的位置移动到它的位置-它的高度。在第二行,它返回到第一个位置(我认为moveBy是相对的) _ 由于使用函数SKAction.repeatForever(movingAndReplacingBackground),背景将再次移动。我想你可以把它拿走
let movingAndReplacingBackground = SKAction.sequence([shiftBackground,replaceBackground])
编辑: 我忘了循环 在循环中,给背景一个初始位置
background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * CGFloat(i)), y: self.frame.midY)
如果您想要一个立即可见的背景,请尝试将其替换为0,中间
background.position = CGPoint(x: 0, y: self.frame.midY)
或-height,如果希望背景显示在屏幕顶部,则为0。在这种情况下,您需要替换shiftBackground和replaceBackground
background.position = CGPoint(x: -backgroundTexture.size().height, y: 0)
and
let shiftBackground = SKAction.moveBy(x: 0, y: backgroundTexture.size().height, duration: 5)
let replaceBackground = SKAction.moveBy(x: 0, y: 0, duration: 0)
我不确定,但我认为,如果您只需要一个动作,您可以删除该序列:
class GameScene: SKScene {
var background = SKSpriteNode()
override func didMove(to view: SKView) {
let backgroundTexture = SKTexture(imageNamed: "bbbgg.png")
let scrollByTopBackground = SKAction.moveBy(x: O, y: backgroundTexture.size().height, duration: 5)
background = SKSpriteNode(texture:backgroundTexture)
background.position = CGPoint(x: -backgroundTexture.size().height, y: self.frame.midY)
background.size.height = self.frame.height
background.run(scrollByTopBackground)
self.addChild(background)
}
}
编辑以回答您的问题,请尝试此
class GameScene: SKScene {
var background = SKSpriteNode()
override func didMove(to view: SKView) {
let backgroundTexture = SKTexture(imageNamed: "bbbgg.png")
let shiftBackground = SKAction.moveBy(x: 0, y: -backgroundTexture.size().height, duration: 5)
let replaceBackground = SKAction.moveBy(x: 0, y:backgroundTexture.size().height, duration: 0)
let movingAndReplacingBackground = SKAction.repeatForever(SKAction.sequence([shiftBackground,replaceBackground]))
for i in 0...2 {
background=SKSpriteNode(texture:backgroundTexture)
background.position = CGPoint(x: 0, y: self.frame.midY + (backgroundTexture.size().height * CGFloat(i)))
background.size.height = self.frame.height
background.run(movingAndReplacingBackground)
self.addChild(background)
}
}
我做了这些更改,但现在什么也没有出现,我还希望在运行应用程序时,第一个背景存在,之后移动开始。我希望移动永远重复,因为我使用
repeatForever
在您的评论后更新我的代码,现在的问题是:它是从上到下移动的,但它是从屏幕中间开始移动的,而不是从顶部开始移动,而且我认为在移动之间应用程序会停止几秒钟,为什么?在你第一个回答中的问题你有这个背景。位置=CGPoint(x:backgroundTexture.size().width/2+(backgroundTexture.size().width*CGFloat(i)),y:self.frame.midY)现在你有了background.position=CGPoint(x:0,y:0)我们可以尝试混合background.position=CGPoint(x:0,y:self.frame.midY)Thx,这很有效!但是我仍然看到每一个动作之间的空白,在下一个背景出现之前需要几秒钟,我希望下一个背景立即出现在旧的背景消失之后,在它们之间看不到任何空白或空白,这是可能的吗?
class GameScene: SKScene {
var background = SKSpriteNode()
override func didMove(to view: SKView) {
let backgroundTexture = SKTexture(imageNamed: "bbbgg.png")
let shiftBackground = SKAction.moveBy(x: 0, y: -backgroundTexture.size().height, duration: 5)
let replaceBackground = SKAction.moveBy(x: 0, y:backgroundTexture.size().height, duration: 0)
let movingAndReplacingBackground = SKAction.repeatForever(SKAction.sequence([shiftBackground,replaceBackground]))
for i in 0...2 {
background=SKSpriteNode(texture:backgroundTexture)
background.position = CGPoint(x: 0, y: self.frame.midY + (backgroundTexture.size().height * CGFloat(i)))
background.size.height = self.frame.height
background.run(movingAndReplacingBackground)
self.addChild(background)
}
}