Objective C - CCSprites在我的代码的不同部分进行缩放和显示

时间:2012-01-11 15:44:35

标签: objective-c cocos2d-iphone ios-simulator ccsprite

我正在构建一个具有主菜单层和背景图层(用于游戏)的应用程序。我在两个地方加载完全相同的CCParallax节点代码,使用相同的图像创建无限滚动背景。

在游戏背景图层上,背景会适当显示和滚动。但是,在主菜单图层上,背景缩放很奇怪(它们不会占用整个屏幕),也不会进行适当的滚动(可能是因为缩放以某种方式关闭,因此偏移不起作用)。

以下是主菜单背景的截图: Main Menu Background

这里是背景图层(绿线是游戏玩法的一部分): Gameplay background

几乎看起来菜单层正在添加非视网膜显示版本的文件,但是如果我将模拟器加载到iPhone(非视网膜)模式,那么经典的背景仍然是关闭的:

Non-retina menu background

加载两者的代码完全相同。

这是主菜单加载程序代码:

-(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];
        }
    } 
}

以下是背景图层的代码:

- (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 个答案:

没有答案