2

I am trying to create a background to flow with the game. However the image isn't Continuous. There is a space between each of the image loads. I want the image to continue to loop.

Here is the method to create the sprite

CCSprite *sprite = [CCSprite spriteWithFile:@"Image.png" rect:CGRectMake(0, 0, 960, 640)];

ccTexParams tp = {GL_NEAREST, GL_NEAREST, GL_REPEAT, GL_REPEAT};

[sprite.texture setTexParameters:&tp];
sprite.anchorPoint = ccp(1.0f/8.0f, 0);
sprite.position = ccp(screenW/8, 0);

Method to update the position of the sprite.

- (void) setOffsetX:(float)offsetX {
if (_offsetX != offsetX) {
_offsetX = offsetX;
CGSize size = _sprite.textureRect.size;
_sprite.textureRect = CGRectMake(_offsetX, 0, size.width, size.height);
}
}

Any help please

James Webster
  • 31,873
  • 11
  • 70
  • 114
Bryan1234321
  • 63
  • 1
  • 6

2 Answers2

3

Your image width needs to be a power of two. i.e. the width has to be 64, 128, 256, 512, etc if you want it to repeat

The gap you are seeing is where OpenGL has padded empty space to your texture to make it power of two.

James Webster
  • 31,873
  • 11
  • 70
  • 114
  • OpenGL has not required images to be powers of two in half a decade, minimum. Now, perhaps the CCSprite API or whatever he's using that actually uses OpenGL does, but OpenGL _itself_ does not. – Nicol Bolas Sep 11 '11 at 09:14
  • For repeating images it still does. – James Webster Sep 11 '11 at 09:15
  • There is no place in the OpenGL specification that it states that using GL_REPEAT requires power of two texture sizes. Not in anything GL 2.0 or above. – Nicol Bolas Sep 11 '11 at 09:29
  • The OP makes no reference to what version of OpenGL they are using. They do however, have an issue that is caused by not using power of two textures, that's why I posted the relevant solution. – James Webster Sep 11 '11 at 10:28
  • I notice you have low reputation so I assume you're a new user. You should accept and upvote an answer if it's correct. Thanks! – James Webster Sep 11 '11 at 19:58
1

After trying it a few times, the best way is to ensure that the sprite dimensions are a power of 2. This will ensure that you can scale the layer and all remains fine. If you don't plan on scaling the layer, then you can use any size sprites and use this:

[[CCDirector sharedDirector] setProjection:CCDirectorProjection2D];

http://ak.net84.net/iphone/gap-between-sprites-when-moving-in-cocos2d/

Aram Kocharyan
  • 20,165
  • 11
  • 81
  • 96
  • 1
    This helps to solve same spacing between each image issue with 512x512 image size. I have added this line applicationDidFinishLaunching: method ----- [[CCDirector sharedDirector] setProjection:CCDirectorProjection2D]; ----- thanks Aram. – mohsinj Jul 16 '13 at 18:16