我开始使用SKSpriteNode类别系统来扩展SKSpriteNode的基本功能,并且现在通过编辑器添加到.sks场景的精灵从场景中丢失(不是在运行时以编程方式添加到场景中的精灵) 。当我在.sks上记录一些精灵时,我可以看到它们的正确位置和缩放参数,但在XCode的调试控制台中纹理始终是[' nil']。当我为例如名为" house1"的log元素时,会发生这种情况(参见下面的NSLog行)。内部静态SKNoe在GameScene1.sks的场景编辑器上。在场景编辑器上,我可以看到所有元素(包括那个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类的继承 - 因此我获得了GameScene1,GameScene2等。 有谁知道我错过了什么才能看到纹理?
答案 0 :(得分:0)
我从中删除了initWithCoder覆盖方法 SKSpriteNode + StaticLevelElement.m 现在一切正常,纹理显示,精灵全部显示在屏幕上。