在Cocos2d中没有调用单例方法



我正在调用一个Singleton方法,当我尝试这样做时不会被调用。我没有得到任何错误或任何东西,只是我无法看到CCLOG消息。

在什么原因下编译器不给你错误,不允许你调用一个方法?

[[GameManager sharedGameManager] openSiteWithLinkType:kLinkTypeDeveloperSite];

方法定义如下:

-(void)openSiteWithLinkType:(LinkTypes)linkTypeToOpen
{
    CCLOG(@"WE ARE IN openSiteWithLinkType"); //I NEVER SEE THIS MESSAGE
    NSURL *urlToOpen = nil;
    if (linkTypeToOpen == kLinkTypeDeveloperSite) 
    {
        urlToOpen = [NSURL URLWithString:@"http://somesite.com"];    
    }
    if (![[UIApplication sharedApplication]openURL:urlToOpen])
    {
         CCLOG(@"%@%@",@"Failed to open url:",[urlToOpen description]);
        [self runSceneWithID:kMainMenuScene];
    }
}

下面是我的单例代码:

#import "GameManager.h"
#import "MainMenuScene.h"

@implementation GameManager
static  GameManager* _sharedGameManager = nil;
@synthesize isMusicON;
@synthesize isSoundEffectsON;
@synthesize hasPlayerDied;
+(GameManager*) sharedGameManager
{
@synchronized([GameManager class])
    {
        if (!_sharedGameManager) 
        {
            [[self alloc] init];
            return _sharedGameManager;
        }
        return nil;
    }
}

+(id)alloc
{
    @synchronized ([GameManager class])
    {
        NSAssert(_sharedGameManager == nil,@"Attempted to allocate a second instance of the Game Manager singleton");
        _sharedGameManager = [super alloc];
        return _sharedGameManager;
    }
    return nil;
}
-(id)init
{
    self = [super init];
    if (self != nil)
    {
        //Game Manager initialized
        CCLOG(@"Game Manager Singleton, init");
        isMusicON = YES;
        isSoundEffectsON = YES;
        hasPlayerDied = NO;
        currentScene = kNoSceneUninitialized;
    }
    return self;
}

-(void) runSceneWithID:(SceneTypes)sceneID
{
    SceneTypes oldScene = currentScene;
    currentScene = sceneID;
    id sceneToRun = nil;

    switch (sceneID) 
    {
        case kMainMenuScene:
            sceneToRun = [MainMenuScene node];
            break;
        default:
            CCLOG(@"Unknown Scene ID, cannot switch scenes");
            return;
            break;
    }
    if (sceneToRun == nil)
    {
        //Revert back, since no new scene was found
        currentScene = oldScene;
        return;
    }


    //Menu Scenes have a value of < 100
    if (sceneID < 100) 
    {
        if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad)
        {
            CGSize screenSize = [CCDirector sharedDirector].winSizeInPixels;
            if (screenSize.width == 960.0f)
            {
                //iPhone 4 retina
                [sceneToRun setScaleX:0.9375f];
                [sceneToRun setScaleY:0.8333f];
                CCLOG(@"GM: Scaling for iPhone 4 (retina)");
            }
            else
            {
                [sceneToRun setScaleX:0.4688f];
                [sceneToRun setScaleY:0.4166f];
                CCLOG(@"GM: Scaling for iPhone 3G or older (non-retina)");
            }
        }    
    }

    if ([[CCDirector sharedDirector] runningScene] == nil)
    {
        [[CCDirector sharedDirector] runWithScene:sceneToRun];
    }
    else
    {
        [[CCDirector sharedDirector] replaceScene:sceneToRun];
    }
}

-(void)openSiteWithLinkType:(LinkTypes)linkTypeToOpen
{
    CCLOG(@"WE ARE IN openSiteWithLinkType");
    NSURL *urlToOpen = nil;
    if (linkTypeToOpen == kLinkTypeDeveloperSite) 
    {
        urlToOpen = [NSURL URLWithString:@"http://somesite.com"];    
    }
    if (![[UIApplication sharedApplication]openURL:urlToOpen])
    {
         CCLOG(@"%@%@",@"Failed to open url:",[urlToOpen description]);
        [self runSceneWithID:kMainMenuScene];
    }
}

-(void) test
{

    CCLOG(@"this is test");
}
@end

也许sharedGameManager返回nil?这不会导致错误。

编辑:问题在你发布的新代码中:

if (!_sharedGameManager) {
    [[self alloc] init];
    return _sharedGameManager;
}
return nil;

如果_sharedGameManager非nil,这将返回nil。你想要的:

if (!_sharedGameManager) {
    [[self alloc] init];
}
return _sharedGameManager;

查看这篇关于如何创建一个合适的单例的Cocoa With Love文章

最新更新