Cocosd2d執行個體教程(六) Cocos2d實現螢幕背景的自動滾動

來源:互聯網
上載者:User

1.介紹

實現螢幕背景的自動滾動是遊戲常遇到的功能,這樣我們就不用繪製很長的背景圖片,只要設計一張就可以,省時省力。這章將實現這個功能,並把原始碼貢獻給大家,廢話不多說,先上個圖,其實是動態,只是截動態圖有點麻煩。

2.代碼實現部分

螢幕的動態滾動主要是一個重新整理機制的問題。

第一步,還是進入HelloWorldLayer.h中定義一些節點的對象

<span style="color:#362e2b">#import <GameKit/GameKit.h>        // When you import this file, you import all the cocos2d classes  #import "cocos2d.h"        // HelloWorldLayer  @interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate>  {                CCParallaxNode *backgroundNode;//</span><span style="color:#ff6666">這個節點是實現滾動的關鍵節點</span><pre name="code" class="objc" style="color:rgb(54,46,43); line-height:26px"></pre><span style="color:#362e2b">    CCSprite *mainBg;    CCSpriteBatchNode *batchNode;        }// returns a CCScene that contains the HelloWorldLayer as the only child+(CCScene *) scene;@end</span>

第二步當然是來到HelloWorldLayer.m中。

返回欄目頁:http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

首先來寫一下重新整理的函數

- (void)updateBackground:(ccTime)dt {      CGSize size = [CCDirector sharedDirector].winSize;                CGPoint backgroundScrollVel = ccp(-size.width, 0);      backgroundNode.position =      ccpAdd(backgroundNode.position,             ccpMult(backgroundScrollVel, dt));      CGSize winSize = [CCDirector sharedDirector].winSize;                          NSArray *backgrounds = [NSArray arrayWithObjects:mainBg,nil];      for (CCSprite *background in backgrounds) {          if ([backgroundNode convertToWorldSpace:background.position].x < -background.contentSize.width) {             //if中判斷是如果螢幕超出圖片範圍               backgroundNode.position = ccp(winSize.width*4,0);//圖片就以這個速度移動          }      }  }

接著,將上一個函數加到update中,實現即時更新。

//即時更新  - (void)update:(ccTime)dt {                [self updateBackground:dt];     }

修改init函數

<span style="color:#362e2b">-(id) init  {      // always call "super" init      // Apple recommends to re-assign "self" with the "super's" return value      if( (self=[super init]) ) {          CGSize winSize = [CCDirector sharedDirector].winSize;                        // 1) 建立 CCParallaxNode視差滾動節點          backgroundNode = [CCParallaxNode node];          [self addChild:backgroundNode z:-2];                        // 2) 建立需要添加到CCParallaxNode視差滾動節點的精靈對象                        mainBg = [CCSprite spriteWithFile:@"bg.png"];          // 3) 設定雲彩的浮動速度 和背景速度                       CGPoint bgSpeed = ccp(0.05, 0.05);                                      // 4) 將精靈對象添加為CCParallaxNode視差滾動節點的子節點          [backgroundNode addChild:mainBg z:-1                     parallaxRatio:bgSpeed                    positionOffset:ccp(200,winSize.height*0.5)];                         [self scheduleUpdate];//</span><span style="color:#ff6666">調用update,注意了,這個調用方法很奇特</span><span style="color:#362e2b">              }      return self;  }  </span>

原始碼資源下載地址:http://download.csdn.net/detail/gshengod/6425997

作者:csdn部落格 李博Garvin

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.