ラベル cocos2d の投稿を表示しています。 すべての投稿を表示
ラベル cocos2d の投稿を表示しています。 すべての投稿を表示

2014年3月12日水曜日

cocos2dでの並列処理

NSOperationQueueを使う


NSOperationQueue* queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
    [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"bgm2.mp3"];
}];

2014年3月1日土曜日

CCLayerのアンカーポイント変更

CCLayerのアンカーポイント変更


CCLayerはアンカーポイントの無視する設定になっているのでそれを解除する必要がある

//アンカーポイントを無視を無効化
layer.ignoreAnchorPointForPosition = NO;

//アンカーポイントの変更
layer.anchorPoint = ccp(0.5, 0.5);

2014年2月28日金曜日

cocos2dのpopSceneの注意点

pushSceneでシーンをプッシュし、popSceneでシーンを破棄するときに呼び出し元のシーンのonEnterが呼ばれるので注意すること。

2014年2月27日木曜日

cocos2d:CCLineBreakModeがdeprecated

CCLineBreakModeがdeprecatedの場合、頭にkをつける


CCLineBreakModeWordWrap → kCCLineBreakModeWordWrap
CCLineBreakModeCharacterWrap → kCCLineBreakModeCharacterWrap

cocos2d:CCTextAlignmentがdeprecated

CCTextAlignmentの値がdeprecatedの場合、頭にkをつける。


CCTextAlignmentCenter → kCCTextAlignmentCenter
CCTextAlignmentLeft → kCCTextAlignmentLeft
CCTextAlignmentRight → kCCTextAlignmentRight

2014年2月26日水曜日

cocos2dでメニューに画像を使う

CCMenuItemSpriteを使用する


//選択前のボタン
CCSprite *nomalButton = [CCSprite spriteWithFile:@"button.png"];

//選択後のボタン(色を暗くした同じ画像)
CCSprite *selectedButton = [CCSprite spriteWithFile:@"button.png"];
selectedButton.color = ccc3(64, 64, 64);
    
CCMenuItemSprite *item = [CCMenuItemSprite itemWithNormalSprite:nomalButton selectedSprite:selectedButton target:self selector:@selector(tapButton)];
CCMenu* menu = [CCMenu menuWithItems:item, nil];
[self addChild:menu];

2014年2月24日月曜日

cocos2dでAppDelegateの取得

cocos2dでAppDelegateの取得


AppController *appDelegate = (AppController *)[[UIApplication sharedApplication] delegate];

2014年2月21日金曜日

cocos2dでポーズの実装と注意点

cocos2dでポーズの実装と注意点


ポーズと解除


//ポーズ
[[CCDirector sharedDirector] pause];

//ポーズ解除
[[CCDirector sharedDirector] resume];


注意点


1.ホームボタンでのポーズ

AppDelegateの以下のメソッドにもpause,resumeが書かれている。
  • applicationWillResignActive
  • applicationDidBecomeActive
これはホームボタンが押されたときに呼ばれる。
これらのメソッドが呼ばれても問題ないようにフラグ等を追加して制御する必要がある。

2.タッチメソッド

ポーズ中でもタッチ機能は生きているので別途無効化する必要がある。

2014年2月19日水曜日

cocos2dで複数のアクションを同時実行

cocos2dで複数のアクションを同時実行する方法


CCSpawnを使用


id fadeIn = [CCFadeIn actionWithDuration:1.0];
id jump = [CCJumpBy actionWithDuration:1.0 position:ccp(0, 50) height:50 jumps:1];
id spawn = [CCSpawn actions:fadeIn, jump, nil];
[sprite runAction:spawn];

2014年2月18日火曜日

cocos2dでスクロールするレイヤー

Cocos2d「Extensions」を使用してスクロールするレイヤーを実装


ダウンロード



Xcodeに追加

ZIPのExtensions->CCScrollLayerをフォルダごと追加


インポート


#import "CCScrollLayer.h"


初期設定


- (id) init {
 if ((self = [super init])){
  // シーンを複数作成
  NSMutableArray *layerArray = [NSMutableArray array];
  CCLayer* layer1 = [CCLayer node];
  CCLayer* layer2 = [CCLayer node];
  CCLayer* layer3 = [CCLayer node];
  
  [layerArray addObject:layer1];
  [layerArray addObject:layer2];
  [layerArray addObject:layer3];
  
  // CCScrollLayerに追加
  CCScrollLayer *scroller = [[CCScrollLayer alloc] initWithLayers:layerArray widthOffset: 0];

  //ページインジケーターのポジション
  scroller.pagesIndicatorPosition = ccp(self.contentSize.width / 2, 60);
  [self addChild:scroller];
 }
}

2014年2月14日金曜日

cocos2dでのスケジューラーの使い方

//スタート
[[[CCDirector sharedDirector] scheduler] scheduleSelector:@selector(hoge) forTarget:self interval:0.5 paused:NO];

//ストップ
[[[CCDirector sharedDirector] scheduler] unscheduleSelector:@selector(hoge) forTarget:self];

//ポーズ
[[[CCDirector sharedDirector] scheduler] pauseTarget:self];

//ポーズ解除
[[[CCDirector sharedDirector] scheduler] resumeTarget:self];

2013年11月21日木曜日

2013年10月23日水曜日

cocos2dでTextField

CCUIViewWrapperを使用

以下のURLよりソースを取得

※そのままではBuild時に警告が出るので以下を修正
[[[[CCDirector sharedDirector] view] window] addSubview: uiItem];
//[[[CCDirector sharedDirector] openGLView] addSubview:uiItem];

/****************************************/

//if(!p.isRelativeAnchorPoint)
if(p.ignoreAnchorPointForPosition)
    transform = CGAffineTransformTranslate(transform, p.anchorPoint.x, p.anchorPoint.y);
    //transform = CGAffineTransformTranslate(transform, p.anchorPointInPixels.x, p.anchorPointInPixels.y);

/****************************************/

transform = CGAffineTransformTranslate(transform, -p.anchorPoint.x, -p.anchorPoint.y);
//transform = CGAffineTransformTranslate(transform, -p.anchorPointInPixels.x, -p.anchorPointInPixels.y);


UITextFieldを実装

ヘッダー

@interface GameLayer : CCLayer <UITextFieldDelegate> {
    CCUIViewWrapper *textFieldWrapper;
    UITextField *textBox;
}
@end

実装

- (void)addTextField {
    textBox = [[[UITextField alloc] init] autorelease];
    textBox.frame = CGRectMake(175, 120, 110, 25);
    textBox.borderStyle = UITextBorderStyleRoundedRect;
    textBox.placeholder = @"hogehoge";
    textBox.returnKeyType = UIReturnKeyDone;
    textBox.clearButtonMode = UITextFieldViewModeWhileEditing;
    textBox.autocapitalizationType = UITextAutocapitalizationTypeNone;
    textBox.enablesReturnKeyAutomatically = YES;
    textBox.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
    textBox.textAlignment = NSTextAlignmentCenter;
    textFieldWrapper = [CCUIViewWrapper wrapperForUIView:textBox];
    textBox.delegate = self;
    [self addChild:textFieldWrapper];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    CCLOG(@"text->%@", textBox.text);
}

2013年10月11日金曜日

GameCenter



事前準備

iTunesConnectでアプリを登録し、GameCenterを有効にする


Info.plistの設定

Required device capabilitiesに「gamekit Boolean YES」を追加


認証

// GameCenter認証
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
    if (error != nil) {
        //エラー処理
    }
}];

GKLeaderboardViewControllerDelegateプロトコル適用

//ヘッダーファイルにプロトコル追加
@interface TestLayer : CCLayer <GKLeaderboardViewControllerDelegate>


得点を送信

//iTunesConnectで設定したリーダーボードIDを設定
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:@"Leaderboard ID"] autorelease];
scoreReporter.value = HiScore;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
    if (error != nil) {
        //エラー処理
    }
}];


リーダーボードの表示

GKLeaderboardViewController *lbController = [[GKLeaderboardViewController alloc] init];
if (lbController != nil) {
    lbController.leaderboardDelegate = self;
    [[CCDirector sharedDirector] presentModalViewController:lbController animated:YES];
}

//リーダーボードを抜ける時の処理
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController {
    [[CCDirector sharedDirector] dismissViewControllerAnimated:YES completion:^{
        //
    }];
}

2013年10月7日月曜日

乱数

//シード
srandom(time(0));
//0-9を取得
int num = CCRANDOM_0_1() * 10;

2013年10月3日木曜日

ノードの削除

自分自身を削除

//YESでアクションやスケジュールもクリーンアップ
[self removeFromParentAndCleanup:YES];
//removeFromParentAndCleanup:YESと同じ
[self removeFromParent]


親ノードから削除

[self removeChildByTag:101]

2013年10月2日水曜日

CGPointの演算

//加算 (c1.x+c2.x,c1.y+c2.y)
ccpAdd(c1, c2);

//減算 (c1.x-c2.x,c1.y-c2.y)
ccpSub(c1, c2);

ベジェ曲線での移動

ccBezierConfig bezConf;
bezConf.controlPoint_1 = ccp(0, 0);
bezConf.controlPoint_2 = ccp(30, 0);
bezConf.endPosition = ccp(30, 10);
id move = [CCBezierBy actionWithDuration:1.0 bezier:bezConf];
[self runAction:move];

2013年10月1日火曜日

時間調整のアクション

//時間調整
id delay = [CCDelayTime actionWithDuration:1.0];
CCSequence* seq = [CCSequence actions:delay, action ,nil];
[self runAction:seq];

座標の絶対値、相対値変換

スプライトからの相対値(ローカル座標)を、画面上の絶対座標(ワールド座標)に変換

//絶対値→相対値
CGPoint local = [sprite convertToNodeSpace:ccp(x, y)];

//相対値→絶対値
CGPoint world = [sprite convertToWorldSpace:ccp(x, y)];