More Related Content
More from Satoshi Oomori (9)
Cocoa勉強会20140419ゲームをつくってみる
- 25. アトラス
!
• テクスチャだけ
SKSpriteNode *obj = [SKSpriteNode spriteNodeWithImageNamed:@“Spaceship"];
• テクスチャアトラス
SKTextureAtlas *plateAnimatedAtlas =
[SKTextureAtlas atlasNamed:@"spaceship"];
NSUInteger numImages = plateAnimatedAtlas.textureNames.count;
for (int i=0; i < numImages; i++) {
NSString *textureName =
[NSString stringWithFormat:@"%03d", i];
SKTexture *temp =
[plateAnimatedAtlas textureNamed:textureName];
[plateFrames addObject:temp];
}
- 30. エミッター
//エミッターの作成
NSString *path = [[NSBundle mainBundle]
pathForResource:@"FireParticle" ofType:@"sks"];
static SKEmitterNode* spark2 = nil;
spark2 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
spark2.position = contact.contactPoint;
spark2.numParticlesToEmit = contact.collisionImpulse;
//アクション
SKAction *fadeOut = [SKAction fadeOutWithDuration:1.0f];
SKAction *remove = [SKAction removeFromParent];
SKAction *sequence = [SKAction sequence:@[fadeOut, remove ]];
[spark2 runAction:sequence];
[spark2 runAction:soundPlay];
!
[self addChild:spark2];
- 41. 敵キャラ
• 出現時間をリストにして、
NSArray *anArray =
@[@{@"name":@"teppan",@"time":@1.0f,@"posX":@100.0,@"pos
Y":@600.0},
@{@“name":@"teppan",@"time":@3.0f,@"posX":@200.0,@"posY"
:@600.0},
……
- 42. 敵キャラ
• リストに基づいてキャラクタを作成するようにした。
//0.3秒間隔で調査
if ((beforeTime + 0.3f) < currentTime){
[enemyArray enumerateObjectsUsingBlock:^(id obj,
NSUInteger idx, BOOL *stop) {
if ([[obj objectForKey:@"time"] floatValue] <
(currentTime - durationTime) ) {
if ([[obj objectForKey:@"name" ] isEqualToString:@"teppan"]) {
…
}
… …
}
…
}
…
}
- 50. EmitterNodeにAction
SKAction *soundPlay;
!
soundPlay = [SKAction playSoundFileNamed:@"bomb.m4a" waitForCompletion:NO];
!
//エミッターの作成
NSString *path = [[NSBundle mainBundle]
pathForResource:@"FireParticle" ofType:@"sks"];
static SKEmitterNode* spark2 = nil;
spark2 = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
spark2.position = contact.contactPoint;
spark2.numParticlesToEmit = contact.collisionImpulse;
//アニメーション
SKAction *fadeOut = [SKAction fadeOutWithDuration:1.0f];
SKAction *remove = [SKAction removeFromParent];
SKAction *sequence = [SKAction sequence:@[fadeOut, remove ]];
[spark2 runAction:sequence];
[spark2 runAction:soundPlay];
!
[self addChild:spark2];
ココ
ココ
- 52. BGM
@property (nonatomic) AVAudioPlayer *
backgroundMusicPlayer;
!
//BGM
NSError *error;
NSURL * backgroundMusicURL = [[NSBundle mainBundle]
URLForResource:@"BGM" withExtension:@"m4a"];
self.backgroundMusicPlayer = [[AVAudioPlayer
alloc] initWithContentsOfURL:backgroundMusicURL
error:&error];
!
self.backgroundMusicPlayer.numberOfLoops = -1;
[self.backgroundMusicPlayer prepareToPlay];
[self.backgroundMusicPlayer play];
- 81. タッチしたノードは?
• タッチした部分にあるノードは何?か調べて
SKNode *nodeAtPoint = [self nodeAtPoint:[touch
locationInNode:self]];
• あとはノードの名前で処理を変える
if ([nodeAtPoint.name isEqualToString: @"restart"]) {
OOOLabelButtonNode *startButton =
(OOOLabelButtonNode *)nodeAtPoint;
startButton.highlighted = YES;
}
- 124. 敵キャラAI
• 敵キャラと自機の距離が200pt以下になった場合砲弾を発射する。
SKAction *attackAction = [SKAction customActionWithDuration:2.0 actionBlock:^(SKNode *node,
CGFloat elapsedTime){
NSArray *nodes = [self.parent children];
[nodes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
SKNode *node = (SKNode *)obj;
if ([node.name isEqualToString: @"spaceship"]){
float deltaX = node.position.x - self.position.x;
float deltaY = node.position.y - self.position.y;
float delta = sqrtf(pow(deltaX,2.0) + pow(deltaY,2.0));
//NSLog(@"%.2f",delta);
if (delta > 200.0) {
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@150.0,@"posX",
[SKTransition fadeWithDuration:1],@"transition", nil];
if (!fireing){
[self performSelector:@selector(fireEnemyShell:) withObject:dic afterDelay:
0.0f];
fireing = YES;
}
}
}
}];
}];
[obj runAction:attackAction];
- 133. キャラクタの動作
NSArray *maruMotionArray =
@[@{@"command":@"move",@"x":@0.0,@"y":@0.0},
@{@"command":@"curve",@"x":@150.0,@"y":@-100.5,@"cp1x":@60.0
,@"cp1y":@-110.5,@"cp2x":@40.0,@"cp2y":@-90.5},
@{@"command":@"curve",@"x":@-50.0,@"y":@-340.0,@"cp1x":@-100
.0,@"cp1y":@-100.0,@"cp2x":@-110.0,@"cp2y":@-350.0},
@{@"command":@"curve",@"x":@500.0,@"y":@-140.5,@"cp1x":@400.
0,@"cp1y":@-240.0,@"cp2x":@600.0,@"cp2y":@-40.0}
];
!
上記パスをシナリオ配列に組み込む 20秒後に出現するキャラクタに動きを設定
@{@"name":@"maru",@"time":@20.0f,@"posX":@100.0,@"posY":@600
.0,@"motion":maruMotionArray},