加载进度条和滚动背景

我们之前学习了打飞机的游戏,但是我们可能觉得枯燥的画面很不舒服,现在我们就来学习进度条的加载和背景的滚动show

1.首先来看看loadingbar的动态加载

1)首先在哎loading.h文件中加入如下的属性

    CCProgressTimer *progress;
    float progressInterval;

2)。m文件的实现如下

-(id) init
{
    
    if ( ( self = [ super init] ) )
    {
        
        winSize = [[CCDirector sharedDirector] winSize];
        winCenter = ccp(winSize.width / 2, winSize.height / 2);
        
        progress = [CCProgressTimer progressWithSprite:[CCSprite spriteWithFile:@"progressbar.png"]];
        
        [progress setPercentage:0];  //设置bar的初始化为0
        progress.scale = 0.5f;  //缩小一半
        
        progress.midpoint = ccp(0, 0.5); //进度条动画的起始位置,默认是图片的中点。如果想从头开始加载,必须改成(0,y);
        progress.barChangeRate = ccp(1, 0); //沿x轴方向加载,y轴不变
        
        progress.type = kCCProgressTimerTypeBar;
        
        [progress setPosition:winCenter];
        [self addChild:progress];
        

        
        CCLabelTTF *loadingText = [CCLabelTTF labelWithString:@"Loading..." fontName:@"Arial" fontSize:20];
        loadingText.position = ccpAdd(winCenter, ccp(0,50));
        [self addChild:loadingText];
                        
    }
    
    return self;
}

3)更新progressUpdate方法

-(void) progressUpdate
{
    if (--assetCount)
    {
        [progress setPercentage:(100.0f - (progressInterval *assetCount))];
        //留着后面显示进度条用
    }
    else {
        
        CCProgressFromTo *ac = [CCProgressFromTo actionWithDuration:0.5 from:progress.percentage to:100];
        CCCallBlock *callBack = [CCCallBlock actionWithBlock:^(){
            [self loadingComplete];
            CCLOG(@"All done loading assets.");
        }];
        
        id action = [CCSequence actions:ac,callBack, nil];
        [progress runAction:action];
        
    }

}

2.使用CCFollow 和CCParallaxNode 动作添加滚动背景

1)Helloworld.h中加入如下的属性

    //添加滚动背景
    CCParallaxNode *_backgroundNode;
    int _totalSeconds;  //进行的游戏时长

2).m中更新 updatebackground方法,我们相应的也导入了api:

#import "CCParallaxNode-Extras.h"

#pragma mark 背景的更新方法
- (void)updateBackground:(ccTime)dt
{
    CCSprite *sprite;
    int index = 0;
	CCARRAY_FOREACH([_backgroundNode children],sprite)
	{
        CGPoint pt = [_backgroundNode convertToWorldSpace:sprite.position];
        //        CCLOG(@"pt.x = %f, pt.y = %f",pt.x,  pt.y);
        if ( pt.y <= -sprite.contentSize.height) {
            CCLOG(@"===============");
            [_backgroundNode incrementOffset:ccp(0,(sprite.contentSize.height - offset) * 2.0f) forChild:sprite];
        }
        
        index++;
	}
    

}

3.init方法的修改

        //16.添加连续滚动背景
        _backgroundNode = [CCParallaxNode node];
        [self addChild:_backgroundNode z:-1];
        
        CGPoint ratio = ccp(1.0,0.5);
        CCSprite *bgSprite1 = [CCSprite spriteWithSpriteFrameName:@"background_1.jpg"];
        [[bgSprite1 texture] setAliasTexParameters];
        bgSprite1.anchorPoint = ccp(0,0);
        [_backgroundNode addChild:bgSprite1 z:1 parallaxRatio:ratio positionOffset:ccp(0,0)];
        
        CCSprite *bgSprite2 = [CCSprite spriteWithSpriteFrameName:@"background_2.jpg"];
        [[bgSprite2 texture] setAliasTexParameters];
        bgSprite2.anchorPoint = ccp(0,0);
        [_backgroundNode addChild:bgSprite2 z:1 parallaxRatio:ratio positionOffset:ccp(0,winSize.height - offset)];
        
        //一定要注意,之前的背景设置要取消掉
        
	}
	return self;
}

4.onEnter方法的修改

- (void)onEnter
{
    [super onEnter]; //一定要注意添加此方法,否则将停留在开始界面
    CGSize winSize = [[CCDirector sharedDirector] winSize];
    [CCMenuItemFont setFontSize:20];
    [CCMenuItemFont setFontName:@"Arial"];
    
    CCMenuItemFont *startItem = [CCMenuItemFont itemWithString:@"开始游戏" block:^(id sender)
                                 {
                                     _isGameStarted = YES;
                                     CCMenuItem *item = (CCMenuItemFont*)sender;
                                     item.visible = NO;
                                     
                                     //6.spawn enemy after 1.0 sec
                                     [self performSelector:@selector(spawnEnemy)
                                                withObject:nil
                                                afterDelay:1.0f];
                                     
                                     //7.enable accelerometer
                                     self.isAccelerometerEnabled = YES;
                                     //9.enable touch
                                     self.isTouchEnabled = YES;
                                     
                                     
                                     //8.添加开始连续滚动背景的代码
                                     const int MAX_LEVEL_WIDTH = 320;
                                     const int MAX_LEVEL_HEIGHT = 480 * 100;
                                     CCSprite *hiddenPlayerSprite = [CCSprite spriteWithSpriteFrameName:@"hero_1.png"];
                                     hiddenPlayerSprite.position = ccp(winSize.width / 2, winSize.height / 2);
                                     [self addChild:hiddenPlayerSprite z:-4 tag:1024];
                                     
                                     _totalSeconds = 60;
                                     id move = [CCMoveBy actionWithDuration:_totalSeconds position:ccp(0,MAX_LEVEL_HEIGHT)];
                                     [hiddenPlayerSprite runAction:move];
                                     //让背景开始滚动
                                     [_backgroundNode runAction:[CCFollow actionWithTarget:hiddenPlayerSprite
                                                                             worldBoundary:CGRectMake(0, 0, MAX_LEVEL_WIDTH, MAX_LEVEL_HEIGHT)]];
                                 }];
    
    
    
    
    startItem.position = ccp(winSize.width / 2, -winSize.height / 2);
    _startGameMenu = [CCMenu menuWithItems:startItem, nil];
    _startGameMenu.position = CGPointZero;
    [self addChild:_startGameMenu];
    
    
    //7 基本动作   从原来的位置移动到新的位置
    id moveBy = [CCMoveBy actionWithDuration:1.0 position:ccp(0, winSize.height)];  //位置的移动
    
    [_startGameMenu runAction:moveBy]; //开始移动
    //8 和位置有关的基本动作
    
    
    
    
    
    //1 CCActionManager
    [[[CCDirector sharedDirector] actionManager ] pauseTarget:_startGameMenu];//暂停
    
    [self schedule:@selector(resumeStartMenuAction:) interval:1.0]; //等待十秒之后才能开始移动
    
    //2CCAction 抽象类,几乎所有的类都继承该类
    
    //3.CCFiniteTimeAction 该类为有限时间动作,包含CCActionInstant 瞬时动作 和CCActionInterval 区间动作,他们包含了很多不同的动作
    //4 CCRepaeatForever 无限重复的动作
    
    //5跟随节点的动作CCFollow .可以替代Camera
    //6 CCSpeed  更还节点动作的速度。
    // 7CCOrbitCamera 继承与CCActionCamera 。使用球坐标系围绕屏幕中心旋转摄像机的视角
    

}

文章导航