我正在尝试将SpriteKit场景的SKTexture的预加载UIViewController数组传递给初始化时的场景。但是,我似乎无法为SKScene自定义初始化方法以传递到数组中。这就是我想要做的:@interface MyViewController ()@property (nonatomic, strong) NSArray *texturePack;@end- (void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; // Configure the view. SKView * skView = (SKView *)self.spriteView; if (!skView.scene) { skView.showsFPS = YES; skView.showsNodeCount = YES; // Create and configure the scene. // Pass variable?* MyScene *scene = [MyScene initWithSize:skView.bounds.size]; scene.scaleMode = SKSceneScaleModeAspectFill; // Present the scene. [skView presentScene:scene]; }}* 我不确定如何在初始化之前将self.texturePack传递给场景? 如果有人对如何在初始化时将变量传递给SKScene提出任何建议,我将不胜感激。 最佳答案 您必须在SKScene的子类中公开声明该属性。@interface MyScene : SKScene@property (nonatomic, strong) NSArray *texturePack;@end然后,当您创建场景的实例时。为新声明的属性设置一个值。完成此操作后,您可以从场景实例中访问阵列。SKView * skView = (SKView *)self.spriteView;if (!skView.scene) { skView.showsFPS = YES; skView.showsNodeCount = YES; MyScene *scene = [MyScene initWithSize:skView.bounds.size]; [scene setTexturePack:someArrayReference]; scene.scaleMode = SKSceneScaleModeAspectFill; // Present the scene. [skView presentScene:scene];} 编辑:您是否正在寻找一种将数组作为参数的自定义初始化方法?如果是这样,请将其添加到场景子类中,并在场景的标头中对其进行 public 声明。- (instancetype)initWithSize:(CGSize)size andCustomParameter:(NSArray *)theArray { self = [super initWithSize:size]; if (self) { // do something with the array on iniitialization } return self;}关于ios - 将变量传递给SKScene,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21760060/ 10-12 14:32