<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                昨天復習了關于COCOS2d的一些基礎知識,今天就來學習了關于游戲加載方面的問題和代碼重構。 寫一個簡單的游戲數據加載的demo。 1.如果想在進入游戲前,加載游戲,肯定有進度條什么的吧,當然,也就是我們要再游戲開始前有一個關于游戲加載的場景類。 由于我自己寫的demo丟失,只好拿著實例寫了。。。sorry,不過我會認真的寫注釋的 建立如下:? ~~~ #import <Foundation/Foundation.h> #import "cocos2d.h" @interface LoadingScreen : CCLayer { CGSize winSize; CGPoint winCenter; int assetCount; } +(CCScene *) scene; //場景類的轉化和生成 /**Called if there is any music to load, it loads the files one by one via the NSArray */ -(void) loadMusic:(NSArray *) musicFiles; //音樂文件的加載 /**Called if there are any sfx to load, it loads the files one by one via the NSArray */ -(void) loadSounds:(NSArray *) soundClips; /**Called if there are any Sprite Sheets to load, it loads the files one by one via the NSArray */ -(void) loadSpriteSheets:(NSArray *) spriteSheets; //精靈表單的加載,我更喜歡叫一組精靈 /**Called if there are any images to load, it loads the files one by one via the NSArray. Images can be a cache of backgrounds or anything else. You can add to this method to have it do whatever you want with the list. */ -(void) loadImages:(NSArray *) images; //圖片的加載 /**Called if there are any assets to load, it loads the files one by one via the NSArray. Assets basically are anything not of the above. This pretty much should be modified to your needs. (Such as models, etc) */ -(void) loadAssets:(NSArray *) assets; //季度條進度的加載 /**updates the progress bar with the next step. When progress bar reaches 100% It calls loadingComplete which can change scenes, or do anything else you wish. */ -(void) progressUpdate; //主線程 的分線程更新完成之后的調用 /**Called by progressUpdate when all assets are loaded from the manifest. */ -(void) loadingComplete; // 全部完成調用 @end ~~~ 2.m文件的準備工作 ~~~ #import "LoadingScreen.h" #import "SimpleAudioEngine.h" //The next scene you wish to transition to. #import "HelloWorldLayer.h" @implementation LoadingScreen +(CCScene *) scene { // 'scene' is an autorelease object. CCScene *scene = [CCScene node]; NSString *className = NSStringFromClass([self class]); // 'layer' is an autorelease object. id layer = [NSClassFromString(className) node]; // add layer as a child to scene [scene addChild: layer]; // return the scene return scene; } -(id) init { if ( ( self = [ super init] ) ) { winSize = [[CCDirector sharedDirector] winSize];//尋找導演視圖的寬高 winCenter = ccp(winSize.width / 2, winSize.height / 2); CCLabelTTF *loadingText = [CCLabelTTF labelWithString:@"Loading..." fontName:@"Arial" fontSize:20]; loadingText.position = ccpAdd(winCenter, ccp(0,50)); [self addChild:loadingText];//放置加載BAR } return self; } ~~~ 3.進入場景之后的加載方法,也別注意不用的資源加載的方式的不同,主線程加載 ~~~ -(void) onEnterTransitionDidFinish { [super onEnterTransitionDidFinish]; NSString *path = [[CCFileUtils sharedFileUtils] fullPathFromRelativePath:@"preloadAssetManifest.plist"]; NSDictionary *manifest = [NSDictionary dictionaryWithContentsOfFile:path]; NSArray *spriteSheets = [manifest objectForKey:@"SpriteSheets"]; NSArray *images = [manifest objectForKey:@"Images"]; NSArray *soundFX = [manifest objectForKey:@"SoundFX"]; NSArray *music = [manifest objectForKey:@"Music"]; NSArray *assets = [manifest objectForKey:@"Assets"]; assetCount = ([spriteSheets count] + [images count] + [soundFX count] + [music count] + [assets count]); if (soundFX) [self performSelectorOnMainThread:@selector(loadSounds:) withObject:soundFX waitUntilDone:YES]; if (spriteSheets) [self performSelectorOnMainThread:@selector(loadSpriteSheets:) withObject:spriteSheets waitUntilDone:YES]; if (images) [self performSelectorOnMainThread:@selector(loadImages:) withObject:images waitUntilDone:YES]; if (music) [self performSelectorOnMainThread:@selector(loadMusic:) withObject:music waitUntilDone:YES]; if (assets) [self performSelectorOnMainThread:@selector(loadAssets:) withObject:assets waitUntilDone:YES]; } ~~~ 4.單個加載方法 SEL方法 ~~~ -(void) loadMusic:(NSArray *) musicFiles { CCLOG(@"Start loading music"); for (NSString *music in musicFiles) { [[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:music]; [self progressUpdate]; } } -(void) loadSounds:(NSArray *) soundClips { CCLOG(@"Start loading sounds"); for (NSString *soundClip in soundClips) { [[SimpleAudioEngine sharedEngine] preloadEffect:soundClip]; [self progressUpdate]; } } -(void) loadSpriteSheets:(NSArray *) spriteSheets { for (NSString *spriteSheet in spriteSheets) { [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:spriteSheet]; [self progressUpdate]; } } -(void) loadImages:(NSArray *) images { CCLOG(@"LoadingScreen - loadImages : You need to tell me what to do."); for (NSString *image in images) { //Do something with the images [[CCTextureCache sharedTextureCache] addImage:image]; [self progressUpdate]; } } -(void) loadAssets:(NSArray *) assets { //Overwrite me CCLOG(@"LoadingScreen - loadAssets : You need to tell me what to do."); for (NSString *asset in assets) { //Do something with the assets [self progressUpdate]; } [self progressUpdate]; } ~~~ 4.工具類方法 ,加載,加載一部分,和加載完成的方法 ~~~ -(void) progressUpdate { if (--assetCount) { //留著后面顯示進度條用 } else { [self loadingComplete]; CCLOG(@"All done loading assets."); } } -(void) loadingComplete { CCDelayTime *delay = [CCDelayTime actionWithDuration:2.0f]; CCCallBlock *swapScene = [CCCallBlock actionWithBlock:^(void) { [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:1.0f scene:[HelloWorldLayer scene]]]; }]; CCSequence *seq = [CCSequence actions:delay, swapScene, nil]; [self runAction:seq]; } @end ~~~ 至此,已經結束了加載場景的vc。 我們將要把這個scene放到我們的game中去 5.一步一步走! 修改 Appdelegate.m方法中得啟動方法 只是在啟動方法得末尾加上加載的方法而已 ~~~ // and add the scene to the stack. The director will run it when it automatically when the view is displayed. [director_ pushScene: [LoadingScreen scene]]; ~~~ 6.修改HellpWorldLayer.m文件的init方法 1)增加游戲菜單 ~~~ //15.add game start menu & relative game logic _isGameStarted = NO; [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; }]; startItem.position = ccp(winSize.width / 2, winSize.height / 2); _startGameMenu = [CCMenu menuWithItems:startItem, nil]; _startGameMenu.position = CGPointZero; [self addChild:_startGameMenu]; } ~~~ 由于觸摸 開始游戲按鈕之后才可以進行游戲,所以MEnu的position一定要設置成CGPointZero 現在可以試試效果了~ 二,代碼重構 1,添加精靈表單 ~~~ enum { kTagPalyer = 1, kTagBatchNode = 2, }; ~~~ ~~~ //添加精靈表單 CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:@"gameArts.png"]; batchNode.position = CGPointZero; [self addChild:batchNode z:0 tag:kTagBatchNode]; ~~~ 2.修改Sprite的初始化方式 ~~~ //2.add background CCSprite *bgSprite = [CCSprite spriteWithSpriteFrameName:@"background_1.jpg"]; bgSprite.position = ccp(winSize.width / 2,winSize.height/2); [batchNode addChild:bgSprite z:-100]; //3.add player's plane,初始化方式 從 spriteWithFIle改為 spriteWithSpriteFrameName CCSprite *playerSprite = [CCSprite spriteWithSpriteFrameName:@"hero_1.png"]; playerSprite.position = CGPointMake(winSize.width / 2, playerSprite.contentSize.height/2 + 20); [batchNode addChild:playerSprite z:4 tag:kTagPalyer]; //4.init enemy sprites array _enemySprites = [[CCArray alloc] init]; //5.initialize 10 enemy sprites & add them to _enemySprites array for future useage const int NUM_OF_ENEMIES = 10; for (int i=0; i < NUM_OF_ENEMIES; ++i) { CCSprite *enemySprite = [CCSprite spriteWithSpriteFrameName:@"enemy1.png"]; enemySprite.position = ccp(0,winSize.height + enemySprite.contentSize.height + 10); enemySprite.visible = NO; [batchNode addChild:enemySprite z:4]; [_enemySprites addObject:enemySprite]; } //8.game main loop [self scheduleUpdate]; _isTouchToShoot = NO; //10.init bullets _bulletSprite = [CCSprite spriteWithSpriteFrameName:@"bullet1.png"]; _bulletSprite.visible = NO; [batchNode addChild:_bulletSprite z:4]; ~~~ 3.為了減少修改的次數,定義一個 - (CCSprite *)getPlayerSPrite方法 ~~~ -(CCSprite*)getPlayerSprite{ CCSpriteBatchNode *batchNode = (CCSpriteBatchNode*)[self getChildByTag:kTagBatchNode]; return (CCSprite*)[batchNode getChildByTag:kTagPalyer]; } ~~~ 4.修改發射子彈的碰撞檢測方法 ~~~ -(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ //修改成,必須點選中playerSprite才能夠發射子彈 //方法1:因為boundingBox是相對于世界坐標系而言的,所以要用self convertTouchToNodeSpace轉換成世界坐標系中的坐標 UITouch *touch = [touches anyObject]; CCSprite *playerSprite = [self getPlayerSprite]; CGPoint pt; // pt = [touch locationInView:[touch view]]; // pt = [[CCDirector sharedDirector] convertToGL:pt]; // pt = [self convertToNodeSpace:pt]; //上面三句調用,可以簡化為下面一句調用 //CGPoint pt = [self convertTouchToNodeSpace:touch]; // if (CGRectContainsPoint(playerSprite.boundingBox, pt)) { // _isTouchToShoot = YES; // } //=================================================================== //方法2: // pt = [touch locationInView:[touch view]]; // pt = [[CCDirector sharedDirector] convertToGL:pt]; // pt = [playerSprite convertToNodeSpace:pt]; //簡化為下面的一句代碼調用 CCSpriteBatchNode *batchNode = (CCSpriteBatchNode*)[self getChildByTag:kTagBatchNode]; pt = [batchNode convertTouchToNodeSpace:touch]; CCLOG(@"pt.x = %f, pt.y = %f",pt.x, pt.y); if (CGRectContainsPoint(playerSprite.boundingBox, pt)) { _isTouchToShoot = YES; CCLOG(@"touched!"); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看