2011年12月30日金曜日
タイルマップの作り方
CCTMXTiledMap *map;
CCTMXLayer *bgLayer;
@property (nonatomic,retain)CCTMXTiledMap *map;
@property (nonatomic,retain)CCTMXLayer *bgLayer;
@synthesize map;
@synthesize bgLayer;
self.map = [CCTMXTiledMap tiledMapWithTMXFile:@"theMap.tmx"];
self.bgLayer = [map layerNameed:@"bg"];
[self addChilde:map z:-1];
self.map = nil;
self.bgLayer = nil;
CCTMXLayer *bgLayer;
@property (nonatomic,retain)CCTMXTiledMap *map;
@property (nonatomic,retain)CCTMXLayer *bgLayer;
@synthesize map;
@synthesize bgLayer;
self.map = [CCTMXTiledMap tiledMapWithTMXFile:@"theMap.tmx"];
self.bgLayer = [map layerNameed:@"bg"];
[self addChilde:map z:-1];
self.map = nil;
self.bgLayer = nil;
ラベルの作り方
CCLabel *label
label=[CCLabel labelWithString:@"HelloWorld" forName:@"Marker Felt" fontSize:24];
[self add:label];
[label setString:@"Hello world"];
label.color = ccc3(0,0,0);
label=[CCLabel labelWithString:@"HelloWorld" forName:@"Marker Felt" fontSize:24];
[self add:label];
[label setString:@"Hello world"];
label.color = ccc3(0,0,0);
データの保存
書き込み
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
[ud setObject:data forKey:@"dataKey"];
[ud synchronize];
読み込み
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
NSString *theString=[ud objectForKey:@"theStringKey"];
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
[ud setObject:data forKey:@"dataKey"];
[ud synchronize];
読み込み
NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
NSString *theString=[ud objectForKey:@"theStringKey"];
トランジション、シーンの変更
シーンの変更
#import "newScene.h"
ロード中
[[CCDirector sharedDirector] pushScene:[newScene scene];
シーンの変更
[[CCDirector sharedDirector] replaceScene:[newScene scene];
トランジション
[[CCDirector sharedDirector] replaceScene:[CCTransitionPageTurn transitionWithDuration:1 scene:[newScene scene]];
#import "newScene.h"
ロード中
[[CCDirector sharedDirector] pushScene:[newScene scene];
シーンの変更
[[CCDirector sharedDirector] replaceScene:[newScene scene];
トランジション
[[CCDirector sharedDirector] replaceScene:[CCTransitionPageTurn transitionWithDuration:1 scene:[newScene scene]];
シーンの作り方
それぞれクラスを作る。(CCLayerがサブクラス)
.h
+(CCScene *) scene;
.m
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
newScene *layer = [newScene node];
[scene addChild:layer];
return scene;
}
-(id)init
{
if((self=[super init])){
return self;
}
}
.h
+(CCScene *) scene;
.m
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
newScene *layer = [newScene node];
[scene addChild:layer];
return scene;
}
-(id)init
{
if((self=[super init])){
return self;
}
}
音楽をつける
#import "SimpleAudioEngine.h"
エフェクト
[[SimpleAudioEngine sharedEngine] preloadEffect:@"sound.mp3"]; (プレロードinit内)
[[SimpleAudioEngine sharedEngine] playEffect:@"sound.mp3"];
BGM(繰り返し)
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"sound.mp3"]; (プレロードinit内)
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"sound.mp3"];
エフェクト
[[SimpleAudioEngine sharedEngine] preloadEffect:@"sound.mp3"]; (プレロードinit内)
[[SimpleAudioEngine sharedEngine] playEffect:@"sound.mp3"];
BGM(繰り返し)
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"sound.mp3"]; (プレロードinit内)
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"sound.mp3"];
2011年12月29日木曜日
ボタンの作り方
CCMenuItem *button=[CCMenuItemImage itemFromNormalImage:@"button.png" selectecdImage:@"button.png" target:self selector:@selector(push)];
button.position=ccp(100,100);
CCMenu *menu=[CCMenu menuWithItems:putton,nil];
[self addChild:menu];
-(void)push{
}
button.position=ccp(100,100);
CCMenu *menu=[CCMenu menuWithItems:putton,nil];
[self addChild:menu];
-(void)push{
}
タッチイベント
#import "CCTouchDispatcher.h"
[[CCTouchDispatcher sharedDispatcher] addTargetDelegate:self priority:0 swallowsTouches:YES];
self.isTouchEnabled = YES;
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
return YES;
}
-(void)ccTouchEnded:(UItouch *)touch withEvent:(UIEvent *)event{
CGPoint location=[touch locationInView:[touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector]convertToGL:location];
[[CCTouchDispatcher sharedDispatcher] addTargetDelegate:self priority:0 swallowsTouches:YES];
self.isTouchEnabled = YES;
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
return YES;
}
-(void)ccTouchEnded:(UItouch *)touch withEvent:(UIEvent *)event{
CGPoint location=[touch locationInView:[touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector]convertToGL:location];
スプライトシート(アニメーション)
リソース
sprite.plist
sprite.png
sprite1.png
インスタンス
CCSpriteFrameCache *cache (sprite.plist)
NSMutableArray *array (sprite(i).png)
(NSString *name)
id animObject (array)
id animAction (animObject)
CCSprite *sprite (sprite.1とanimAction)
//cache
CCSpriteFrameCache *cache=[CCSpriteFrameCache sharedSpriteFrameCache];
[cache addSpriteFrameWithFile:@"sprite.plist"];
//frame array
NSMutableArray *framesArray = [NSMutableArray array];
for (int i=1;i<10;i++){
NSString *name=[NSString stringWithFormat:@"sprite.png",i];
id object=[cache spriteFrameByName name];
[framesArray addObject:object];
}
//animation object
id animOb = [CCAnimation animationWithFrames:framesArray delay:0.1];
//animation action
id animAct = [CCAnimate actionWithAnimation:animOb restoreOriginalFrame:NO];
animAct = [CCRepeatForever actionWithAction:animAct];
//sprite
CCSprite *sprite=[CCSprite spriteWithSpriteFrameName:@"sprite1.png"];
[self addChilde:sprite];
[sprite runAction:animAct];
スケジュール、コールバック
コールバック
id callback = [CCCallFunc actionWithTarget:self selector:@selector(showSun)];
-(void) showSun { }
スケジュール
[self schedule:@selector(frame:)];
-(void) frame:(ccTime)dt{ }
アップデート
[self scheduleUpdate];
-(void)update:(ccTime)deltaTime{ }
id callback = [CCCallFunc actionWithTarget:self selector:@selector(showSun)];
-(void) showSun { }
スケジュール
[self schedule:@selector(frame:)];
-(void) frame:(ccTime)dt{ }
アップデート
[self scheduleUpdate];
-(void)update:(ccTime)deltaTime{ }
イージング
<action1にイージングをつける>
id ease = [CCEaseInOut actionWithAction:action1 rate:3];
種類
CCEaseIn
CCEaseOut
CCEaseInOut
id ease = [CCEaseInOut actionWithAction:action1 rate:3];
種類
CCEaseIn
CCEaseOut
CCEaseInOut
アクション
リバース
id revAct =[action1 reverse];
リピート
id repAct =[CCRepeat actionWithAction:action1 times:回数];
id repAct =[CCRepeatForever actionWithAction:action1];
順番
id seq = [CCSequence actions:action1,action2,action3,nil];
同時
id spa = [CCSpawn actions:action1,action2,action3,nil];
id revAct =[action1 reverse];
リピート
id repAct =[CCRepeat actionWithAction:action1 times:回数];
id repAct =[CCRepeatForever actionWithAction:action1];
順番
id seq = [CCSequence actions:action1,action2,action3,nil];
同時
id spa = [CCSpawn actions:action1,action2,action3,nil];
アクションの種類
基本
id move = [CCMoveTo actionWithDuration:4.2 position:ccp(100,100)];
[bird runAction:move];
移動
CCMoveBy actionWithDuration:秒数 position:ccp(100,100)
CCMoveTo actionWithDuration:秒数 position:ccp(100,100)
CCJumpBy actionWithDuration:秒数 position:ccp(0,0)height:高さ jumps:回数
CCJumpTo actionWithDuration:秒数 position:ccp(0,0) height:高さ jumps:回数
拡大
CCScaleBy actionWithDuration:秒数 scale:倍率
CCScaleTo actionWithDuration:秒数 scale:倍率
回転
CCRotateBy actionWithDuration:秒数 angle:角度
CCRotateBy actionWithDuration:秒数 angle:角度
表示
CCShow action
CCHide action
CCBlink actionWithDuration:秒数 blinks:回数
CCToggleVisibility action
(↑表示と非表示の切り替え)
フェイドイン、アウト
色
CCTintBy actionWithDuration:秒数 red:値 green:値 blue:値
CCTintTo actionWithDuration:秒数 red:値 green:値 blue:値
ストップ
CCDelayTime actionWithDuration:秒数
id move = [CCMoveTo actionWithDuration:4.2 position:ccp(100,100)];
[bird runAction:move];
移動
CCMoveBy actionWithDuration:秒数 position:ccp(100,100)
CCMoveTo actionWithDuration:秒数 position:ccp(100,100)
CCJumpBy actionWithDuration:秒数 position:ccp(0,0)height:高さ jumps:回数
CCJumpTo actionWithDuration:秒数 position:ccp(0,0) height:高さ jumps:回数
拡大
CCScaleBy actionWithDuration:秒数 scale:倍率
CCScaleTo actionWithDuration:秒数 scale:倍率
回転
CCRotateBy actionWithDuration:秒数 angle:角度
CCRotateBy actionWithDuration:秒数 angle:角度
表示
CCShow action
CCHide action
CCBlink actionWithDuration:秒数 blinks:回数
CCToggleVisibility action
(↑表示と非表示の切り替え)
フェイドイン、アウト
色
CCTintBy actionWithDuration:秒数 red:値 green:値 blue:値
CCTintTo actionWithDuration:秒数 red:値 green:値 blue:値
ストップ
CCDelayTime actionWithDuration:秒数
バックを白にする
@interface HelloWorldLayer : CCLayerColor
{
}
@imprimentation HelloWorldLayer
-(id) init
{
if ( (self =[super initWithcolor:ccc4(225,225,225,225)]))
{
}
{
}
@imprimentation HelloWorldLayer
-(id) init
{
if ( (self =[super initWithcolor:ccc4(225,225,225,225)]))
{
}
スプライトの作り方
スプライト(bird)
CCSprite *bird=[CCSprite spriteWithFile:@"bird.png"];
bird.position=ccp(100,100);
[self addChild:bird];
CCSprite *bird=[CCSprite spriteWithFile:@"bird.png"];
bird.position=ccp(100,100);
[self addChild:bird];
登録:
コメント (Atom)