2

I want to draw a infinitely repeating parallax using Cocos2D on Android. Now, there are some solutions given to this problem in Objective C, but I'm stuck with my implementation in Android. I have tried using

CCSprite background = CCSprite.sprite("background_island.png");
CCTexParams params = new CCTexParams(GL10.GL_LINEAR,GL10.GL_LINEAR,GL10.GL_REPEAT,GL10.GL_REPEAT);
            background.getTexture().setTexParameters(params);

But it only extends the background in 1 direction. I guess I have to use 2 sprites, such that as soon as 1st finishes, the other starts and vice versa, but I'm stuck with the implementation.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Saurabh Verma
  • 6,328
  • 12
  • 52
  • 84

2 Answers2

3

I had the same problem and figured it out.

Try this. Declare the background and offset as a member:

CCSprite _bg;
float _bgOffset;

In your scene constructor:

CGSize winSize = CCDirector.sharedDirector().displaySize();
_bg = CCSprite.sprite("yourbg.png"); // needs to be square, i.e. 256x256
_bg.setTextureRect(0, 0, winSize.width, winSize.height, false);
_bg.getTexture().setTexParameters(GL10.GL_LINEAR, GL10.GL_LINEAR, GL10.GL_REPEAT,
        GL10.GL_REPEAT);
_bg.setAnchorPoint(CGPoint.zero());
this.addChild(_bg);

And in your update(float dt) method:

if (_bgOffset > 2000000000)
    _bgOffset = 0; // don't want problems, do we?
_bgOffset += dt * PIXELS_PER_SECOND; // this can be dynamic if you want
_bg.setTextureRect(0, _bgOffset, _bg.getTextureRect().size.width,
            _bg.getTextureRect().size.height, false);

See "Repeating Backgrounds" in http://www.raywenderlich.com/3857/how-to-create-dynamic-textures-with-ccrendertexture for the Objective C code

If you need to go both ways, you could perhaps start with a non-zero _bgOffset and see if that works.

Hope this helps someone!

Oleg Vaskevich
  • 12,444
  • 6
  • 63
  • 80
  • Hi Oleg Vaskevich, Thanks for your code...I done with it. But I have facing one issue...I got some gap between two _bg.....Can you give some more guideline on it? – Kalpesh Nov 08 '12 at 07:34
  • Are you sure that your background sprites are square? – Oleg Vaskevich Nov 11 '12 at 00:48
  • It's not square but it is rectangle. But thanks I done it with by move animation and it's work perfect. And ya thanks after all I can done it from your idea. – Kalpesh Nov 12 '12 at 09:02
  • Hello using this code I am able to move my background in upward direction.How can I move it from right to left direction? – Rishabh Bhardwaj Apr 16 '13 at 05:33
  • Just set _bgOffset to the x-coordinate instead of y.... `_bg.setTextureRect(_bgOffset, 0, _bg.getTextureRect().size.width, _bg.getTextureRect().size.height, false);` – Oleg Vaskevich Apr 16 '13 at 06:37
0

Please check out below link for Parallax vertical endless background: http://kalpeshsantoki.blogspot.in/2014/07/create-vertical-endless-parallax.html

CGSize winSize = CCDirector.sharedDirector().displaySize();

//I made graphics for screen 720*1200....so I made this dynamic scale to support multiple screens
float sX = winSize.width / 720.0f;
float sY = winSize.height / 1200.0f;
background = CCVerticalParallaxNode.node(sX, sY, true);

background.addEntity(1f, "background.png", 0);
background.addEntity(3, "road_simple.png", winSize.width / 2);
background.addEntity(1.7f, "road_side.png", 0);
addChild(background);
Kalpesh
  • 1,767
  • 19
  • 29