jeudi 23 juin 2016

Texture for sprite not found and sprite not displayed when using unarchiveFromFile


I started using unarchiveFromFile for SKScene building, and 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 "background" on scene editor for GameScene1.sks. On Scene Editor I can see all elements (including background element) fine, but when I start it, none of the sprites added to that .sks file are displayed. Here is the relevant code:

//  GameViewController.m
#import "GameViewController.h"
#import "GamePlayLogic.h"
#import "GameScene1.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 GameViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;
    /* Sprite Kit applies additional optimizations to improve rendering performance */
    skView.ignoresSiblingOrder = YES;

    // Create and configure the scene.
    GameScene1 *scene = [GameScene1 unarchiveFromFile:@"GameScene1"];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    // Present the scene.
    [skView presentScene:scene];
}

GameScene is:

//  GameScene.m

@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 {
    [self setBackground];

    //Overriden in each level scene
    [self initStaticLevelElements];
    [self addPlayer];
    [self initCompetitors];
    [self initLevelDataAndInterface];
    [self startNewGame];
}

-(void) setBackground {
    SKNode * bck = [self childNodeWithName:@"background"];
    [bck setZPosition: 100];
    NSLog(@"background is: %@", bck);
}

GameScene1.m is:

//  GameScene1.m
#import "GameScene1.h"
#import "SKSpriteNode+StaticLevelElement.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"

//Basic initialization, goals, inventory, etc.
-(id) initWithCoder:(NSCoder *)aDecoder {

    self.levelName = @"Level 1";
    self = [super initWithCoder:aDecoder];
    return self;
}

-(void) initStaticLevelElements {
    self.staticLevelElements = [[NSMutableArray alloc] init];

    self.name = @"name for scene one";

    SKNode * statics = [self childNodeWithName:@"statics"];

    statics.zPosition = 100;
    NSLog(@"statics' parent: %@", statics.parent );
}

GameScene1.h is:

//  GameScene1.h

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


@interface GameScene1 : GameScene

@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?


Aucun commentaire:

Enregistrer un commentaire