我试图找到在Swift中垂直滚动背景的最佳方法,而不使用更新方法.背景是1应该循环无限的图像.图像应位于屏幕中间(全尺寸),向下移动,并且该图像顶部应始终是另一个图像生成.
然而,我的工作不正常,它会产生2张图像,而且在延迟之后它们会弹出并再次启动动作.此外,它有时会从30 FPS下降到26,这是我想要避免的.我希望有人能够更好地帮助我.
let Background = SKTexture(imageNamed: "Background.png")
Background.filteringMode = .Nearest
let BackgroundMove = SKAction.moveByX(0, y: -self.frame.size.height, duration: 10)
let BackgroundReset = SKAction.moveByX(0, y: self.frame.size.height, duration: 0.0)
let BackgroundMoveAndResetForever = SKAction.repeatActionForever(SKAction.sequence([BackgroundMove,BackgroundReset]))
for var i:CGFloat = 0; i < 2.0 + self.frame.size.height / (Background.size().height * 2); ++i {
let sprite = SKSpriteNode(texture: Background)
sprite.size = CGSize(width: self.frame.size.width / 2, height: self.frame.size.height)
sprite.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2 * i)
sprite.zPosition = 1
sprite.runAction(BackgroundMoveAndResetForever)
self.addChild(sprite)
}
Run Code Online (Sandbox Code Playgroud)
这是我用来制作在屏幕上从右到左移动的背景的代码.它使用一个循环来生成三个背景,所有这些背景形成一条线,当它们完成时,它穿过屏幕并返回到它们的原始位置.要将其转换为从上到下移动的屏幕,您必须将moveByXSKActions 替换为moveToY,并使用更改的y值交换更改的x值.希望这可以帮助!
func makeBackground() {
var backgroundTexture = SKTexture(imageNamed: "img/bg.png")
//move background right to left; replace
var shiftBackground = SKAction.moveByX(-backgroundTexture.size().width, y: 0, duration: 9)
var replaceBackground = SKAction.moveByX(backgroundTexture.size().width, y:0, duration: 0)
var movingAndReplacingBackground = SKAction.repeatActionForever(SKAction.sequence([shiftBackground,replaceBackground]))
for var i:CGFloat = 0; i<3; i++ {
//defining background; giving it height and moving width
background=SKSpriteNode(texture:backgroundTexture)
background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * i), y: CGRectGetMidY(self.frame))
background.size.height = self.frame.height
background.runAction(movingAndReplacingBackground)
self.addChild(background)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5689 次 |
| 最近记录: |