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

2010年11月18日 星期四

iPhone 開發筆記18: cocos2d MultipleTouch

A. 新增 cocos2d Application Project: boardGame5

B. HelloWorldScene.h
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"

// HelloWorld Layer                 //@add
@interface HelloWorld : CCLayer <CCTargetedTouchDelegate>

// returns a Scene that contains the HelloWorld as the only child
+(id) scene;

@end

-----------------------------------------------------------------------------------------------

C. boardGame5AppDelegate.m

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
    CC_DIRECTOR_INIT();
   
    //@add
    // Init the window
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
   
    //@add
    [window setUserInteractionEnabled:YES]; 
    [window setMultipleTouchEnabled:YES];
    ....
}

-----------------------------------------------------------------------------------------------

D. HelloWorldScene.m

// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init] )) {
       
        //@add
        self.isTouchEnabled = YES;  
        // @add
        [[CCTouchDispatcher sharedDispatcher]
         addTargetedDelegate:self priority:0 swallowsTouches:YES];
    ....
    }
    return self;
}


// @add
-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent*)event
{   
    CGPoint touchLocation=[touch locationInView:[touch view]];
    touchLocation=[[CCDirector sharedDirector] convertToGL:touchLocation];
    CGPoint nodePosition = [self convertToNodeSpace: touchLocation];
   
    int t_x = nodePosition.x;
    int t_y = nodePosition.y;    

    ....
   
    return YES;
}

2010年11月17日 星期三

iPhone 開發筆記17: cocos2d 圖片動畫效果

A. 新增 cocos2d Application Project.

B. 在 HelloWorldScene.m:
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init] )) {
        ....
        // 從左上角往右下角切割圖檔
        //CCSprite *sprite = [CCSprite spriteWithFile:@"Icon.png" rect:CGRectMake(20, 20, 57, 57)];

        CCSprite *sprite = [CCSprite spriteWithFile:@"Icon.png"]; // source size
        sprite.position = ccp(240, 50);
   
        /* 動畫效果: case01 -> normal */
        id action =
        // (1). CCJumpTo: 跳躍至絕對座標
        // [CCJumpTo actionWithDuration:2 position:ccp(300,0) height:50 jumps:4]; // -> 300, 0

        // (2). CCJumpBy: 跳躍至相對座標(以原始座標累加距離的跳躍)
        // [CCJumpBy actionWithDuration:2 position:ccp(300,0) height:50 jumps:4]; // -> 540, 50

        // (3). CCRotateTo: 旋轉
        // [CCRotateTo actionWithDuration:2 angle:180];
       
        // (4). 淡入
        // [CCFadeIn actionWithDuration:2];
       
        // (5). 淡出
        // [CCFadeOut actionWithDuration:2];
       
        // (6). 移至絕對座標
        // [CCMoveTo actionWithDuration:2 position: ccp(100, 100)];
       
        // (7). 移至相對座標
        // [CCMoveBy actionWithDuration:2 position: ccp(100, 100)]; // 340, 150

        // 混合動作
        // (8). CCSequence: one by one (連續動作)
        [CCSequence actions:
        // [CCRotateTo actionWithDuration:2 angle:180], // ---> error with reverse happend?
        [CCRotateBy actionWithDuration:2 angle:180],
        [CCJumpBy actionWithDuration:2 position:ccp(300,0) height:50 jumps:4],
        [CCFadeOut actionWithDuration:2],
        [CCCallFunc actionWithTarget:self selector:@selector(callback1)],
        nil];

        // 混合動作
        // (9). CCSpawn: do it the sametime (同時動作)
        /*
         [CCSpawn actions:
         [CCRotateTo actionWithDuration:2 angle:180], // ---> error with reverse happend?
         [CCRotateBy actionWithDuration:2 angle:180],
         [CCJumpBy actionWithDuration:2 position:ccp(300,0) height:50 jumps:4],
         [CCFadeOut actionWithDuration:2],
         [CCCallFunc actionWithTarget:self selector:@selector(callback1)],
         nil];
         */

        /* 動畫效果: case02 -> repeat */
        // (10). CCRepeatForever: 無限重複執行
        // CCRepeatForever *repeat = [CCRepeatForever actionWithAction:action];
       
        // (11). CCRepeat: 重複執行
        // CCRepeat *repeat = [CCRepeat actionWithAction:action times:2];

       
/* 動畫效果: case03 -> reverse */
        // (12). reverse: 反向執行
        // id t_reverse = [CCSequence actions:action, [action reverse], nil];

        // runAction
        // case 1: for normal
        [sprite runAction:action];
       
        // case 2: for repeat
        // [sprite runAction:repeat];
       
        // case 3: for reverse
        // [sprite runAction:t_reverse];

        [self addChild: sprite];
    }
    return self;
}


- (void) callback1
{
    CCLabel *labelEnd = [CCLabel labelWithString:@"Game Over" fontName:@"Marker Felt" fontSize:64];
   
    CGSize size = [[CCDirector sharedDirector] winSize];
   
    // position the label on the center of the screen
    labelEnd.position = ccp( size.width /2 , size.height/2 - 100 );
   
    [self addChild: labelEnd];
}

iPhone 開發筆記16: cocos2d 安裝與移除

A. cocos2d 官網: http://www.cocos2d-iphone.org/

B. 下載穩定版本(Stable version): http://www.cocos2d-iphone.org/download

C. 安裝方式:
   1. 解壓縮下載的檔案: cocos2d-iphone-0.99.4.tar.gz
   2. 在 Terminal 下, 到解壓縮目錄內執行:
      $ cd cocos2d-iphone-0.99.4
      $ sudo ./install-templates.sh
   3. 可以看到以下的安裝訊息:
      Installing cocos2d template:
      /Library/Application Support/Developer/Shared/Xcode/Project Templates/cocos2d 0.99.4/cocos2d Application/

      Installing cocos2d + box2d template:
      /Library/Application Support/Developer/Shared/Xcode/Project Templates/cocos2d 0.99.4/cocos2d Box2d Application/

      Installing cocos2d + chipmunk template:
      /Library/Application Support/Developer/Shared/Xcode/Project Templates/cocos2d 0.99.4/cocos2d Chipmunk Application/

      creating destination directory:
      /Library/Application Support/Developer/Shared/Xcode/File Templates/cocos2d 0.99.4/

D. 移除方式:
   刪除: C 安裝後所產生的目錄即可.