我們之前學習了打飛機的游戲,但是我們可能覺得枯燥的畫面很不舒服,現在我們就來學習進度條的加載和背景的滾動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 。使用球坐標系圍繞屏幕中心旋轉攝像機的視角
}
~~~