SpriteKit Obj-C - 我如何从一个节点射到另一个节点?

时间:2015-04-07 17:22:11

标签: ios objective-c timer sprite-kit

所以,我有一个项目,在屏幕的底部有一个加农炮(目前是一个快速的球),以及从上面产生的小晕。当它们产卵时,它们会移动到屏幕较高部分的随机位置。这个,我已经有了这段代码:

#import "GameScene.h"


@implementation GameScene
{
    CGPoint _centerOfScreen;

    SKEmitterNode *zappy;
    SKEmitterNode *behind;
}


-(void)didMoveToView:(SKView *)view {
    // Scene.
    printf("Welcome to the GameScene!\n");
    self.backgroundColor = [SKColor blackColor];
    self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);

    // Set Initial Values.
    _centerOfScreen = CGPointMake(self.size.width * 0.5, self.size.height * 0.5);


    // Zappy Ball.
    NSString *zapFile = [[NSBundle mainBundle] pathForResource:@"Zappy" ofType:@"sks"];
    zappy = [NSKeyedUnarchiver unarchiveObjectWithFile:zapFile];
    zappy.name = @"zappy";
    zappy.position = CGPointMake(_centerOfScreen.x, 0.0);
    [self addChild:zappy];
    NSString *behindFile = [[NSBundle mainBundle] pathForResource:@"Behind" ofType:@"sks"];
    behind = [NSKeyedUnarchiver unarchiveObjectWithFile:behindFile];
    behind.position = zappy.position;
    [self addChild:behind];


    // Call Functions.
    SKAction *spawnHalos = [SKAction sequence:@[[SKAction waitForDuration:0.8],
                                                [SKAction performSelector:@selector(spawnHalos) onTarget:self]]];
    [self runAction:[SKAction repeatActionForever:spawnHalos] withKey:@"SpawnHalos"];


}


-(void)rotateNode:(SKNode *)facingNode toFaceNode:(SKNode *)facedNode {

    double angle = atan2(facedNode.position.y - facingNode.position.y, facedNode.position.x - facingNode.position.x);

    if (facingNode.zRotation < 0) {
        facingNode.zRotation = facingNode.zRotation + M_PI * 2;
    }

    [facingNode runAction:[SKAction rotateToAngle:angle duration:0]];
}


-(void)spawnHalos {
    printf("Spawn\n");

    SKSpriteNode *halo = [SKSpriteNode spriteNodeWithImageNamed:@"RedHalo"];
    halo.size = CGSizeMake(halo.size.width * 0.8, halo.size.height * 0.8);
    halo.name = @"halo";
    halo.position = CGPointMake((arc4random_uniform(self.size.width * 2.0)),
                                (arc4random_uniform(self.size.height) + self.size.height));
    [self addChild:halo];
    [halo runAction:[SKAction moveTo:CGPointMake((arc4random_uniform((self.size.width * 0.7) + (self.size.width * 0.25))),
                                                 ((arc4random_uniform(self.size.height * 0.20) + (self.size.height * 0.8))))
                            duration:1.0]];

    [self rotateNode:halo toFaceNode:zappy];


    // Lessen the time between spawnings.
    SKAction *spawnHaloAction = [self actionForKey:@"SpawnHalo"];
    if (spawnHaloAction.speed < 2.3) {
        spawnHaloAction.speed += 0.01;
    }

}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        SKNode *touchedNode = [self nodeAtPoint:location];

        if ([touchedNode.name  isEqual: @"halo"]) {
            // Touched a halo –– DESTROY IT!!
            printf("Remove\n");
            [touchedNode removeFromParent];
        }


    }

}


-(void)update:(NSTimeInterval)currentTime {
    /* Called before each frame is rendered */

    [self enumerateChildNodesWithName:@"halo" usingBlock:^(SKNode *node, BOOL *stop) {
        [self rotateNode:node toFaceNode:zappy];

        // Shrink the size of the halos, gradually...
        //[node runAction:[SKAction scaleXBy:0.99 y:0.99 duration:0.0]];
    }];

}


@end

所以,我已经明白了。我的问题是,我需要让光环出现在屏幕上,等待大约2秒钟,然后开始在屏幕底部的大炮处拍摄。大约2秒后,它再次射击。它继续拍摄直到它的拍摄(没有拍摄光晕的必要代码)。我开始研究它的代码,但我只能得到它同时每个光环射击。

我希望每个光环的计时器在它本身出现在屏幕上时启动。我该怎么做呢?

谢谢,克里斯蒂安!所有帮助都是。 。 。有帮助的。

1 个答案:

答案 0 :(得分:1)

您可以使用SKAction块。我在下面的示例中使用了self。您可以将self替换为您的节点。

SKAction *wait0 = [SKAction waitForDuration:2.0];
SKAction *block0 = [SKAction runBlock:^{
    NSLog(@"shoot");
}];
[self runAction:[SKAction repeatActionForever:[SKAction sequence:@[block0, wait0]]]];