2015-01-20 75 views
2

我在我的項目中有一個.png動畫,700張圖片,大小爲150 px x 150 px。.png動畫spritekit性能下降

它工作正常,但每次動畫開始,整個遊戲凍結約0.1秒。像它的加載,但我在initWithSize中實現了.png數組。像這樣:

SKTextureAtlas *barrierUfoAtlas = [SKTextureAtlas atlasNamed:@"BarrierUfoAnimation"]; 
NSArray *barrierUfoAtlasNames = [barrierUfoAtlas textureNames]; 
NSArray *sortetBarrierUfoAtlasNames = [barrierUfoAtlasNames sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 
NSMutableArray *barrierUfoTextures = [NSMutableArray array]; 

for (NSString *filename in sortetBarrierUfoAtlasNames) { 
    SKTexture *texture = [barrierUfoAtlas textureNamed:filename]; 
    [barrierUfoTextures addObject:texture]; 
} 
self.barrierUfoAnimation = [SKAction animateWithTextures:barrierUfoTextures timePerFrame:0.024]; 

然後邊玩邊約1-2分鐘。動畫開始了。 現在不需要加載任何東西,只需啓動動畫即可。 有什麼方法可以改進它?

+1

而被加載所有的遊戲資源可以有一個「加載」消息,並開始你的遊戲玩一次。 – sangony 2015-01-20 23:00:19

+0

好的謝謝,但是當我實現與.png文件的數組是不是已經加載?你能更具體地瞭解你的想法嗎?如何安排? – NeoGER89 2015-01-21 00:37:50

+1

您是否知道150x150乘以700大約等於60 mb的內存使用量?根據其他內存使用情況和設備的不同,您可能無法同時將所有內容都納入內存。 – LearnCocos2D 2015-01-21 00:48:46

回答

4

這是很多方面預加載之一:

@implementation GameScene { 
SKTextureAtlas *myAtlas1; 
BOOL loadingComplete; 
} 

-(id)initWithSize:(CGSize)size { 
    if (self = [super initWithSize:size]) { 

    // the usual stuff... 

    loadingComplete = false; 
    [self loadMyAtlas1]; 
    } 

    return self; 
} 

-(void)loadMyAtlas1 { 
    myAtlas1 = [SKTextureAtlas atlasNamed:@"MyAtlasName"]; 
    [SKTextureAtlas preloadTextureAtlases:[NSArray arrayWithObject:myAtlas1] withCompletionHandler:^{ 
    [self finishedLoading]; 
    }]; 
} 

-(void)finishedLoading { 
    // other stuff you might do here 
    loadingComplete = true; 
} 

-(void)update:(CFTimeInterval)currentTime { 
    if(loadingComplete) { 
     // run game code 
    } else { 
     // wait for the water to boil 
    } 
} 
+0

謝謝你,這對我很有用。不知道這種預加載方法。 – NeoGER89 2015-01-21 02:14:30

+1

預加載是答案。即使我在場景開始時填充數組,動畫在第一時間仍然滯後。預加載解決這個問題。謝謝。 – mkeremkeskin 2015-11-03 14:42:02

+0

感謝作品完美! – StackBuddy 2016-10-25 20:59:48