第一步:安裝cocos2d
從http://www.cocos2d-iphone.org/download下載cocos2d模板,建議下在穩定的版本。打開終端,切換到cocos2d的解壓目錄,執行命令: ./install-templates.sh,安裝完成,打開xcode4.4,如下圖:
cocos2d-iphone 安裝成功。
第二步:創建cocos2d應用程序
選擇cocos2d ios模板,應用名稱輸入HelloWord,完成創建。打開應用,如下圖:
文件目錄如下(注:xcode版本:4.4.1)
第三步:添加精靈
本篇的所有編碼都將在HelloWorldLayer.m文件例完成。
總共添加兩個精靈,我們需添加一個額外的圖片,用作一個sprite。另外一個使用以包含在項目裏的icon.png。
定義變量:
1 #import "HelloWorldLayer.h" 2 #import "CCTouchDispatcher.h" 3 4 // Needed to obtain the Navigation Controller 5 #import "AppDelegate.h" 6 7 #pragma mark - HelloWorldLayer 8 9 CCSprite *seeker1; 10 CCSprite *cocosGuy; 11 12 // HelloWorldLayer implementation 13 @implementation HelloWorldLayer
代碼如下
1 // on "init" you need to initialize your instance 2 -(id) init 3 { 4 // always call "super" init 5 // Apple recommends to re-assign "self" with the "super's" return value 6 if( (self=[super init]) ) { 7 8 // create adn initealize our seeker sprite,and add it to this layer 9 seeker1 = [CCSprite spriteWithFile:@"seeker.png"]; 10 seeker1.position = ccp(50, 100); 11 [self addChild:seeker1]; 12 [self schedule:@selector(nextFrame:)]; 13 14 //do the same our cocos2d guy, reusing the app icon as its imange 15 cocosGuy = [CCSprite spriteWithFile:@"Icon.png"]; 16 cocosGuy.position = ccp(200, 180); 17 [self addChild:cocosGuy]; 18 19 self.isTouchEnabled = YES; 20 21 } 22 return self; 23 }
保存運行:
每三行增加一個精靈,使用“ccp”(cocos2d的點)宏來創建一個二維的X和Y座標點(CGPoint結構),請注意,在cocos2d,X座標從在屏幕的左側並向右增加,Y座標開始,在屏幕的底部和增加向上。
最後,每個精靈的第三行調用“addChild”添加新創建的精靈的層。
移動精靈:有兩種方法
1.使用一個行爲告訴精靈超過一定時間自動移動到目標位置。
2.在遊戲的運行過程中調用方法讓精靈自己移動。
在這個例子中,我們採取第二種方法安排一個週期性的回調讓“seeker1”sprite移動,代碼爲:[self schedule:@selector(nextFrame:)];
並添加方法:
1 - (void)nextFrame:(ccTime)dt{ 2 seeker1.position = ccp(seeker1.position.x + 100*dt, seeker1.position.y); 3 if (seeker1.position.x > 480 + 32) { 4 seeker1.position = ccp(-32, seeker1.position.y); 5 } 6 }
這項工作的效應是,在每一幀的動畫,cocos2d的庫將調用你的“nextFrame”事件,並經過從上一幀到這一幀的時間(以秒爲單位)。這是你做各種事的機會:移動精靈的周圍,檢查碰撞,更新物理模型,產生新的敵人,刪除不再需要的精靈等。
精靈的遞增位置:X,100*DT 意味着,在一秒鐘內,精靈將移動100個像素,然後我們檢查看sprite是否已經離開屏幕。我們在此假設在橫向模式下,這是480像素寬,使你的代碼更通用的,你需要回調[CCDirector sharedDirector] winSize]得到屏幕的實際大小。但現在480就行,我們加32,因爲這是這個精靈的寬度的一半 - 這樣的中心是超越480 +32時,整個精靈的右側消失在屏幕上。當這種情況發生時,我們重設x位置爲-32,從而將其置於完全消失於屏幕的左側。
第四步:響應觸摸
響應觸摸是有點不同的調度回調的處理事件。事件包括像觸摸屏和加速度計的讀數。已經有具體定義的方法爲這些-在觸摸事件的情況下,有兩種不同的方法:“standard”或“targeted”。請參閱Touch Delegates進行更深層次的討論。
添加頭文件:#import "CCTouchDispatcher.h"
我們需要告訴CClayer我們要的是觸摸事件的“targeted”而不是“standard”,代碼如下:
-(void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
在XCODE4.4.1中會提示sharedDispatcher方法被捨棄,具體情況以後討論。
在init方法中添加:self.isTouchEnabled = YES 註冊觸摸事件。
由於我們使用的是觸摸事件的“targeted”,所以必須至少實現ccTouchBegin方法,返回YES告訴touch dispather 你要求的這個觸摸,添加代碼:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
return YES;
}
這次觸摸也會導致另一個觸摸方法被回調,我們在下面這個方法裏編寫代碼讓第二個精靈移動到觸摸結束的地方:
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint location = [self convertTouchToNodeSpace:touch];
[cocosGuy stopAllActions];
[cocosGuy runAction:[CCMoveTo actionWithDuration:1 position:location]];
}
convertTouchToNodeSpace 做了一下幾件事情:
1. 得到在這個視圖上觸摸的位置location
2.它要求CCDirector將其轉換爲GL座標,也就是我們使用同一座標系畫圖。(例如,縱向和橫向座標的轉換)
3.最後它將這些GL座標轉換到節點空間,這樣做是爲了CCLayer,使它沒有區別。
後邊的代碼的目的是停止精靈當前的活動,進行下面的動作。
運行代碼,將會看到,seeker1勻速往右運動,點擊屏幕cocosGuy移動到點擊的地方。
這次就寫到這吧,有什麼不對的地方,還希望閱者指正!
參考網址:http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:lesson_2._your_first_game