0

Note: for this I am using a program called spritebuilder, which allows me to create a game with less code than would normally be needed. If you know a solution that's just all code, then by all means feel free to share it :)

Also, for this question, I followed a tutorial at this link: Build Your Own Flappy Bird Clone. Just scroll down to the part that says: "Loop the Ground"

So here's my problem. Im currently working on a game, and I created a camera which scrolls vertically long with the character sprite i created, however i need a certain image to loop. When the image leaves the bottom part of the screen I would like it to loop around to the top of the screen, infinitely. For this i created two identical images (in this case its the bark of a tree). One will be on screen, while the other will be offscreen, so as the first image leaves the screen, the second will replace it (seamlessly). I created two objects for the images, and assigned them the name _ground1, and _ground2, and I also created an NSArray in which to store them in. (Please refer to the link above if it is somewhat confusing) Here is the code that I have:

CCNode *_ground1;
CCNode *_ground2;
NSArray *_grounds;

  for (CCNode *ground in _grounds) {
    // get the world position of the ground
    CGPoint groundWorldPosition = [_physicsNode convertToWorldSpace:ground.position];
    // get the screen position of the ground
    CGPoint groundScreenPosition = [self convertToNodeSpace:groundWorldPosition];
    // if the left corner is one complete width off the screen, move it to the right
    if (groundScreenPosition.y <(-1 * ground.contentSize.height)) {
        ground.position = ccp(ground.position.x , ground.position.y + 2 * ground.contentSize.height);
    }

For some reason when I try this, it doesnt seem to work. what happens is that, the camera will travel vertically as it is meant to do, but the images do not loop. Once the two images leave the bottom of the screen, no new images replace them.

James Webster
  • 31,873
  • 11
  • 70
  • 114
user3413380
  • 75
  • 11

2 Answers2

0

i also done this project as above tutorials. it work fine but you have some mistake to set variable in spritebuilder. in your above code replce code as and try it. you only put less than may be it issue.

 if (groundScreenPosition.y <=(-1 * ground.contentSize.height)) {
        ground.position = ccp(ground.position.x , ground.position.y + 2 * ground.contentSize.height);
    }
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
0

You are using CCNode objects as _ground1and _ground2.

CCNode objects usually do not have a contentSize, they will return 0 unless you explicitly set them inSpriteBuilder`.

Make sure that you are using CCSprite objects in SpriteBuilder and in your code.

Also, as a friendly hint you should also consider refactoring (renaming) your sprites with more meaningful names for your use case like _treeBark1 and treeBark2 for example.

Tibor Udvari
  • 2,932
  • 3
  • 23
  • 39