Creating a repeating background/infinite parallax effect in cocos2d

I’m working on an iPhone game and thought I’d share some code for a infinite parallax class I made. I’m aware of CCParallaxNode, I’ve named mine CCParallaxScrollNode and it greatly simplifies the task of creating a repeating background with parallax effects. It does all the dirty work for you, essentially. Here’s the info and link, and a sample project. Remember to change the Base SDK in the project settings to the latest you have.

CCParallaxScrollNode on GitHub

Download Sample Project

  • Alex

    Hi,
    Thank you for the tutorial and provided info; I’m also trying to build a parallax effect similar as you did but with CCLayers… and I manage to scroll them but I don’t know to set the limit of the scroll to be the width of my back layer.

    Cheers! :D

    • Alex

      P.S. – Wanted to ask you if you know how to fix that

  • Aram

    Hmm, I’ve read that it’s best to use a CCSpriteBatchNode and a bunch of CCSprites added to it for efficiency, as opposed to CCLayers which are more costly to render. Either way, your CCLayer is a CCSprite, so it shouldn’t be too hard to just use CCSprites, unless you’re using some methods specific to CCLayer. The general idea with my method is that you ensure the sprite is at least as wide as your screen width, then you make 2 copies of them, place on on the screen and the other next to it on the right. When the first moves to the left and off the screen, the sprite beside it moves onto the screen. Once the left sprite has moved off completely, it places itself behind the right one and becomes the new right. This keeps repeating and makes an infinite background, and with different speeds and overlaying you can create the parallax effect. That’s pretty much what my class above does.

  • teddy

    how do I change the sprite that is following. for example have one sprite follow itself like you have above then after an amount of time change that sprite to another sprite??
    I’ve seen it done in a game called jetpack joyride, where the background changes after an amount of time.

    • Anonymous

      The methods above don’t contain logic for swapping sprites, that would be something you could write. So at some point where one of the sprites travels a certain distance you would remove both and add their replacements.

  • Guest

    Hi!
    Download link is broken.Could you fix it?

  • http://aramk.com/ Aram Kocharyan

    Works fine for me…

  • http://www.facebook.com/profile.php?id=1645190136 Ahmad Ashraf Azman

    i got small little problem. im usually move parallax node.  when i try to use this approach the parallax didn’t move. When i see the code seem like you moving the child inside parallax rather than parallax node itself. Am i correct? and how to modified this code so i can move the parallax.

  • http://twitter.com/lilwins Little Wins, LLC

    I ported this to Cocos2d-x (C++).  I left out the batch features, still getting the hang of C++ and how it handles static classes.  But otherwise it works pretty well.

    Here’s a link to the github repo: https://github.com/jasonmarziani/CCParallaxScrollNode

    Thanks for the work on this!

    • http://aramk.com/ Aram Kocharyan

      Nicely done!

  • Pingback: List of Open Source Cocos2d Projects, Extensions and Code Snippets | iUridium

  • Jubun

    Thanks For Share Tutorial It Gave me Lot Help

  • Allan

    Hi,

    Thanks for share this! I have some questions: With this class you created, I can choose which part of the parallax will be repeated and which will not? For example, the sky is a static element and other element are moving.

    There is the possibility of parallax moving in the y-axis rather than the x-axis?