0

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

Community
  • 1
  • 1

1 Answers1

0

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.

mattk
  • 333
  • 3
  • 11
  • Hi Matt and thanks for the reply. I tried your changes but now half way through the scroll the obstacles just shoot up and disappear with no respawn…. really confusing. – user2800989 May 10 '14 at 23:44
  • I also tried if (obstacleScreenPosition.y > _physicsNode.contentSize.height) { same result. hmmmm….maybe my physics node dimensions are wrong? I followed the tut…. – user2800989 May 10 '14 at 23:47
  • That is strange, I thought that the downwards scroll worked because the screen position of zero would be at the bottom and when the obstacle screen position is less than an obstacles negative width it should be removed and spawn another. So, I thought well if the obstacle position is greater than the height of the screen it should be removed and spawn a new one like in the downward case. If you can find out the height of the screen that code piece should work. I thought that in the tutorial the physics node is percentage based and if so you could check it by using the contentSize.height. – mattk May 11 '14 at 00:04
  • It may actually need to be checked by a object that uses units for measurement and coordinates. However you are probably always going to want the physics node to be percentage based for different device deployment. – mattk May 11 '14 at 00:05
  • it's 100% by 100%, bottom left, anchor 0.0 – user2800989 May 11 '14 at 00:12
  • i tried various combinations of anchor points and different corners. no luck. – user2800989 May 11 '14 at 00:16
  • As the content size of the physics node is in percent, I don't know how that correlates to the unit size of the obstacle. If you can figure that out then maybe you can edit the code to work. Sorry I cant be of further help. – mattk May 11 '14 at 01:10