Texture for sprite not found and sprite not displayed when using SKSpriteNode with a category

旧城冷巷雨未停 提交于 2019-12-08 11:26:10

问题


I started using SKSpriteNode category system for extending the base functionality of the SKSpriteNode, and the sprites added to the .sks scene through the editor are now missing from the scene (not the ones programmatically added to the scene additionaly, in runtime). When I logged some of the sprites on the .sks, I can see their correct location, and scale parameters, but texture is always ['nil'] in the debugging console in XCode. This happens (see below NSLog line) when I for instance log element that is named "house1" inside statics SKNoe on scene editor for GameScene1.sks. On Scene Editor I can see all the elements (including that house1 element) fine, but when I start the project, none of the sprites added to that .sks file are displayed. Here is the relevant code:

GameScene header file is:

//GameScene.h

#import "Hero.h"
#import "SKSpriteNode+StaticLevelElement.h"

@interface GameScene : SKScene <SKPhysicsContactDelegate>

-(void) initStaticLevelElements;

@end

GameScene implementation file is:

//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 is:

//  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 is:

//  GameScene1.h

#import <SpriteKit/SpriteKit.h>
#import "GameScene.h"


@interface GameScene1 : GameScene

@end

SKSpriteNode+StaticLevelElement.h is:

//  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 is:

//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

For setting the specific data for each scene(level), I use inheritance of the base GameScene class - thus I get GameScene1, GameScene2 etc. Does anyone know what am I missing in order to be able to see the textures as well?


回答1:


I deleted the initWithCoder override method from SKSpriteNode+StaticLevelElement.m and everything works fine now, the textures are displayed and the sprites all display on the screen.



来源:https://stackoverflow.com/questions/37974247/texture-for-sprite-not-found-and-sprite-not-displayed-when-using-skspritenode-wi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!