0
votes

I am working with the flappy bird demo trying different things just to get to "know each other". Going through the demo, I've managed to change the direction of the game to vertical scroll moving upwards. Having reversed the CGFloat to negative values makes my obstacles move upward but once they are out of bounds they do not re-spawn. If I change the values for a downward scroll they re-spawn as per the update method. Can someone explain to me what I'm doing wrong with the x to y conversion? Why is the bottom recognized and the top of my screen not? Thanks in advance

#import "MainScene.h"

static const CGFloat scrollSpeed = -280.f; //upwards
static const CGFloat firstObstaclePosition = -568.f; 
static const CGFloat distanceBetweenObstacles = 80;

@implementation MainScene {
CCSprite *_hero;
CCPhysicsNode *_physicsNode;
NSMutableArray *_obstacles;
}

- (void)spawnNewObstacle {
CCNode *previousObstacle = [_obstacles lastObject];
CGFloat previousObstacleYPosition = previousObstacle.position.y;
if (!previousObstacle) {
    // this is the first obstacle
    previousObstacleYPosition = firstObstaclePosition;
}
CCNode *obstacle = [CCBReader load:@"Obstacle"];
obstacle.position = ccp(0, previousObstacleYPosition + distanceBetweenObstacles);
[_physicsNode addChild:obstacle];
[_obstacles addObject:obstacle];
}
- (void)update:(CCTime)delta {
_hero.position = ccp(_hero.position.x, _hero.position.y + delta * scrollSpeed);//move on Y axis
_physicsNode.position = ccp(_physicsNode.position.x, _physicsNode.position.y - (scrollSpeed *delta));//scroll in Y axis
//spawn more
NSMutableArray *offScreenObstacles = nil;
for (CCNode *obstacle in _obstacles) {
    CGPoint obstacleWorldPosition = [_physicsNode convertToWorldSpace:obstacle.position];
    CGPoint obstacleScreenPosition = [self convertToNodeSpace:obstacleWorldPosition];
    if (obstacleScreenPosition.y < -obstacle.contentSize.height) {
        if (!offScreenObstacles) {
            offScreenObstacles = [NSMutableArray array];
        }
        [offScreenObstacles addObject:obstacle];
    }
}
for (CCNode *obstacleToRemove in offScreenObstacles) {
    [obstacleToRemove removeFromParent];
    [_obstacles removeObject:obstacleToRemove];
    // for each removed obstacle, add a new one
    [self spawnNewObstacle];
}
}

- (void)didLoadFromCCB {
self.userInteractionEnabled = TRUE;
_obstacles = [NSMutableArray array];
[self spawnNewObstacle];
[self spawnNewObstacle];
[self spawnNewObstacle];
}

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

}
@end

I've attached the _physicsNode screenshot from SB.

enter image description here

1

1 Answers

0
votes

It looks like your obstacles will be spawning fine if they are a short, constant height, and the distance between them value is large enough. It may be better to incorporate the height of the obstacles to get a more meaningful value of the distance variable. Just a thought.

The line -

obstacle.position = ccp(0, previousObstacleYPosition + distanceBetweenObstacles);

Could be -

obstacle.position = ccp(0, previousObstacleYPosition + distanceBetweenObstacles + previousObstacle.contentSize.height);

As for the problem of the vertical scrolling working downwards and not upwards I believe it is due to this line:

if (obstacleScreenPosition.y < -obstacle.contentSize.height) {

Since this line is responsible for determining when an obstacle is off the screen it has an effect on the spawning of the next obstacle. It makes sense why this line works for downwards scrolling but needs to be changed for upwards scrolling.

Try:

 if (obstacleScreenPosition.y > (_physicsNode.contentSize.height + obstacle.contentSize.height)) {

You may or may not need the size of the obstacle depending on where it is anchored.

I hope this works, Good luck.