網頁

顯示具有 Cocos2d 標籤的文章。 顯示所有文章
顯示具有 Cocos2d 標籤的文章。 顯示所有文章

2010年7月26日 星期一

iPhone-Cocos2d CCSprite Custom Touch Event

CCSprite 自定義觸控事件
Last Update: 2010/07/26 22:31


簡介


我們常用的那個 HelloWorldScene, 是繼承 CCLayer
它是可以註冊觸控事件 (ccTouchBegan...)
如果 CCSprite 本身有自動執行的行為, 並非與場景的互動
或是為了從場景獨立出來, or anything...
同樣的可以為 CCSprite 註冊


前置


環境:
XCode 3.2
Simulator 3.1.3
Cocos2d 0.99.4

準備工作:None

這裡有<主要的程式檔>可以載, 也可依下步驟完成



實作


  • 繼承CCSprite

  • 新增一個類別來繼承CCSprite
    咱們要覆寫 onEnter 及 onExit 來註冊和取消觸控事件
    因此咱們的 h 檔像這樣

    @interface MySprite : CCSprite < CCTargetedTouchDelegate > {
    CCLabel *label;
    }
    @property (nonatomic, retain) CCLabel *label;
    @end

    這裡的 label 是在觸控事件發生時顯示文字給我們看
    CCTargetedTouchDelegate 是實作觸控事件的協定


  • 註冊/取消觸控事件

  • 我們來到 mm檔

    - (void)onEnter{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:NO];
    [super onEnter];
    }

    - (void)onExit{
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
    [super onExit];
    }

    注意下 swallowsTouches:no 以及 priority:1
    如果你把優先權設的比場景所屬的觸控事件還高
    又把 swallowsTouches 設成 Yes
    那你的 CCSprite 就會把觸控事件偷偷吃掉, 不告訴你的場景


  • 觸控事件

  • 還是在同一個 mm檔

    - (CGRect)rect{
    CGSize s = [self boundingBox].size;
    return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
    }
    - (BOOL)containsTouchLocation:(UITouch *)touch{
    return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
    }

    上列程式, 這是為了判定是否點擊到的函式



    - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    if([self containsTouchLocation : touch]){
    [self->label setString:@"Dont touch me"];
    }
    return YES;
    }
    - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
    if([self containsTouchLocation : touch]){
    [self->label setString:@" "];
    }
    }

    這就是我們實作觸控事件的地方



  • 加入場景

  • 在我們最常用的 HelloWorldScene 裡 init 函式 的 if 區段
    用下述代碼覆寫

    CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Marker Felt" fontSize:64];
    CGSize size = [[CCDirector sharedDirector] winSize];
    label.position = ccp( size.width /2 , size.height/2 + 100);
    [self addChild: label];


    MySprite *sprite = [MySprite spriteWithFile:@"img.png"];
    sprite.label = label;
    sprite.position = ccp(size.width/2, 100);
    [self addChild:sprite];

    只是在原本的Hello World
    加入 已繼承 CCSprite 的 MySprite
    記得把你的圖檔放進專案裡, 順便調整一下位置
    還有引入 MySprite.h 標頭檔

    The End...

    2010年7月14日 星期三

    iPhone-Cocos2d Progress/Health Bar

    iPhone - Cocos2d 進度 / 血量 條
    Last Update: 2010/07/14 21:18



    前置


    環境:
    XCode 3.2.2
    Compile: Simulator 3.1.3-Debug
    iPhone SDK 4.0
    Cocos2d 0.99.4

    準備工作:
    外框及內部圖片


    實作






    新增Cocos2d專案以後
    將圖片加入Resource

    我們將用 CCProgressTimer 來做進度 / 血量 條
    把下列程式覆寫在 init 函式裡的 if 區段



    CCSprite *spriteBorder = [CCSprite spriteWithFile:@"hp_border.png"];
    spriteBorder.position = ccp(150,150);
    [self addChild:spriteBorder];

    先放個外框


    CCProgressTimer *spriteBar = [CCProgressTimer progressWithFile:@"hp_bar.png"];
    spriteBar.type = kCCProgressTimerTypeHorizontalBarLR;
    [spriteBorder addChild:spriteBar];
    [spriteBar setAnchorPoint:ccp(0,0)];

    .type 選擇水平從左到右
    將 Bar 加入外框的子節點
    並將它 "Anchor" 在(0, 0)的位置
    這樣省了計算外框和 Bar 的關係位置


    spriteBar.percentage = 80;
    CCAction *action = [CCProgressTo actionWithDuration:1 percent:1];
    [spriteBar runAction:action];

    2 種方式變更計量
    直接設定 以及 CCProgressTo (action)




    iPhone-Cocos2d Animation Frame 2

    iPhone-Cocos2d 動畫 (連續圖片播放) part.2
    Last Update: 2010/07/14 20:43

    前置


    環境:
    XCode 3.2.2
    Cocos2d 0.99.4

    準備工作:
    連續圖片


    步驟


    新增Cocos2d專案
    將圖片直接加入Resource
    將 init 函式裡的 Code 用下列程式碼代換
    滿淺顯易懂的, 就不說明了


    if( (self=[super init] )) {


    CCSprite *mySprite = [[CCSprite alloc] initWithFile:@"fox_0001.png"];
    CCSprite *mySprite2 = [[CCSprite alloc] initWithFile:@"fox_0001.png"];


    CCAnimation *walk = [[CCAnimation alloc] initWithName:@"walk" delay:0.1];
    for(int i=1;i<=10;i++){
    [walk addFrameWithFilename:[NSString stringWithFormat:@"fox_%04d.png", i]];
    }


    [mySprite addAnimation:walk];
    [mySprite2 addAnimation:walk];

    //[mySprite setDisplayFrame:@"walk" index:0];
    //[mySprite setDisplayFrame:@"walk" index:1];

    CCAction *action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]];
    CCAction *action2 = [CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO];
    [mySprite runAction:action];
    [mySprite2 runAction:action2];


    mySprite.position = ccp(150,150);
    mySprite2.position = ccp(300,150);

    [self addChild:mySprite];
    [self addChild:mySprite2];
    }
    return self;



    2010年7月6日 星期二

    iPhone-Cocos2d Other

    雜項 其它 未整理


    向量:
    ccpLength
    ccpLengSQ
    ccpDistance
    ccSub





    Call function
    CCCallFuncND eventFunc = [CCCallFuncND
    actionWithTarget:self
    selector:@selector(gotoGameScene:data:)
    data:role];
    [eventFunc execute];





    CCSequence, dynamically add action

    +(CCFiniteTimeAction *) getActionSequence: (NSArray *) actions
    {
    CCFiniteTimeAction *seq = nil;
    for (CCFiniteTimeAction *anAction in actions)
    {
    if (!seq){
    seq = anAction;
    }else{
    seq = [CCSequence actionOne:seq two:anAction];
    }
    }
    return seq;
    }








    自定義資料
    CCNode.tag
    CCNode.userData

    2010年6月29日 星期二

    iPhone-Cocos2d Animation Frame

    iPhone-Cocos2d 動畫 (連續圖片播放)
    Last Update: 2010/07/14 20:42


    前置


    環境:
    XCode 3.2.2
    Cocos2d 0.99.4

    準備工作 :
    Zwoptex (官網)
    連續圖片


    建立動畫資源檔案


    以 Zwoptex 製作 .plist 及 .png
    執行 Zwoptex => File => New
    再將所有圖片全選, 拖曳到 Zwoptex 的視窗裡

    點擊Untrim(不自動剪裁)
    選擇適當大小
    Layout -> Apply (自動排版)
    儲存圖片(Texture, 建議png)
    儲存描述檔 ( .plist, 格式為xml, 主要描述各圖檔座標)


    把它們加到專案的 Resource 裡吧
    加入後, 開啟 .plist -> Root -> Frame 可以看到原始圖檔名稱


    接下來的一切動作都只在 init 函式裡


    建立動畫來源


    接著要建立 CCSpriteSheet 及 CCAnimation

    我是以Cocos2d的範本建立專案
    如果什麼都沒做就 Debug 的話應該會看到 Hello World
    我們不需要這個
    請將 init 函式的 if 區塊內容清空
    加入 CCSpriteSheet 的 初始化, 看起來會像下面的程式碼

    -(id) init
    {
    if( (self=[super init] )) {
    CCSpriteSheet *ss = [CCSpriteSheet
    spriteSheetWithFile:@"fox.png"];
    }
    return self;
    }



    緊接著 CCSpriteSheet
    一樣在 if 區塊裡加入下面的程式碼, 將 CCAnimation 建立起來


    [[CCSpriteFrameCache sharedSpriteFrameCache]
    addSpriteFramesWithFile:@"fox.plist"];//Load plist.

    NSMutableArray *animFrames = [NSMutableArray array];
    for(int i = 1; i <= 10; ++i) {//for each frame.
    [animFrames addObject:
    [[CCSpriteFrameCache sharedSpriteFrameCache]
    spriteFrameByName:
    [NSString stringWithFormat:@"fox_%04d.png", i]]];
    }
    CCAnimation *anim = [CCAnimation
    animationWithName:@"walk"
    delay:0.1 frames:animFrames];

    OK, 一行行說吧
    先把 描述檔(.plist)載入

    再將每個 Frame 取出並儲存(for)
    其中的字串 @"fox_$04d.png"
    是圖檔的名稱格式, 它會填入4個數字, 不足補零

    最後我們有了圖檔的座標、大小和順序就可以建立動畫



    連結你的動畫



    CCAction *action = [CCRepeatForever actionWithAction:
    [CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO]];

    CCSprite 要執行什麼動作都必需要依靠 CCAction 來操作
    所以我們將 CCAnimation 與 CCAction 做個連結吧



    CCSprite *sprite =[CCSprite
    spriteWithSpriteFrameName:@"fox_0001.png"];

    CGSize winSize = [CCDirector sharedDirector].winSize;
    sprite.position = ccp(winSize.width/2, winSize.height/2);

    這裡值得提的, 大概是 spriteWithSpriteFrameName
    不再是用一般 Resource 的圖檔, 而是來自 Frame name



    [sprite runAction:action];
    [ss addChild:sprite];
    [self addChild:ss];

    sprite 要執行剛建好的動作(CCAction)
    再將 sprite 加入 ss(CCSpriteSheet), 以連結圖檔
    最後把 ss 加入當前場景即可

    恭喜,執行吧!!

    完整專案下載