【问题标题】:Background images flickering in a SpriteKit app using Swift使用 Swift 在 SpriteKit 应用程序中闪烁的背景图像
【发布时间】:2015-11-03 20:18:51
【问题描述】:

我在 sprite kit 应用中有几层背景、中景、前景、地面等。

我已经设法让它们循环播放,但问题是当它们加载时它们会闪烁并且不能从一张图像平滑过渡到另一张图像。

我也遇到了图像之间出现间隙的问题,但通过在每个图像的宽度上添加一个额外的像素来解决这个问题。

闪烁不会每次都发生,它会循环播放,似乎与图像的大小(帧或视图)、从宽度中移除多余的像素、改变速度、改变设备等。

此外,Z 位置没问题,游戏视图控制器中的忽略兄弟顺序对解决它没有任何影响。

有人知道为什么会这样吗?我的代码中是否有什么我做错了,或者其他人是否遇到了与 SpriteKit 类似的情况?我在其他地方找不到明确的比较。

这是我受影响的代码示例:

在viewDidLoad中:

//Declaration of the midground nodes.

var mgImage: SKSpriteNode = SKSpriteNode()
var mgImage2: SKSpriteNode = SKSpriteNode()

在 didMoveToView 中:

    //Creates an instance of both sprites that are of the same image that are to be lined up against each other in the x axis creating the illution of continuity.

    mgImage = SKSpriteNode(imageNamed: "CityMid.png")
    mgImage2 = SKSpriteNode(imageNamed: "CityMid.png")

    //Specifies the Z position of the images.

    mgImage.zPosition = 1
    mgImage2.zPosition = 1

    //Scales the image to the correct size of the screen.

    mgImage.size.width = self.frame.width + 1
    mgImage.size.height = self.frame.height

    mgImage2.size.width = self.frame.width + 1
    mgImage2.size.height = self.frame.height

    //Specicies the x position of the images.  By offsetting the second you create the illution of a long, continuous image.

    mgImage.position.x = view.bounds.size.width * 0.5
    mgImage2.position.x = view.bounds.size.width * 1.5

    //Specifies the y postion of the images, obviously these are the same as they are not to be offset at any time.

    mgImage.position.y = (self.frame.size.height - self.frame.size.height) + self.mgImage.size.height / 2
    mgImage2.position.y = (self.frame.size.height - self.frame.size.height) + self.mgImage2.size.height / 2

    //Prevents aliasing with other nearby spites.

    mgImage.texture?.filteringMode = SKTextureFilteringMode.Nearest
    mgImage2.texture?.filteringMode = SKTextureFilteringMode.Nearest

    //Adds instances of the sprites to the scene.

    self.addChild(mgImage)
    self.addChild(mgImage2)

这对于这里列出的所有层也是一样的,速度等似乎并没有让它变得更好或更糟。

任何帮助将不胜感激。

谢谢,

史蒂文

【问题讨论】:

    标签: ios sprite-kit skspritenode


    【解决方案1】:

    我是否正确,您想要一个以不同速度无限移动的背景?

    我认为当有两个图像在同一个地方时会发生闪烁,但编程不知道需要打开哪个,因此彼此交替。

    PS。这是一个答案,因为我无法发表评论。

    【讨论】:

    • 但是你想让它左右移动还是只是一个不断移动的背景?
    • 在一个平滑的循环中不停地移动,在循环一定次数后改变速度,但非常平滑,没有闪烁。
    • 如果您的后台运行流畅但只是时不时闪烁。尝试为每个背景添加一个其他 z 值,以便程序知道顶部的内容。再休息我帮不了你
    • self.frame.size.height - self.frame.size.height 你知道这没有任何作用吗?
    • 其实没有,不应该注明高度吗?
    猜你喜欢
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    相关资源
    最近更新 更多