xcode 无尽的滚动(重复)背景在Spritekit游戏-斯威夫特

6fe3ivhb  于 2023-01-31  发布在  其他
关注(0)|答案(7)|浏览(102)

我想为我的spritekit游戏创建一个无限滚动的背景,它应该由一个或两个图像组成,可能会重复它们自己?我找到了这些onetwo的例子,但它们是在obj. C中。
我不知道我如何在Swift中实现这一点。而且有没有可能手动设置速度?
附言:我没有把obj.c转换成swift的技能(Xcode开发新手)

kwvwclae

kwvwclae1#

我知道这是迟到的游戏,但我发现如何做到这一点水平以及!
从Egghead的代码开始(干得好!)我修改了一些东西:

let background1 = SKSpriteNode(imageNamed: "Street_Example")
    let background2 = SKSpriteNode(imageNamed: "Street_Example")

还有:

background1.position = CGPoint(x: frame.size.width / 2, y:frame.size.height / 2)
    background1.size = CGSize(width: frame.width, height: frame.height)
    background1.anchorPoint = CGPointZero
    background1.position = CGPointMake(0, 0)
    background1.zPosition = -15
    self.addChild(background1)

    background2.size = CGSize(width: frame.width, height: frame.height)
    background2.anchorPoint = CGPointZero
    background2.position = CGPointMake(background1.size.width - 1,0)
    background2.zPosition = -15
    self.addChild(background2)

并更新背景的位置:

background1.position = CGPointMake(background1.position.x-2, background1.position.y)
    background2.position = CGPointMake(background2.position.x-2, background2.position.y)
    if(background1.position.x < -background1.size.width)
    {
      background1.position = CGPointMake(background1.position.x + background2.size.width , background2.position.y)
    }
    if(background2.position.x < -background2.size.width)
    {
      background2.position = CGPointMake(background2.position.x + background1.size.height, background1.position.y) 
    }
nwlqm0z1

nwlqm0z12#

我找到了一种方法,不知何故,我设法将this obj.C转换为swift
必须公开声明这两个节点的

let background1 = SKSpriteNode(imageNamed: "bg1")
let background2 = SKSpriteNode(imageNamed: "bg2")

在“didMoveToView”方法中

background1.anchorPoint = CGPointZero
background1.position = CGPointMake(0, 0)
background1.zPosition = -15
self.addChild(background1)

background2.anchorPoint = CGPointZero
background2.position = CGPointMake(0, background1.size.height - 1)
background2.zPosition = -15
self.addChild(background2)

并在“覆盖函数更新(currentTime:CFTimeInterval)”方法添加

background1.position = CGPointMake(background1.position.x, background1.position.y - 2)
background2.position = CGPointMake(background2.position.x, background2.position.y - 2)

            if(background1.position.y < -background1.size.height)
            {
                background1.position = CGPointMake(background2.position.x, background1.position.y + background2.size.height )
            }

            if(background2.position.y < -background2.size.height)
            {
                background2.position = CGPointMake(background1.position.x, background2.position.y + background1.size.height)

            }

我不知道这是不是最有效的方法,其他的问题提到了一个For循环,但是在我看来这更简单。

i34xakig

i34xakig3#

我做了一个类,目前它是为Swift 5.0或者4.2写的,支持上下左右方向。
看看它是否对你有帮助,它叫做InfiniteScrollingBackground:https://github.com/ThiagoAM/InfiniteScrollingBackground

jtjikinw

jtjikinw4#

你不需要这些。
只需将此函数与您声明的变量和didMoveToView中使用的属性一起使用。

func backgroudScrollUpdate(){
    background1.position = CGPointMake(background1.position.x, background1.position.y - 1)
    background2.position = CGPointMake(background1.position.x, background2.position.y - 1)
    if background1.position.y == -UIScreen.mainScreen().bounds.height{
        background1.position = CGPointMake(0, 0)
        background2.position = CGPointMake(0, 0 + UIScreen.mainScreen().bounds.height)
    }
}

然后在update方法中调用它。
当然,这不是最好的方法,因为当你有更多的go for a循环时,它对两个图像都是可读的。

k5ifujac

k5ifujac5#

这是我得出的结论:

func terrain() -> SKNode {
        let color = UIColor.init(red: randomColorChannel(), green: randomColorChannel(), blue: randomColorChannel(), alpha: 1.0)

        let scale: CGFloat = 1.0// UIScreen.mainScreen().scale
        let size = CGSizeMake(frame.size.width * scale, frame.size.height * scale)
        let node = SKSpriteNode.init(color: color, size: size)
        node.anchorPoint = CGPointZero
        node.position = CGPointMake(0, UIScreen.mainScreen().bounds.size.height)

        return node
    }

并在更新中执行以下操作:

override func update(currentTime: NSTimeInterval) {
        var newPosition1 = terrain1.position
        var newPosition2 = terrain2.position

        newPosition1.y -= terrainSpeed
        if newPosition1.y < 0 {
            newPosition2.y -= terrainSpeed
        }

        terrain1.position = newPosition1
        terrain2.position = newPosition2

        if terrain1.position.y <= -UIScreen.mainScreen().bounds.size.height {
            removeChildrenInArray([terrain1])
            terrain1 = terrain()
            addChild(terrain1)
        }

        if terrain2.position.y <= -UIScreen.mainScreen().bounds.size.height {
            removeChildrenInArray([terrain2])
            terrain2 = terrain()
            addChild(terrain2)
        }
    }

所以基本上,地形是放在视图上方的,然后它们开始向下移动,并在需要时用新的来替换,重复。

i2loujxw

i2loujxw6#

func backgroudScrollUpdate(){
        BG .position = CGPoint(x: BG.position.x - 5, y: BG.position.y)
        BG2.position = CGPoint(x: BG2.position.x - 5, y: BG2.position.y)
        if BG2.position.x <= -self.frame.size.width {
            BG.position = CGPoint(x: self.frame.size.width, y: 0)
        }
        if BG.position.x <= -self.frame.size.width {
        BG2.position = CGPoint(x: self.frame.size.width, y: 0)
    }
}
  • 针对SWIFT 3.1更新-
    这是我一直用来使它工作的Y轴只是切换所有额外的东西在X位置到Y位置
ltqd579y

ltqd579y7#

var backgroundImageWidth: CGFloat = 3000

override func didMove(to view: SKView) {
    self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    for i in 0...2 {
        let backgroundImage =  SKSpriteNode(imageNamed: "NightSky")
        backgroundImage.name = "back"
        backgroundImage.size = CGSize(width: backgroundImageWidth, height: 1000)
        backgroundImage.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        backgroundImage.position = CGPoint(x: CGFloat(i)*backgroundImage.size.width, y:0)
        self.addChild(backgroundImage)

    }
}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
    self.enumerateChildNodes(withName: "back", using: ({
        (node, error) in
        node.position.x -= 2
        if node.position.x < -(2 * self.backgroundImageWidth) {
            node.position.x += 3 * self.backgroundImageWidth
        }
                               
    }))
}

相关问题