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

时间:2013-11-10 23:47:20

标签: cocos2d-iphone nsuserdefaults nscoding

在我保存ScrollingBackground对象的过程中,我将CCSprites子类化为符合NSCoding。 ScrollingBackground不显示。请参阅下面的相关代码。我不确定什么是错的。请帮忙。

ScrollingBackground.h: (CCBackgroundSprite的界面)

@interface CCBackgroundSprite: NSObject <NSCoding>

@property (nonatomic, assign) float xValue;
@property (nonatomic, assign) float yValue;
@property (nonatomic, retain) NSString* backgroundStringName;

@end

ScrollingBackground.m: (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"];
}

ScrollingBackground的其他非Sprite相关属性的编码/解码:

-(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"];
           .
           .
           .
     }
 }

保存和加载ScrollingBackground对象:

- (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;
 }

更新:

我对spriteProperties方法进行了以下更改:

-(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),这意味着我必须对其进行编码/解码。我如何将其子类化以及具有哪些属性?不太确定它是如何运作的。

2 个答案:

答案 0 :(得分:1)

你说你已经将CCSprite分包了,但你实际上是NSObject的子类。尝试:

@interface CCBackgroundSprite: CCSprite <NSCoding>
...

答案 1 :(得分:1)

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