I’m trying to setup a simple scrolling parallax starfield. It should move in both directions based on how the user moves. It seems like it should be possible to do this via GL_REPEAT, but when I set it up it just shows the background sprite once.
auto bg1 = Sprite::create("stars1.png");
Texture2D::TexParams p;
p.minFilter = GL_LINEAR;
p.magFilter = GL_LINEAR;
p.wrapS = GL_REPEAT;
p.wrapT = GL_REPEAT;
bg1->getTexture()->setTexParameters(p);
addChild(bg1, -1);
// simulate the user moving
bg1->runAction(MoveBy::create(10, Vec2(-4000, 0)));
Thanks for replying. So is it theoretically possible to scale the size of a sprite after it’s been created, to allow this sort of repeating pattern? If so, is there a maximum size of a sprite in Cocos, either hard or soft (performance related)?
I’m going for “infinite” scrolling, but in reality it’s a finite game area that if I could create a large enough sprite I could constrain the game area based on that.
While I was waiting for a reply, I’ve started working on a tile-based approach where I’m dynamically creating and deleting tiles so it seems infinite. If I could avoid this complexity it would be better
Off the top of my head, I think you set the size by setting the size of the texture rect. Something like sprite->getTexture()->setTextureRect(0, 0, w, h); I am not sure about about a limit to the size or performance impact.
The way I have done “infinite scrolling” in the past is to have one repeating texture that has dimensions that are multiples of the texture’s dimensions and 1 texture width/height more than the screen width height. E.g. If your texture is 256x256 and your screen is 1024x768, you create a texture that is 1280x1024. (1280 is the next highest multiple of 256 after 1024, and 1024 is the next highest multiple of 256 after 768.) Then whenever you move in a certain direction, check if you have moved the width/height of the texture, and if you have then move the sprite to avoid the edge appearing on the screen.
For example, if you have a layer called scroller that moves to give the appearance of your character moving and a Sprite called background that is your background texture, then something like this:
Sorry if I didn’t explain that clearly enough. It’s basically that you move the background until its edge reaches the side of the screen and then you move the background back by the size of the tile so it looks exactly the same on screen, but you can keep moving it without the edge appearing. Hope that helps.
This is great, it validates the alternate approach I was building. Seemed a bit over-engineered, but I guess not. I’m basically doing 9 sprites (larger than the screen each) in a 3x3 grid. If you move one full sprite width to the left/right I create a new column of sprites on the side we’re moving towards, and clear out the sprites on the trailing side. Same deal for up/down.
The advantage of this is I can use different sprites for each “tile”, which avoids patterns with the sprites and lets me add random “backgrounds” (I’m not concerned about the fact that these will change if the user moves back over the same “space”).
I’m also hoping to repeat the pattern for two or three layers, so I can create a parallax effect.