具有符合 NSCoding 的 CCSprite 的自定义对象不显示 [英] custom object with CCSprite that conforms to NSCoding not displaying

查看:15
本文介绍了具有符合 NSCoding 的 CCSprite 的自定义对象不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了保存一个 ScrollingBackground 对象,我将 CCSprite 子类化为符合 NSCoding.ScrollingBackground 不显示.请参阅下面的相关代码.我不太确定出了什么问题.请帮忙.

In my effort to save a ScrollingBackground object I've subclassed the CCSprites to conform to NSCoding. The ScrollingBackground doesn't display. Please see the relevant code below. I'm not really sure whats wrong. Please help.

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

ScrollingBackground.h: (CCBackgroundSprite's interface)

@interface CCBackgroundSprite: NSObject <NSCoding>

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

@end

滚动背景.m:(CCBackgroundSprite 的实现)

ScrollingBackground.m: (CCBackgroundSprite's implementation)

@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 的实例:

Setting CCBackgroundSprite's instances for the CCSprite properties:

-(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 相关属性的编码/解码:

encoding/decoding of other non-Sprite related properties of the 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 对象的保存和加载:

Saving and loading of ScrollingBackground object:

- (void)saveBackgroundObject:(ScrollingBackground *)object key:(NSString *)key {
   [self spriteProperties];
   NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object];
   NSString *dataToString = [NSString stringWithFormat:@"%@", encodedObject];
   CCLOG(@"encodedObject = %@ 
", 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 = %@ 
", dataToString);
   ScrollingBackground *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
   return object;
 }

更新:

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

I have made the following changes the spriteProperties method:

-(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 的主要原因是我一直在重用精灵.还要设置我所做的背景图像:

The main reason I am using displayFrame.textureFilename above is because I'm reusing the sprites along the way. Also to setup of the background images I did:

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

我刚刚意识到一些事情:

I've just realized a few things:

  1. startingSprites 中的 CCLOG 为空
  2. 我沿途重复使用 currentBackgroundBatchNode(这是一个 CCSpriteBatchNode),这意味着我必须对其进行编码/解码.我如何子类化它以及具有哪些属性?不太确定它会如何工作.
  1. the CCLOG's in startingSprites are null
  2. I reuse the currentBackgroundBatchNode (which is a CCSpriteBatchNode) along the way, meaning that I have to encode/decode it. How do I subclass it and with what properties? Not too sure how it'll work out.

推荐答案

我看了你的很多帖子,也和这个有关.我建议不要尝试将几个 cocos2d 类子类化以符合 NSCoding,而应该使用更简单的解决方法.我相信你的背景有它自己的层,所以你为什么不保存各种背景参数并为你的背景创建另一个 init 来处理重新加载背景状态的情况.

I have read a number of your posts, also related to this. I would recommend that instead of trying to subclass several cocos2d classes to conform to NSCoding you should use a simpler work around. I believe your background has it's own layer, so why don't you rather save various background parameters and create another init for your background to handle cases for reloading the background state.

这篇关于具有符合 NSCoding 的 CCSprite 的自定义对象不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆