Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spritekit animation load time

Tags:

ios

xcode5

I have a simple game that I've been working on, a side scrolling space adventure where you save cats. A very simple game, but I'm running across an issue with. My game starts up normally, showing the main menu scene. Hit play, and it transititons to the game scene without issue, however, I'm finding that the first time I press start to begin the game, I get a bit of loading lag. This lag only appears the very first time you load the game.

Upon hitting start, the following method is called:

-(void)nonplayerSprites:(StartReason)endReason{
if (endReason == kStartGame){
//    [self playMusic:@"Electrodoodle.mp3"];
[self spawnPlayer];
[self setupUI];

[self runAction:[SKAction repeatActionForever: [SKAction sequence:@[[SKAction          
performSelector:@selector(enemySpawn) onTarget:self],
[SKAction waitForDuration:2.0]]]]];

[self runAction:[SKAction repeatActionForever: [SKAction sequence:@[[SKAction   
performSelector:@selector(spawnCat) onTarget:self],
[SKAction waitForDuration:1.5]]]]];

[self runAction:[SKAction repeatActionForever: [SKAction sequence:@[[SKAction 
performSelector:@selector(powerUpSpawnCheck) onTarget:self],[SKAction 
waitForDuration:2.0]]]]];


} else if (endReason == kEndGame){
    [self removeAllActions];
} else if (endReason == kRestartGame){
    MainGame * myScene =
    [[MainGame alloc] initWithSize:self.size];

    SKTransition *reveal =
    [SKTransition fadeWithDuration:0.5];

    [self.view presentScene: myScene transition: reveal];
}

}

I have four sprite nodes that it calls, a UFO, a cat, the player rocket and a randomized power up. Two SKLabelNodes for player score and life total are called via the spawnUI method, and Additionally, music will play. Although I currently have it disabled.

The enemy and friendly nodes are all called using a similar method:

-(void)enemySpawn

{

int enemyChoice = [self getRandomNumberBetween:0 to:1];



if (enemyChoice == 0){

    _enemy = [SKSpriteNode spriteNodeWithImageNamed:@"enemy"];

    [_enemy setScale:1.0];

}else if (enemyChoice == 1){

    _enemy = [SKSpriteNode spriteNodeWithImageNamed:@"enemy_alt"];

    [_enemy setScale:0.50];

}

_enemy.anchorPoint = CGPointZero;

_enemy.zPosition = 2.0;

_enemy.name = @"rocket";

_enemy.position = CGPointMake(self.size.width + _enemy.size.width, [self 
getRandomNumberBetween:0 to:250]);



SKAction *enemyMove = [SKAction sequence:@[[SKAction moveToX:-40 duration:[self 
getRandomNumberBetween:2 to: 5]],

                                            [SKAction removeFromParent]]];

[_worldNode addChild:_enemy];

[_enemy runAction:enemyMove];
}

I feel like I know what the issue is, the game data is being loaded into ram for the first time, but how do I avoid the pause while this loads? I wonder if I'm doing something wrong, because it doesn't seem like I'm loading enough to cause a noticable 1 to 3 second pause upon hitting the start button. The second time through, say after the player dies and restarts, the pause is gone and everything plays out smoothly. I apologize if I'm leaving out any important information, I'm pretty new at SpriteKit.

Anyone have some thoughts?

Thanks in advance.

like image 434
skamedly Avatar asked Apr 21 '26 06:04

skamedly


1 Answers

I had the same problem.

It seems that when you use [SKSpriteNode spriteNodeWithImageNamed:@"enemy"] or *[SKSpriteNode spriteNodeWithImageNamed:@"enemy_alt"]* or any other texture loading method, texture is not stored in buffers for quick access so first call of those methods is giving you lags.

The solution is next :

Add properties for your game objects textures

@property(nonatomic,strong) SKTexture *enemyTexture; . . .

add method to your scene subclass:

-(void)preloadTextures
{
    self.enemyTexture = [SKTexture textureWithImageNamed:@"enemy"];
    .
    .
    .
    .

    [SKTexture preloadTextures:@[self.enemyTexture,...] withCompletionHandler:^{
        NSLog(@"Textures preloaded");
    }];
}

Now call that method before presenting

MainGame * myScene =
[[MainGame alloc] initWithSize:self.size];

SKTransition *reveal =
[SKTransition fadeWithDuration:0.5];

[myScene preloadTextures];
[self.view presentScene: myScene transition: reveal];

Hope it will do the trick for you too.

like image 58
haawa Avatar answered Apr 22 '26 18:04

haawa