2013年9月27日金曜日

スプライトにタッチを実装

ヘッダー

@interface SpriteWithTouch : CCSprite <CCTouchOneByOneDelegate>

実装

- (void) onEnter {
    [super onEnter];
    [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    
    CGPoint touchLocation = [touch locationInView:touch.view];
    CGPoint location = [[CCDirector sharedDirector] convertToGL:touchLocation];
    //CCSpriteの描画領域でタッチされているか判定
    if (CGRectContainsPoint(self.boundingBox, location)) {
        CCLOG(@"touch!");
        return YES;
    } else {
        return NO;
    }
}

- (void) onExit {
    [super onExit];
    [[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self];
}

図形描画

矩形描画

- (void)draw {
    [super draw];
    ccDrawColor4B(255, 128, 0, 255);
    glLineWidth(5);
    ccDrawRect(ccp(0, 0), ccp(100, 100));
}

2013年9月26日木曜日

cocos2dでテクスチャアトラスを使う

スプライト

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sprite.plist"];
CCSprite *hoge = [[CCSprite node] initWithSpriteFrameName:@"hoge.png"];


アニメーション

CCAnimation *anime = [CCAnimation animation];
for (int cnt=1; cnt<=5; cnt++) {
    [anime addSpriteFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"hoge%02d.png", cnt]]];
}
anime.delayPerUnit = 0.1;
anime.loops = 1;
//キャッシュに追加
[[CCAnimationCache sharedAnimationCache] addAnimation:anime name:@"anime01"];

注意点

画像の最大サイズは各端末毎に異なる
端末最大サイズ
iPhone4, iPad2, iPodtouch 4th2048 * 2048
iPhone5, iPhone4S, iPad3, iPodtouch 5th4096 * 4096
iPhone3Gなど古い端末1024 * 1024

2013年9月25日水曜日

通知

送信
//通知送信
NSNotification *notification = [NSNotification notificationWithName:@"hoge" object:self];
[[NSNotificationCenter defaultCenter] postNotification:notification];


受信
//通知受信
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(test) name:@"hoge" object:nil];

//呼ばれる関数
- (void)test {
    NSLog(@"notification!");
}

削除
- (void) dealloc {
    //通知受信の削除
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc removeObserver:self name:@hoge" object:nil];
    [super dealloc];
}

2013年9月24日火曜日

プログレスバー

//initの処理
CCProgressTimer *timer = [CCProgressTimer progressWithSprite:[CCSprite spriteWithFile:@"test.png"]];
timer.type = kCCProgressTimerTypeRadial;
timer.percentage = 100;
timer.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:timer z:2 tag:123];
[self scheduleUpdate];


- (void) update:(ccTime)delta {
    CCProgressTimer *timer = (CCProgressTimer*)[self getChildByTag:123];
    timer.percentage -= delta * 150;
    if (timer.percentage <= 0) {
        timer.percentage = 100;
    }
}

2013年9月19日木曜日

画像、アニメのキャッシュ削除

- (void) dealloc {
    //画像、アニメのキャッシュ削除
    [CCAnimationCache purgeSharedAnimationCache];
    [[CCTextureCache sharedTextureCache] removeAllTextures];
        
    [super dealloc];
}

アニメーションの逆再生、順次実行

スプライトアニメーションは逆再生(リバース)したり、順番に実行することができる

 CCAnimation* anime = [[CCAnimationCache sharedAnimationCache] animationByName:@"anime"];
 id action = [CCAnimate actionWithAnimation:anime];
 //リバース
 id back = [action reverse];
 //順番に実行
 CCSequence* seq = [CCSequence actions:action, back, nil];
 [self runAction:seq];