gpt4 book ai didi

ios - 在 Sprite Kit 中如何在开始时初始化所有场景?

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:04:01 25 4
gpt4 key购买 nike

每当我想转换到某个场景时,SKTransition 甚至需要几秒钟才能开始。是否可以在游戏开始前将所有场景都初始化?

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];
}
}

如何确保我的 Sprite Kit 游戏运行流畅?

编辑:事实证明,问题是我一直让主线程休眠以淡出音乐。我已经让我所有的音频方法在后台运行,现在工作正常。

最佳答案

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

新场景.h:

typedef void (^AGAssetLoadCompletionHandler)(void);

@interface GameScene : SKScene<SKPhysicsContactDelegate, UIGestureRecognizerDelegate>

+ (void)loadSceneAssetsWithCompletionHandler:(AGAssetLoadCompletionHandler)handler;

@end

新场景.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

然后从您的 UIViewController 中呈现 NewScene:

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]];
}];
}

关于ios - 在 Sprite Kit 中如何在开始时初始化所有场景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25526322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com