问题
My xCode project has the following....
AppDelegate
GameViewController
OpeningScene
MainMenuScene
GameScene
When the game starts AppDelegate creates an array of all the textures I will use...
[self.texturesArray addObject:[SKTexture textureWithImageNamed:@"Background05.jpg"]];
There's about 120 textures.
Then I preload them using...
[SKTexture preloadTextures:self.texturesArray withCompletionHandler:^{
NSLog(@"************ Finished Preloading Textures in AppDelegate.m *************");
[[STGD sharedGD].currentScene presentMainMenuScene];
}];
I then present the OpeningScene which waits until the above completion handler tells it to present the main menu.
Here's the problem...
One animation of preloaded textures is used in the main menu (animateWithTextures). SOMETIMES some of the frames are missing and are presented as the white box with the red X that usually denotes a file that could not be found. If this happens then the following also always happens... switching to the game scene the same animation is used (although it is larger in size in GameScene) and the same frames will be missing and displaying the red X. Switching back and forth between a MainMenuScene and a GameScene (recreating each scene each time) and the same frames will be gone until I restart the app.
I know that the textures are being created correctly in code because most of the time it works fine. But about 1 launch in 15 results in some animation textures being lost. And every time it happens it's never the same frames, or even the same number of frames. Nor are they adjacent frames if that means anything :P
I don't believe this ever happened before I started using the preloading. But without the preloading my initial fps was garbage.
Appreciate any insights! Thanks!
UPDATE:
Here is how I populate the texture array in Star.m. The Star object is used in both MainMenuScene and GameScene
self.animationSlides = [NSMutableArray arrayWithCapacity:45];
for (int i = 0; i < 45; i++)
{
NSString *fileName;
if (i < 10)
fileName = [NSString stringWithFormat:@"Star03-03-0%i.png", i];
else
fileName = [NSString stringWithFormat:@"Star03-03-%i.png", i];
[self.animationSlides addObject:[SKTexture textureWithImageNamed:fileName]];
}
Then I create a sprite in Star.m ...
self.animationSprite = [SKSpriteNode spriteNodeWithImageNamed:@"Star03-03-00.png"];
Then animate it in Star.m...
[self.animationSprite runAction:[SKAction repeatActionForever:[SKAction animateWithTextures:self.animationSlides timePerFrame:0.0666]]];
I only test on device, no simulator. I've deleted the app from my device and performed Product -> Clean before reinstalling. I'll let you know if this fixes the issue.
Thanks!
来源:https://stackoverflow.com/questions/36657442/textures-used-for-animatewithtextures-getting-lost-some-frames-show-red-x