サンプル

iPhoneのオモチャ箱 iPhone SDKプログラミング iPhoneのオモチャ箱 iPhone SDKプログラミング

上記の本のサンプルのメモを書きます。

[cc lang="c"] [[CCDirector sharedDirector] runWithScene: [HelloWorld scene]]; [/cc]

上記は[HelloWorld scene]というシーンを実行しなさい。とディレクターがシーンを切り替えているコードです。

[cc lang="c"] [self schedule:@selector(hondleTimeEvent) interval:1/60.0]; [/cc]

1秒間に60回 hondleTimeEvent が呼び出されます。

順番が大事。

メソッドを作成する際に、呼び出すメソッドの前に書いた方がいい。 distanceFromPoint、gameOverがhandleTimeEventメソッドよりも前にこないとビルドできなかった。

[cc lang="c"]

-(float)distanceFromPoint:(CGPoint)p1 toPoint:(CGPoint)p2 {

return sqrt*1; }

-(void)gameOver{

myShip.visible = NO;

CCSprite* gameOverLog = [CCSprite spriteWithFile:@"gameover.png"]; gameOverLog.position = ccp(160,300); [self addChild:gameOverLog]; }

-(void)handleTimeEvent{

if(random() % 60 == 0 && numOfMeteo < MAX_METEO){ meteo[numOfMeteo] = [CCSprite spriteWithFile:@"meteo.png"]; meteo[numOfMeteo].position = ccp(random() %320,500); [self addChild:meteo[numOfMeteo]];

id moveAction = [CCMoveTo actionWithDuration:4 position:ccp(random()%320,-30)]; id rotateAction = [CCRotateTo actionWithDuration:4 angle:180]; id action = [CCSpawn actions:moveAction,rotateAction,nil];

[meteo[numOfMeteo] runAction:action];

numOfMeteo++; }

for (int i=0; i < numOfMeteo; i++) {

if ([self distanceFromPoint:myShip.position toPoint:meteo[i].position] < 50) {

if (isgameOver == NO) { [self gameOver]; isgameOver = YES;

} }

if(meteo[i].position.y < 0){

[self removeChild:meteo[i] cleanup:YES]; meteo[i] = meteo[numOfMeteo -1]; numOfMeteo --; i--; } } } [/cc]

[cc lang="c"] [[CCDirector sharedDirector] runWithScene: [Title scene]]; [/cc]

applicationDidFinishLaunchingの中。 最初に始めるシーンをディレクタで指定している。この場合はTitleを最初に表示する。

書き忘れに注意。 self.isTouchEnabled = YES;

*1:p2.x-p1.x)(p2.x-p1.x)+(p2.y-p1.y)(p2.y-p1.y