当SKSpriteNode与类别一起使用时,纹理未find,精灵不显示

我开始使用SKSpriteNode类别系统来扩展SKSpriteNode的基本function,并且通过编辑器添加到.sks场景中的精灵现在从场景中丢失(而不是在运行时以编程方式添加到场景中)。 当我在.sks上logging一些精灵时,我可以看到它们的正确位置和缩放参数,但是在XCode的debugging控制台中纹理总是['nil']。 发生这种情况(见下面的NSLog行)当我例如在GameScene1.sks的场景编辑器中的静态SKNoe内的名为“house1”的日志元素。 在场景编辑器上,我可以看到所有的元素(包括house1元素),但是当我启动项目时,没有添加到该.sks文件的精灵。 这是相关的代码:

GameScene头文件是:

//GameScene.h #import "Hero.h" #import "SKSpriteNode+StaticLevelElement.h" @interface GameScene : SKScene <SKPhysicsContactDelegate> -(void) initStaticLevelElements; @end 

GameScene的实现文件是:

 //GameScene.m #import "SKSpriteNode+StaticLevelElement.h" @implementation SKScene (Unarchive) + (instancetype)unarchiveFromFile:(NSString *)file { /* Retrieve scene file path from the application bundle */ NSString *nodePath = [[NSBundle mainBundle] pathForResource:file ofType:@"sks"]; /* Unarchive the file to an SKScene object */ NSData *data = [NSData dataWithContentsOfFile:nodePath options:NSDataReadingMappedIfSafe error:nil]; NSKeyedUnarchiver *arch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; [arch setClass:self forClassName:@"SKScene"]; SKScene *scene = [arch decodeObjectForKey:NSKeyedArchiveRootObjectKey]; [arch finishDecoding]; return scene; } @end @implementation GameScene -(void)didMoveToView:(SKView *)view { //Overriden in each level scene [self initStaticLevelElements]; } -(void) initStaticLevelElements { } @end 

GameScene1.m是:

 // GameScene1.m #import "GameScene1.h" #import "Customer.h" #import "Competitor.h" @implementation SKScene (Unarchive) + (instancetype)unarchiveFromFile:(NSString *)file { NSString *nodePath = [[NSBundle mainBundle] pathForResource:file ofType:@"sks"]; NSData *data = [NSData dataWithContentsOfFile:nodePath options:NSDataReadingMappedIfSafe error:nil]; NSKeyedUnarchiver *arch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; [arch setClass:self forClassName:@"SKScene"]; SKScene *scene = [arch decodeObjectForKey:NSKeyedArchiveRootObjectKey]; [arch finishDecoding]; return scene; } @end @implementation GameScene1 #include "GoodsConstants.h" -(void) initStaticLevelElements { self.staticLevelElements = [[NSMutableArray alloc] init]; self.name = @"name for scene one"; SKNode * statics = [self childNodeWithName:@"statics"]; statics.zPosition = 100; SKSpriteNode * e = (SKSpriteNode *)[statics childNodeWithName:@"house1"]; NSLog(@"house one is: %@", e); } 

GameScene1.h是:

 // GameScene1.h #import <SpriteKit/SpriteKit.h> #import "GameScene.h" @interface GameScene1 : GameScene @end 

SKSpriteNode + StaticLevelElement.h是:

 // SKSpriteNode+StaticLevelElement.h #import <SpriteKit/SpriteKit.h> #import <SpriteKit/SKSpriteNode.h> @interface SKSpriteNode (StaticLevelElement) -(void) initWithSKNode:(SKNode *)node; @property NSNumber * pseudoDepth; @end 

SKSpriteNode + StaticLevelElement.m是:

 //SKSpriteNode+StaticLevelElement.m #import <objc/runtime.h> #import "SKSpriteNode+StaticLevelElement.h" NSString * const pseudoDepthSelector = @"pseudoDepth"; @implementation SKSpriteNode (StaticLevelElement) @dynamic pseudoDepth; - (instancetype)initWithCoder:(NSCoder *)coder { self = [super initWithCoder:coder]; self.userInteractionEnabled = YES; return self; } - (void)setPseudoDepth:(NSNumber *)pseudoDepth { objc_setAssociatedObject(self, (__bridge const void *)(pseudoDepthSelector), pseudoDepth, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (NSNumber *)pseudoDepth { return objc_getAssociatedObject(self, (__bridge const void *)(pseudoDepthSelector)); } -(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { } -(void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { } @end 

为了设置每个场景(等级)的具体数据,我使用了基础GameScene类的inheritance – 因此我得到了GameScene1,GameScene2等。有没有人知道我错过了什么以便能够看到纹理?

我从SKSpriteNode + StaticLevelElement.m中删除了initWithCoder覆盖方法,现在一切正常,纹理显示,精灵都显示在屏幕上。