처음으로 싱글 톤을 사용합니다. 실제로 구현하는 방법을 모르겠습니다 ... 그래, 몇 가지를 설명해야합니다 : 에서 Hexagon.h (CCNode에서 상속) 여러 개의 스프라이트 (여기서는 "육각형"이라고 함)를 만들고 싶습니다. 그러나 그들은 장면에 아직 추가되지 않습니다. 그들은 로 전화하여 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
,536 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 - existingHexagons
배열이 처음에 초기화 된 적이 있기 때문에 모든>-(id)init
방법
Hexagon *nHex = [[Hexagon alloc]init];
우선, 답변 해 주셔서 감사합니다. 사전을 초기화하는 것은 불행히도 물론 필수적 임에도 불구하고 문제를 해결하지 못했습니다 ... for 루프의 임무는 각기 다른 위치에 여러 개의 육각형을 만드는 것입니다. 그래서 만약 내가'Hexagon * nHex = [[Hexagon alloc] init]이라고 부르면 루프를 반복하고 모든 육각형을 그 위치에 만들어야합니다. 클래스 메소드가 하나의'CCprite *'객체를 반환해야하기 때문에 약간 혼란 스럽습니다. 맞습니까? 따라서 여러 개의 스프라이트를 만드는 것은 불가능합니다. –
글쎄, 반환 값 형식이 CCSprite이기 때문에 그 중 하나만 인스턴스를 반환 할 수 있습니다 ... 그러나 CCSprite 형식을 반환하는 대신 NSArray를 반환 할 수 있습니다. 이러한 배열에는 여러 CCSprites가 포함됩니다. 그래서 당신의 For 루프는 NSMutableArray를 채워야하고, 함수의 끝에서 그런 배열을 반환합니다. – Voldemort