我开始使用SKSpritenode类别系统来扩展SKSpriteNode的基本功能,并且现在通过编辑器添加到.sks场景中的精灵(不是从场景中丢失)在运行时到达场景。当我在.sk上记录一些精灵时,我可以看到它们的正确位置和比例参数,但是Xcode的调试控制台中的纹理总是['nil']。发生这种情况时(请参见下面的NSlog行),例如,当我在gamescene1.sk上名为" house1"的日志元素时,命名为" house1" sknoe。在场景编辑器上,我可以看到所有元素(包括该house1 element)罚款,但是当我启动项目时,都没有显示出.sks文件中的精灵。这是相关代码:
GamesScene头文件是:
//GameScene.h
#import "Hero.h"
#import "SKSpriteNode+StaticLevelElement.h"
@interface GameScene : SKScene <SKPhysicsContactDelegate>
-(void) initStaticLevelElements;
@end
GamesScene实现文件是:
//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 staticlevlelement.h是:
// SKSpriteNode+StaticLevelElement.h
#import <SpriteKit/SpriteKit.h>
#import <SpriteKit/SKSpriteNode.h>
@interface SKSpriteNode (StaticLevelElement)
-(void) initWithSKNode:(SKNode *)node;
@property NSNumber * pseudoDepth;
@end
skspritenode staticlevlelement.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
为了设置每个场景(级别)的特定数据,我使用基本游戏类别的继承 - 因此,我获得了gamescene1,gameScene2等。有人知道我缺少什么也能看到纹理吗?
我从Skspritenode staticlevlelement.m现在一切正常,现在显示纹理,而精灵都显示在屏幕上。