Objective C - CCSprites scaling and displaying differently in different parts of my code

457 views Asked by At

I'm building an app that has a main menu layer and a background layer (for gameplay). I am loading the exact same CCParallax node code in both places, using the same images to create an infinite scrolling background.

On the gameplay background layer, the background displays and scrolls appropriately. However, on the main menu layer, the backgrounds are scaled weird (they don't take up the entire screen), and also don't scroll appropriately (probably because the scale is off somehow, so the offset isn't working).

Here is a screenshot of what the background looks like on the main menu: Main Menu Background

And here it is on the background layer (green lines are part of the gameplay): Gameplay background

It almost looks like the menu layer is adding the non-retina display version of the files, but if I load the simulator into the iPhone (non-retina) mode, the meny background is still way off:

Non-retina menu background

The code that loads the two is pretty much identical.

Here is the main menu loader code:

-(id)init {
    self = [super init];
    if (self != nil) {
        CGSize winSize = [CCDirector sharedDirector].winSize;

        _backgroundNode = [CCParallaxNode node];
        [self addChild:_backgroundNode z:-1];

        _backgroundGrid1 = [CCSprite spriteWithFile:@"grid.png"];
        _backgroundCircuits1 = [CCSprite spriteWithFile:@"bg-circuits.png"];

        _backgroundGrid1.anchorPoint = CGPointMake(0,0);
        _backgroundCircuits1.anchorPoint = CGPointMake(0,0);
        CGPoint gridSpeed = ccp(0.05, 0.05);
        CGPoint circuitSpeed = ccp(0.1, 0.1);

        [_backgroundNode addChild:_backgroundGrid1 z:1 parallaxRatio:gridSpeed positionOffset:ccp(0,-winSize.height)];
        [_backgroundNode addChild:_backgroundCircuits1 z:0 parallaxRatio:circuitSpeed positionOffset:ccp(0,-winSize.height)];

        [self scheduleUpdate];
    }
    return self;
}

- (void)update:(ccTime)dt {

    CGPoint backgroundScrollVel = ccp(0, 1000);
    _backgroundNode.position = ccpAdd(_backgroundNode.position, ccpMult(backgroundScrollVel, dt));

    NSArray *backgroundGrids = [NSArray arrayWithObjects:_backgroundGrid1, nil];
    for (CCSprite *b in backgroundGrids) {
        if ([_backgroundNode convertToWorldSpace:b.position].y > 0) {
            [_backgroundNode incrementOffset:ccp(0,-(b.contentSize.height/3)) forChild:b];
        }
    }

    NSArray *backgroundCircuits = [NSArray arrayWithObjects:_backgroundCircuits1, nil];
    for (CCSprite *bc in backgroundCircuits) {
        if ([_backgroundNode convertToWorldSpace:bc.position].y > 0) {
            [_backgroundNode incrementOffset:ccp(0,-(bc.contentSize.height/3)) forChild:bc];
        }
    } 
}

And here is the code for the background layer:

- (id)init
{
    self = [super init];
    if (self != nil) {
        CGSize winSize = [CCDirector sharedDirector].winSize;

        _backgroundNode = [CCParallaxNode node];
        [self addChild:_backgroundNode z:-1];

        _backgroundGrid1 = [CCSprite spriteWithFile:@"grid.png"];
        _backgroundCircuits1 = [CCSprite spriteWithFile:@"bg-circuits.png"];
        _backgroundGrid1.anchorPoint = CGPointMake(0,0);
        _backgroundCircuits1.anchorPoint = CGPointMake(0,0);
        CGPoint gridSpeed = ccp(0.05, 0.05);
        CGPoint circuitSpeed = ccp(0.1, 0.1);

        [_backgroundNode addChild:_backgroundGrid1 z:1 parallaxRatio:gridSpeed positionOffset:ccp(0,-winSize.height)];
        [_backgroundNode addChild:_backgroundCircuits1 z:0 parallaxRatio:circuitSpeed positionOffset:ccp(0,-winSize.height)];

    [self scheduleUpdate];

    return self;
}

- (void)update:(ccTime)dt {

    CGPoint backgroundScrollVel = ccp(0, 1000);
    _backgroundNode.position = ccpAdd(_backgroundNode.position, ccpMult(backgroundScrollVel, dt));

    NSArray *backgroundGrids = [NSArray arrayWithObjects:_backgroundGrid1, nil];
    for (CCSprite *b in backgroundGrids) {
        if ([_backgroundNode convertToWorldSpace:b.position].y > 0) {
            [_backgroundNode incrementOffset:ccp(0,-(b.contentSize.height/3)) forChild:b];
        }
    }

    NSArray *backgroundCircuits = [NSArray arrayWithObjects:_backgroundCircuits1, nil];
    for (CCSprite *bc in backgroundCircuits) {
        if ([_backgroundNode convertToWorldSpace:bc.position].y > 0) {
            [_backgroundNode incrementOffset:ccp(0,-(bc.contentSize.height/3)) forChild:bc];
        }
    } 
}
0

There are 0 answers