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

Cocos2d iphone 具有符合NSCoding的CCSprite的自定义对象不显示,cocos2d-iphone,nsuserdefaults,nscoding,Cocos2d Iphone,Nsuserdefaults,Nscoding,为了保存ScrollingBackground对象,我对CCSprites进行了子类化,以符合NSCoding。滚动背景不显示。请参阅下面的相关代码。我真的不确定出了什么问题。请帮忙 滚动背景.h: (CCBackgroundSprite的界面) 为CCSprite属性设置CCBackgroundSprite的实例: -(void)spriteProperties { background1 = [[CCBackgroundSprite alloc] init]; [backgrou

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

滚动背景.h: (CCBackgroundSprite的界面)

为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的其他非精灵相关属性的编码/解码:

-(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;
 }
更新:

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

-(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);
}
我刚刚意识到一些事情:

  • 启动精灵中的CCLOG为空
  • 我一路上重用了
    currentBackgroundBatchNode
    (这是一个
    CCSpriteBatchNode
    ),这意味着我必须对它进行编码/解码。如何对其进行子类化,以及使用哪些属性?我不太确定会怎样
    你说你已经子类化了
    CCSprite
    ,但实际上你子类化了
    NSObject
    。尝试:

    @interface CCBackgroundSprite: CCSprite <NSCoding>
    ...
    
    @接口CCBackgroundSprite:CCSprite
    ...
    
    我已经阅读了你的一些帖子,也与此相关。我建议您使用更简单的解决方法,而不是尝试将几个cocos2d类划分为子类以符合NSCoding。我相信你的背景有它自己的图层,所以你为什么不保存各种背景参数并为你的背景创建另一个init来处理重新加载背景状态的情况呢。

    好的一点,还让我想知道addChild:是否曾经在节点上使用过,因为传入非CCNode对象肯定会crash@LearnCocos2D我同意。。。我不知道你会怎么做,你通常需要做的任何事情来设置一个精灵提供的代码。谢谢你提请我注意这一点。我对子类化有点困惑。请查看帖子,我已经更新了。我仍然有一些问题,保存的状态没有显示。PS:你知道Sprite工具包,它支持NSCoding吗?我只是想提一下……我听说了。除了支持NSCoding,我还需要对代码进行任何其他更改吗?我听从了你的建议。这不完全是我想要的,但效果很好。
    -(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);
    }
    
    @interface CCBackgroundSprite: CCSprite <NSCoding>
    ...