具有符合 NSCoding 的 CCSprite 的自定义对象不显示



为了保存 ScrollingBackground 对象,我已将 CCSprites 子类化以符合 NSCoding。滚动背景不显示。请参阅下面的相关代码。我不太确定出了什么问题。请帮忙。

ScrollingBackground.h:(CCBackground精灵的界面)

@interface CCBackgroundSprite: NSObject <NSCoding>
@property (nonatomic, assign) float xValue;
@property (nonatomic, assign) float yValue;
@property (nonatomic, retain) NSString* backgroundStringName;
@end

滚动背景:(CCBackgroundSprite的实现)

@implementation CCBackgroundSprite
-(id)init
{
    if((self = [super init])){
    }
   return self;
}
-(id) initWithCoder:(NSCoder *) aDecoder {
self = [super init];
if(self != nil) {
    self.xValue = [aDecoder decodeFloatForKey:@"xValue"];
    self.yValue = [aDecoder decodeFloatForKey:@"yValue"];
    self.backgroundStringName = [aDecoder decodeObjectForKey:@"backgroundStringName"];
  }
  return self;
}
-(void) encodeWithCoder:(NSCoder *)aCoder {
   [aCoder encodeFloat:self.xValue forKey:@"xValue"];
   [aCoder encodeFloat:self.yValue forKey:@"yValue"];
   [aCoder encodeObject:self.backgroundStringName forKey:@"backgroundStringName"];
}
@end

为 CCSprite 属性设置 CCBackgroundSprite 的实例:

-(void)spriteProperties {
   background1 = [[CCBackgroundSprite alloc] init];
   [background1 setXValue:bg.position.x];
   [background1 setYValue:bg.position.y];
   [background1 setBackgroundStringName:@"bg"];
   background2 = [[CCBackgroundSprite alloc] init];
   [background2 setXValue:bgSwap.position.x];
   [background2 setYValue:bgSwap.position.y];
   [background2 setBackgroundStringName:@"bgSwap"];
   background3 = [[CCBackgroundSprite alloc] init];
   [background3 setXValue:bgSwap2.position.x];
   [background3 setYValue:bgSwap2.position.y];
   [background3 setBackgroundStringName:@"bgSwap2"];
}

编码/解码滚动背景的其他非精灵相关属性:

-(void) encodeWithCoder:(NSCoder *)aCoder {
   [aCoder encodeInt:self.backgroundCount forKey:@"backgroundCount"];
   [aCoder encodeInt:self.backgroundRepeatCount forKey:@"backgroundRepeatCount"];
   [aCoder encodeFloat:self.scrollSpeed forKey:@"scrollSpeed"];
   [aCoder encodeObject:self.backgroundArray forKey:@"backgroundArray"];
   [aCoder encodeObject:self.changeArray forKey:@"changeArray"];
              .
              .
              .
} 
-(id) initWithCoder:(NSCoder *) aDecoder {
self = [super init];
if(self != nil) {
        self.backgroundCount = [aDecoder decodeIntForKey:@"backgroundCount"];
        self.backgroundRepeatCount = [aDecoder decodeIntForKey:@"backgroundRepeatCount"];
        self.scrollSpeed = [aDecoder decodeFloatForKey:@"scrollSpeed"];
        self.backgroundArray = [aDecoder decodeObjectForKey:@"backgroundArray"];
        self.changeArray = [aDecoder decodeObjectForKey:@"changeArray"];
           .
           .
           .
     }
 }

保存和加载滚动背景对象:

- (void)saveBackgroundObject:(ScrollingBackground *)object key:(NSString *)key {
   [self spriteProperties];
   NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
   NSString *dataToString = [NSString stringWithFormat:@"%@", encodedObject];
   CCLOG(@"encodedObject = %@ n", dataToString);
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   [defaults setObject:encodedObject forKey:key];
   [defaults synchronize];
}
-(ScrollingBackground *)loadBackgroundWithKey:(NSString *)key {
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   NSData *encodedObject = [defaults objectForKey:key];
   NSString *dataToString = [NSString stringWithFormat:@"%@", encodedObject];
   CCLOG(@"encodedObject = %@ n", dataToString);
   ScrollingBackground *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
   return object;
 }

更新:

我对精灵属性方法进行了以下更改:

-(void)spriteProperties {
  background1 = [[CCBackgroundSprite alloc] init];
  [background1 setXValue:bg.position.x];
  [background1 setYValue:bg.position.y];
  [background1 setBackgroundImageName:bg.displayFrame.textureFilename];
  [self addChild:background1];
  background2 = [[CCBackgroundSprite alloc] init];
  [background2 setXValue:bgSwap.position.x];
  [background2 setYValue:bgSwap.position.y];
  [background2 setBackgroundImageName:bgSwap.displayFrame.textureFilename];
  [self addChild:background2];
  background3 = [[CCBackgroundSprite alloc] init];
  [background3 setXValue:bgSwap2.position.x];
  [background3 setYValue:bgSwap2.position.y];
  [background3 setBackgroundImageName:bgSwap2.displayFrame.textureFilename];
  [self addChild:background3];
  }

我使用上述displayFrame.textureFilename的主要原因是因为我在此过程中重复使用了精灵。另外,为了设置我所做的背景图像:

-(void)startingSprites  //change later to setupInitialBackground
{
    CGSize s = [[CCDirector sharedDirector] winSize];
    bg = [CCSprite spriteWithSpriteFrameName:@"bgImage1.png"];
    bg.position = ccp(s.width/2, s.height/2);
    [currentBackgroundBatchNode addChild:bg];

    swapbg = [CCSprite spriteWithSpriteFrameName:@"bgImage2.png"];
    swapbg.position = ccp(s.width/2, 3*s.height/2 -1.0);
    [currentBackgroundBatchNode addChild: swapbg];
    swapbg2 = [CCSprite spriteWithSpriteFrameName:@"bgImage3.png"];
    swapbg2.position = ccp(s.width/2, 5*s.height/2 - 2.0);
    [currentBackgroundBatchNode addChild: swapbg2];
    CCLOG(@"bg background is %@", bg.displayFrame.textureFilename);
    CCLOG(@"bgSwap background is %@", swapbg.displayFrame.textureFilename);
    CCLOG(@"bgSwap2 background is %@", swapbg2.displayFrame.textureFilename);
}

我刚刚意识到几件事:

  1. startingSprites中的 CCLOG 为空
  2. 我在此过程中重复使用currentBackgroundBatchNode(这是一个CCSpriteBatchNode),这意味着我必须对其进行编码/解码。如何对其进行子类化以及具有哪些属性?不太确定它将如何解决。

我已经阅读了您的一些帖子,也与此有关。我建议不要尝试子类化几个 cocos2d 类以符合 NSCoding,而应该使用更简单的解决方法。我相信你的背景有自己的层,所以你为什么不保存各种背景参数,并为你的背景创建另一个初始化来处理重新加载后台状态的情况。

你说你已经子类化了CCSprite,但你实际上NSObject子类了。尝试:

@interface CCBackgroundSprite: CCSprite <NSCoding>
...

最新更新