我需要将一个数组从 InGameLayer 传递给 AppDelegate,所以这就是我所做的
在 InGameLayer.h
@interface InGameLayer : CCLayer
@property (nonatomic, strong) CCArray *heroArray;
+(InGameLayer *)sharedInGameLayer;
在 InGameLayer.m
static InGameLayer* sharedInGameLayer;
+(InGameLayer*)sharedInGameLayer
{
if (sharedInGameLayer == nil)
{
sharedInGameLayer = [[self alloc] init];
}
return sharedInGameLayer;
}
//add Object if the button is tapped
- (void)PlayerButton1Tapped:(id)sender
{
CCSprite *hero =[CCSprite spriteWithFile:@"hero.png"];
[_heroArray addObject:hero];
}
在 AppDelegate.m
-(void) applicationDidEnterBackground:(UIApplication*)application
{
CCArray *heroArray = [InGameLayer sharedInGameLayer].heroArray;
CCLOG(@"array = %d", heroArray.count);
}
问题是array = 0,无论我向 heroArray 添加了多少对象。请帮我解决这个问题。提前感谢您的宝贵时间。