所以,我有一个项目,屏幕底部有一门大炮(目前是一个活泼的球),上面有小光环。当它们产卵时,它们会移动到屏幕较高部分的随机位置。这个,我已经有了这个代码:
#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 秒后,它会再次射击。它会继续射击,直到它被击中为止(无需编写 halos 的代码)。我开始为此编写代码,但我只能得到每个光环同时发射的结果。
我希望每个光环的计时器在它本身出现在屏幕上时启动。我该怎么做?
谢谢,克里斯蒂安!所有的帮助都是。 . .有帮助。
最佳答案
您可以使用 SKAction block 。我在下面的示例中使用了 self 。您可以将 self 替换为您的节点。
SKAction *wait0 = [SKAction waitForDuration:2.0];
SKAction *block0 = [SKAction runBlock:^{
NSLog(@"shoot");
}];
[self runAction:[SKAction repeatActionForever:[SKAction sequence:@[block0, wait0]]]];
关于ios - SpriteKit Obj-C——我如何从一个节点拍摄到另一个节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29497413/