Skip to content

Instantly share code, notes, and snippets.

@akio0911
Created May 8, 2014 08:17
Show Gist options
  • Save akio0911/63d393a39949b6288aec to your computer and use it in GitHub Desktop.
Save akio0911/63d393a39949b6288aec to your computer and use it in GitHub Desktop.
- (void)addMole
{
NSLog(@"%s", __func__);
// 今動いていないもぐらのインデックスの配列を作る
NSMutableArray *targetMolesIndex = [NSMutableArray array];
for(int i = 0; i < moles_.count; i++){
NSObject *object = moles_[i];
if(object == [NSNull null]){
[targetMolesIndex addObject:@(i)];
}
}
NSLog(@"targetMolesIndex.count = %d", targetMolesIndex.count);
// すべてのもぐらが動いていたら、何もしない
if(targetMolesIndex.count == 0){
return;
}
// もぐらを出す穴のインデックスを決定
NSNumber *index = targetMolesIndex[arc4random() % targetMolesIndex.count];
// もぐらのスプライトを作る
SKSpriteNode *mole = [SKSpriteNode spriteNodeWithImageNamed:@"Mole"];
mole.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:mole.size.width / 2];
mole.physicsBody.affectedByGravity = NO;
mole.name = kMoleName;
// 配列に保存しておく
moles_[index.integerValue] = mole;
int x = index.integerValue % 3;
int y = index.integerValue / 3;
// SKSpriteNode *ground1 = [self childNodeWithName:kGround1Name];
SKSpriteNode *ground2 = (SKSpriteNode*)[self childNodeWithName:kGround2Name];
SKSpriteNode *ground3 = (SKSpriteNode*)[self childNodeWithName:kGround3Name];
SKSpriteNode *ground4 = (SKSpriteNode*)[self childNodeWithName:kGround4Name];
CGFloat positionX = ground2.position.x + ground2.size.width/4*(-1+x);
CGFloat positionY = 0;
NSLog(@"%f %f", positionX, positionY);
switch (y) {
case 0:
positionY = ground2.position.y;
break;
case 1:
positionY = ground3.position.y;
break;
case 2:
positionY = ground4.position.y;
break;
default:
break;
}
mole.position = CGPointMake(positionX,
positionY);
if(index.integerValue < 3){
mole.zPosition = 0.15;
}
else if(index.integerValue < 6){
mole.zPosition = 0.25;
}
else if(index.integerValue < 9){
mole.zPosition = 0.35;
}
[self addChild:mole];
// もぐらを動かす
SKAction *moveUp = [SKAction moveBy:CGVectorMake(0, mole.size.height*0.9)
duration:0.5];
SKAction *wait = [SKAction waitForDuration:skRand(0.5, 2.0)];
SKAction *moveDown = [SKAction moveBy:CGVectorMake(0, -mole.size.height*0.9)
duration:0.5];
SKAction *block = [SKAction runBlock:^{
[mole removeFromParent];
moles_[index.integerValue] = [NSNull null];
}];
SKAction *seq = [SKAction sequence:@[moveUp, wait, moveDown, block]];
[mole runAction:seq];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment