Spritekit 游戏中的无尽滚动(重复)背景 - Swift [英] Endless scrolling (repeating) background in Spritekit game - Swift

查看:29
本文介绍了Spritekit 游戏中的无尽滚动(重复)背景 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的 spritekit 游戏创建一个无限滚动的背景,它应该由一两张图片组成,它们会重复吗?我发现了这些 一个两个示例,但它们在obj中.C.

I want to Create a endless scrolling background for my spritekit game, iT should consist of one or two images probably, which repeat themselves? I found these one and two examples, but they are in obj. C.

我不知道如何在 Swift 中实现这一点.是否可以手动设置速度?

I have no idea how I can achieve this in Swift. And is it possible to set the speed manually?

Ps:我没有转换obj的技能.C 到 swift(Xcode 开发新手)

Ps: I don't have the skill to convert obj. C into swift (newbie to Xcode dev.)

推荐答案

我知道这对游戏来说已经很晚了,但我也找到了横向的方法!

I know this is late to the game, but I found how to do this horizontally as well!

从 Egg​​head 的代码开始(出色的工作!)我修改了一些东西:

Starting off with Egghead's code (Excellent work!) I modified some things:

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

这篇关于Spritekit 游戏中的无尽滚动(重复)背景 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆