繁体   English   中英

Swift Spritekit滚动背景

[英]Swift Spritekit Scrolling Background

我试图找到最好的方法来在Swift中垂直滚动背景,而不使用Update方法。 背景是1个应该无限循环的图像。 图片应位于屏幕中间(全尺寸),向下移动,并且应始终在该图片的顶部生成另一个图片。

但是,我的工作不完全正确,它生成了2张图片,然后经过一段时间后,它们又弹出并再次启动“动作”。 另外,有时它会从30 FPS下降到26 FPS,我想避免这种情况。 我希望更多地喜欢Swift的人能为我提供帮助。

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)
    }

这是我用来制作在屏幕上从右到左移动背景的代码。 它使用循环来生成三个背景,所有这些背景形成一条线,该线穿过屏幕并在完成后返回其原始位置。 要将其转换为从上到下移动的屏幕,您必须将moveByX SKAction替换为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)
    }
}

我写了这个……可能不算太短,但是它只使用了2个图像副本,这与上面发布的一个最佳答案不同。 它还允许您指定方向(向右滚动为true,向左滚动为false)和速度(指示图像在屏幕上滚动需要多少秒,数字越小=速度越快)。 它也需要您指定视图,并且我还没有使用与所需输出大小不同的图像对其进行测试(尽管我包括了缩放比例,所以它可能可以工作)

func scrollingBackground (view: SKView, rightward: Bool, speed: CGFloat) {

    let background = SKSpriteNode(imageNamed: "space")
    let background2 = SKSpriteNode(imageNamed: "space")

    // variables to manage x position, will be set based on which
    // direction this will be moving
    var b1startX, b2startX, destination, resetPos: CGFloat

    // Set values based on desired direction
    if(rightward){
        b1startX = frame.size.width * -0.5      // value to start offscreen
        b2startX = frame.size.width * 0.5       // value to start on screen
        destination = frame.size.width * 1.5    // position where image will disappear
        resetPos = frame.size.width * -0.5      // position where image will reappear
    }
    else{
        b1startX = frame.size.width * 1.5
        b2startX = frame.size.width * 0.5
        destination = frame.size.width * -0.5
        resetPos = frame.size.width * 1.5
    }

    background.position = CGPoint(x: b1startX, y: frame.size.height / 2)
    background.scale(to: view.bounds.size)
    background.zPosition = -1

    background2.position = CGPoint(x: b2startX, y: frame.size.height / 2)
    background2.scale(to: view.bounds.size)
    background2.zPosition = -1

    // Define actions based on desired direction
    let scrollFromMid = SKAction.moveTo(x: destination, duration: TimeInterval(speed))
    let scrollFromLeft = SKAction.moveTo(x: destination, duration: TimeInterval(speed * 2.0))
    let resetPosition = SKAction.moveTo(x: resetPos, duration: 0)

    //background starts in the default starting position, totally offscreen
    background.run(SKAction.repeatForever(SKAction.sequence([scrollFromLeft, resetPosition])))

    // background2 starts fully displayed, moves offscreen, then into default loop
    background2.run(SKAction.sequence([scrollFromMid, resetPosition,
                    SKAction.repeatForever(
                        SKAction.sequence([scrollFromLeft, resetPosition]))
                    ]))

    addChild(background)
    addChild(background2)

}

当您创建BackgroundReset操作时,请将y更改设置为负值,这会将背景进一步移到屏幕下方。 删除减号可将背景移回屏幕。

let BackgroundReset = 
SKAction.moveByX(0, y: /* removed minus sign here */ self.frame.size.height * 2, duration: 0.0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM