Spritekit how to run a method for a certain amount of time, then start another one

半世苍凉 提交于 2019-12-11 18:38:19

问题


I'm making a game and i'm currently stumped on how to have a method run for a certain amount of time, turn off, then to have another method start running. Currently i have:

[self spawn];

when the scene sets up. Here is the spawn method:

-(void)spawn {
int xMin = 0;
int xMax = 460;
CGPoint startPoint = CGPointMake(xMin + arc4random_uniform(xMax - xMin),320);
[self performSelector:@selector(spawn) withObject:nil afterDelay:.20];
SKSpriteNode *blue = [SKSpriteNode spriteNodeWithImageNamed:@"whitecircle"];
blue.position = CGPointMake(startPoint.x,startPoint.y);
blue.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:21.5];
blue.physicsBody.usesPreciseCollisionDetection = NO;
blue.physicsBody.categoryBitMask = gainCategory;
blue.physicsBody.contactTestBitMask = playerCategory;
blue.physicsBody.dynamic = NO;
[self addChild:blue];
SKAction *runBlue = [SKAction moveToY:0 duration:1.5];
SKAction *remove = [SKAction removeFromParent];
[blue runAction:[SKAction sequence:@[runBlue,remove]]];
}

What i want to know is how can i make this method run for a certain amount of time, then how can i start another method after this one finishes. Thanks


回答1:


I think this is what you are looking to do :

// set duration to seconds you want to wait between spawns
float duration = 1; 

SKAction *actionWait = [SKAction waitForDuration:duration];
SKAction *actionSpawn = [SKAction runBlock:^(void)
{
     [self spawn];
}];

SKAction *actionSequence = [SKAction sequence:@[actionWait, actionSpawn]];
SKAction *actionRepeat = [SKAction repeatActionForever:actionSequence];

[self runAction:actionRepeat];

It's worthwhile to give the Class Reference for SKAction a good reading at least once, so you know it's capabilities.

SKAction Class Reference



来源:https://stackoverflow.com/questions/24975288/spritekit-how-to-run-a-method-for-a-certain-amount-of-time-then-start-another-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!