我第一次使用单身人士而且我真的不知道如何实施它...
好的,我需要解释一些事情:
在 Hexagon.h (继承自CCNode)我想创建多个精灵(这里称为“六边形”)。但是,它们尚未添加到场景中。通过调用Hexagon *nHex = [[Hexagon alloc]init];
将它们添加到 HelloWorldLayer.m 类中。那是对的吗 ?然后是迭代for循环并创建所有六边形还是只创建一个?
好吧无论如何,我有一个单例类,它必须处理所有公共游戏状态信息但是检索是不可能的。例如,我无法检索existingHexagons
的值,因为它返回(null)
个对象。我错误地设置了对象,或者我错误地从单例中检索数据。实际上,我甚至欣赏其中一个问题的答案。请帮我。如果问题不明确,请添加评论,我会尽力澄清。
我现在所拥有的是:
GameStateSingleton.h
#import <Foundation/Foundation.h>
@interface GameStateSingleton : NSObject{
NSMutableDictionary *existingHexagons;
}
+(GameStateSingleton*)sharedMySingleton;
-(NSMutableDictionary*)getExistingHexagons;
@property (nonatomic,retain) NSMutableDictionary *existingHexagons;
@end
GameStateSingleton.m
#import "GameStateSingleton.h"
@implementation GameStateSingleton
@synthesize existingHexagons;
static GameStateSingleton* _sharedMySingleton = nil;
+(GameStateSingleton*)sharedMySingleton
{
@synchronized([GameStateSingleton class])
{
if (!_sharedMySingleton)
[[self alloc] init];
return _sharedMySingleton;
}
return nil;
}
+(id)alloc
{
@synchronized([GameStateSingleton class])
{
NSAssert(_sharedMySingleton == nil, @"Attempted to allocate a second instance of a singleton.");
_sharedMySingleton = [super alloc];
return _sharedMySingleton;
}
return nil;
}
-(id)init {
self = [super init];
if (self != nil) {
}
return self;
}
@end
Hexagon.m
-(CCSprite *)init{
if( (self=[super init])) {
NSString *mainPath = [[NSBundle mainBundle] bundlePath];
NSString *levelConfigPlistLocation = [mainPath stringByAppendingPathComponent:@"levelconfig.plist"];
NSDictionary *levelConfig = [[NSDictionary alloc] initWithContentsOfFile:levelConfigPlistLocation];
NSString *currentLevelAsString = [NSString stringWithFormat:@"level%d", 1];
NSArray *hexPositions;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
hexPositions = [[levelConfig valueForKey:currentLevelAsString] valueForKey:@"hexpositionIpad"];
}
else{
hexPositions = [[levelConfig valueForKey:currentLevelAsString] valueForKey:@"hexpositionIphone"];
}
NSString *whichType = [NSString stringWithFormat:@"glass"];
CGSize screenSize = [CCDirector sharedDirector].winSize;
if ([whichType isEqualToString:@"stone"]){
hexagon = [CCSprite spriteWithFile:@"octagonstone.png"];
}else if([whichType isEqualToString: @"glass"]){
hexagon = [CCSprite spriteWithFile:@"octagoncolored1.png"];
}else if([whichType isEqualToString: @"metal"]){
hexagon = [CCSprite spriteWithFile:@"octagonmetal.png"];
}
NSMutableDictionary *eHexagons =[[GameStateSingleton sharedMySingleton] getExistingHexagons];
for (int i=0;i < [hexPositions count];i++){
CGPoint location = CGPointFromString([hexPositions objectAtIndex:i]);
CGPoint nLocation= ccp(screenSize.width/2 + 68 * location.x,screenSize.height/2 + 39 * location.y);
NSString *aKey = [NSString stringWithFormat:@"hexagon%d",i];
hexagon =[CCSprite spriteWithFile:@"octagoncolored1.png"];
hexagon.position = nLocation;
[eHexagons setObject:hexagon forKey:aKey];
[self addChild:[eHexagons valueForKey:aKey] z:3];
[[GameStateSingleton sharedMySingleton]setExistingHexagons:eHexagons];
}
NSLog(@"these are the existinghexagons %@", existingHexagons);
//This returns a dictionary with one (null) object
}
return hexagon;
}
HelloWorldLayer.m - &gt; -(id)init
方法
Hexagon *nHex = [[Hexagon alloc]init];
答案 0 :(得分:1)
首先,它返回null
,因为existingHexagons
数组从未初始化过。转到单身人士的init
功能并添加:
existingHexagons = [[NSMutableArray alloc]init];
至于你的For循环问题,我没有得到它。我建议每个查询生成一个StackOverflow问题,而不是将二合一放在一起。