0

I am trying to add a vertical scrolling background to my project. From what I scene on the internet. My background consists of 8 images, each [320x1000px].png files. So what I ended up doing for it was this:

//Layered Nodes
var backgroundNode: SKNode!

 override init(size: CGSize) {
    super.init(size: size)

    scaleFactor = self.size.width / 320.0

    // Background
    backgroundNode = createBackgroundNode()
    addChild(backgroundNode)

}

func createBackgroundNode() -> SKNode {

    let backgroundNode = SKNode()
    let ySpacing = 1000.0 * scaleFactor

    for index in 0...3 {

        let node = SKSpriteNode(imageNamed:String(format: "bg%d", index + 1))
        node.setScale(scaleFactor)
        node.anchorPoint = CGPoint(x: 0.5, y: 0.0)
        node.position = CGPoint(x: self.size.width / 2, y: ySpacing * CGFloat(index))
        backgroundNode.addChild(node)

    }

    return backgroundNode
}

Problem is, they use up to 50Mb of the project. I am trying to find a way to do it so it would take much less memory off my game but I can't seem to find it. Is there anything wrong with this? If not, should I best focus on other parts of the project and keep it this way?

  • Are all those images different? If yes, try creating one big background image. If no, compare the memory footprint from [tiling them](http://stackoverflow.com/a/19833309/1445366). – Aaron Brager Aug 04 '15 at 04:01
  • Yes they are different. I put the images together but a black background just appeared. May that be because of the size of the image? By putting them all together in one I end up with an 8000x320 pixel background. – Sergio Robles Aug 04 '15 at 05:04
  • Hard to help without knowing what exactly those 7 images are and what you are trying to accomplish. You say it's 8000x320. Is this meant to be a level of a side scroller ? yet in your code, you are stacking them vertically with your ySpacing value. – prototypical Aug 04 '15 at 18:10
  • Is the game currently not playable on a given device ? – prototypical Aug 04 '15 at 18:15
  • My bad, it is 320x8000. I still don't know why the background appears to be black. I tried with one of the other images and it works perfectly. Every image is 320x1000. And yes, on iPhone 4 or iPad it won't work. You can play it on an iPhone 5 but is a bit slow and on iPhone 6 it works perfectly. @prototypical – Sergio Robles Aug 04 '15 at 19:06
  • What is your target SDK version ? Are you getting an out of memory error ? – prototypical Aug 04 '15 at 20:29
  • Also an SKTexture has max size of 2048x2048. So your image is outside that limit if you combine them all in one background you intend to scroll. – prototypical Aug 04 '15 at 20:35

1 Answers1

0

You can create background on the go from vector drawing using app.

Another point. Are your images optimized? If no you can use ImageOptim for free.

Dominique Vial
  • 3,729
  • 2
  • 25
  • 45