2014-08-27 63 views
2

每当我想要转换到某个场景时,在SKTransition甚至开始之前需要几秒钟的时间。游戏开始之前是否可以初始化所有场景?如何初始化Sprite Kit中的所有场景?

NewScene *newScene = [NewScene sceneWithSize:self.size]; 
SKTransition *reveal = [SKTransition moveInWithDirection:SKTransitionDirectionUp duration:0.5]; 
reveal.pausesIncomingScene = NO; 
[self.view presentScene:newScene transition:reveal]; 

我已经didMoveToView试过这还有:

@implementation NewScene 
-(id)initWithSize:(CGSize)size { 
    if(self = [super initWithSize:size]) { 
    // Load a bunch of stuff like this: 
    SKSpriteNode *menuButton = [SKSpriteNode spriteNodeWithImageNamed:@"mainMenu"]; 
    menuButton.position = CGPointMake(self.frame.size.width/2,self.frame.size.height/4); 
    menuButton.name = @"menuButton"; 
    [self addChild:menuButton]; 
    [menuButton setScale:0.8]; 
    } 
} 

我怎样才能确保我的雪碧套件游戏运行流畅?

编辑: 事实证明,问题是我不断让主线睡觉淡出音乐。我已将所有音频方法在后台运行并且现在工作正常。

+1

你能告诉你用“加载一堆东西”的评论取代了什么?如果需要,您不应该在创建场景时遇到任何问题,而且如果是的话,您应该重新考虑处理数据/加载资源的方式。您可以从尽可能多的异步处理开始,并[预加载纹理](https://developer.apple.com/Library/ios/documentation/SpriteKit/Reference/SKTexture_Ref/Reference/Reference.html#//apple_ref/ OCC/CLM/SKTexture/preloadTextures:withCompletionHandler :)。 – 2014-08-27 11:52:55

+0

@ 0x7fffffff - 编辑我的文章。我尝试了预加载纹理(在Stack Overflow上使用了另一个答案),但结果没有改变。我可能在新场景中加载了错误的纹理。也许你可以告诉我如何在新场景中加载纹理? – Deeyennay 2014-08-27 12:10:36

回答

3

一个好方法是在SKScene的初始化之前异步加载所有资源。苹果公司使用在Adventure game这种方法:

NewScene.h:

typedef void (^AGAssetLoadCompletionHandler)(void); 

@interface GameScene : SKScene<SKPhysicsContactDelegate, UIGestureRecognizerDelegate> 

+ (void)loadSceneAssetsWithCompletionHandler:(AGAssetLoadCompletionHandler)handler; 

@end 

NewScene.m:

@implementation NewScene 

-(id)initWithSize:(CGSize)size { 
    if(self = [super initWithSize:size]) { 
    // Load a bunch of stuff like this: 
    SKSpriteNode *menuButton = [SKSpriteNode spriteNodeWithTexture:[self menuButtonTexture]; 
    menuButton.position = CGPointMake(self.frame.size.width/2,self.frame.size.height/4); 
    menuButton.name = @"menuButton"; 
    [self addChild:menuButton]; 
    [menuButton setScale:0.8]; 
    } 
} 

#pragma mark - Shared Assets 

+ (void)loadSceneAssetsWithCompletionHandler:(AGAssetLoadCompletionHandler)handler { 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 
     // Load the shared assets in the background. 
     [self loadSceneAssets]; 

     if (!handler) { 
      return; 
     } 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      // Call the completion handler back on the main queue. 
      handler(); 
     }); 
    }); 
} 

+ (void)loadSceneAssets { 
    sBackgroundTexture = [SKTexture textureWithImageNamed:@"background"]; 
    sMenuButtonTexture = [SKTexture textureWithImageNamed:@"mainMenu"]; 
    // etc. 
} 

static SKTexture *sBackgroundTexture = nil; 
- (SKTexture *)backgroundTexture { 
    return sBackgroundTexture; 
} 

static SKTexture *sMenuButtonTexture = nil; 
- (SKTexture *)menuButtonTexture { 
    return sMenuButtonTexture; 
} 

@end 

然后就目前NewSceneUIViewController

if (!self.skView.scene) { 
    CGSize viewSize = self.view.bounds.size; 
    // Here you can present some loading scene or splash screen 

    [NewScene loadSceneAssetsWithCompletionHandler:^{ 
     NewScene *scene = [[NewScene alloc] initWithSize:viewSize]; 
     [self.skView presentScene:scene transition:[SKTransition crossFadeWithDuration:1.f]]; 
    }]; 
} 
相关问题