2016-06-22 5 views
0

SKSpriteNode 카테고리 시스템을 사용하여 SKSpriteNode의 기본 기능을 확장하기 시작했습니다. 이제 편집기를 통해 .sks 장면에 추가 된 스프라이트가 장면 (런타임에 프로그래밍 방식으로 추가 된 장면이 아닌). .sks에 스프라이트 중 일부를 로깅 할 때 정확한 위치와 스케일 매개 변수를 볼 수 있지만 텍스처는 XCode의 디버깅 콘솔에서 항상 [ 'nil']입니다. GameScene1.sks의 장면 편집기에서 SKNoe 통계의 내부에 "house1"이라는 이름의 로그 요소가있을 때 이런 일이 발생합니다 (NSLog 행 아래 참조). 장면 편집기에서 house1 요소를 포함한 모든 요소를 ​​볼 수 있지만 프로젝트를 시작할 때 .sks 파일에 추가 된 스프라이트는 표시되지 않습니다.카테고리가있는 SKSpriteNode를 사용할 때 스프라이트에 텍스처가 표시되지 않습니다.

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 

각 장면 (레벨)에 대한 구체적인 설정 데이터, I베이스 GameScene 상속을 사용 클래스 - 따라서 GameScene1, GameScene2 등을 얻을 수 있습니다. 텍스처를 볼 수 있도록 내가 누락 된 부분을 아는 사람이 있습니까?

답변

0

SKWLiteNode + StaticLevelElement.m 에서 initWithCoder 재정의 메서드를 삭제했는데 이제는 모든 것이 잘 작동하고 텍스처가 표시되며 스프라이트가 모두 화면에 표시됩니다.