当我只调用addChild一次时,为什么我的程序会崩溃?

时间:2013-07-17 21:21:20

标签: objective-c cocos2d-iphone addchild

-(void) addMenu
{
    CCSprite *disclaimerPic = [CCSprite spriteWithFile:@"MenuBackground.png"];
    disclaimerPic.anchorPoint = CGPointMake(0,0);
    [self addChild:disclaimerPic];

    //playBasic button
    playBasic = [[SneakyButtonSkinnedBase alloc] init];
    playBasic.defaultSprite = [CCSprite spriteWithFile:@"MainMenuButton.png"];
    playBasic.activatedSprite = [CCSprite spriteWithFile:@"MainMenuButtonBroken.png"];
    playBasic.button = [[SneakyButton alloc] init];
    playBasicButton = playBasic.button;
    playBasicButton.isToggleable = NO;

    //playMemory button
    playMemory = [[SneakyButtonSkinnedBase alloc] init];
    playMemory.defaultSprite = [CCSprite spriteWithFile:@"memoryModeButton.png"];
    playMemory.activatedSprite = [CCSprite spriteWithFile:@"memoryModeButton.png"];
    playMemory.button = [[SneakyButton alloc] init];
    playMemoryButton = playMemory.button;
    playMemoryButton.isToggleable = NO;

    //playFrenzy button
    playFrenzy = [[SneakyButtonSkinnedBase alloc] init];
    playFrenzy.defaultSprite = [CCSprite spriteWithFile:@"frenzyModeButton.png"];
    playFrenzy.activatedSprite = [CCSprite spriteWithFile:@"frenzyModeButton.png"];
    playFrenzy.button = [[SneakyButton alloc] init];
    playFrenzyButton = playFrenzy.button;
    playFrenzyButton.isToggleable = NO;

    //playMemoryLocked button
    playMemoryLocked = [[SneakyButtonSkinnedBase alloc] init];
    playMemoryLocked.defaultSprite = [CCSprite spriteWithFile:@"memoryModeButtonLocked.png"];
    playMemoryLocked.activatedSprite = [CCSprite spriteWithFile:@"memoryModeButtonLocked.png"];
    playMemoryLocked.button = [[SneakyButton alloc] init];
    playMemoryLockedButton = playMemoryLocked.button;
    playMemoryLockedButton.isToggleable = NO;

    //playMemoryLocked button
    playFrenzyLocked = [[SneakyButtonSkinnedBase alloc] init];
    playFrenzyLocked.defaultSprite = [CCSprite spriteWithFile:@"frenzyModeButtonLocked.png"];
    playFrenzyLocked.activatedSprite = [CCSprite spriteWithFile:@"frenzyModeButtonLocked.png"];
    playFrenzyLocked.button = [[SneakyButton alloc] init];
    playFrenzyLockedButton = playFrenzyLocked.button;
    playFrenzyLockedButton.isToggleable = NO;

    playBasic.position = ccp(0,-160);
    playMemoryLocked.position = ccp(-85, -160);
    playFrenzyLocked.position = ccp(85, -160);
    playMemory.position = ccp(-85, -160);
    playFrenzy.position = ccp(85, -160);


    if(([self getMemoryModePoints] >= 1000000 || [self getBasicModePoints] >= 5250000) && unlockedMemory == TRUE)
    {
        NSLog(@"1");
        [self addChild: playBasicButton];
        [self addChild: playMemoryButton];
        [self addChild: playFrenzyButton];
    }

   else if([self getBasicModePoints] >= 1750000 && unlockedMemory != TRUE)
    {
        NSLog(@"2");
        [self addChild: playBasicButton];
        [self addChild: playMemoryButton];
        [self addChild: playFrenzyLockedButton];
        unlockedMemory = TRUE;
    }
    else
    {
        [self addChild: playBasicButton]; //<------ It crashes here D:
        [self addChild: playMemoryLockedButton];
        [self addChild: playFrenzyLockedButton];
    }
}

为什么会在那里崩溃?错误说我称之为'addChild'两次,但我显然没有。有人有解决方案吗?我已经尝试将它放入一个场景并换掉新生成的场景当前场景?

------------------------------编辑---------------- ---------------------------------------

#import "startMenu.h"
#import "Basic_Mode.h"
#import "Memory_Mode.h"
#import "Frenzy_Mode.h"
#import "SneakyButton.h"
#import "SneakyButtonSkinnedBase.h"

static startMenu* instanceOfMyClass;

@implementation startMenu

-(id) init
{
    gLayer = [Gest node];
    instanceOfMyClass = self;
    if(self = [super init])
    {
        [self addChild: gLayer];
        [self addBackground];
        //[self addCCMenu];
        //[self addMenu];
        //[self scheduleUpdate];
    }
    return self;
}

-(void) fancierMenuCheck
{
    if([gLayer gestureRecognizer:6] &&([self getMemoryModePoints] >= 1000000 || [self getBasicModePoints] >= 5250000) && unlockedMemory == TRUE)
        [self toFrenzyMode];
    if([gLayer gestureRecognizer:6] &&([self getMemoryModePoints] < 1000000 || [self getBasicModePoints] < 5250000))
        [self unlockFrenzyModeMessage];

    if([gLayer gestureRecognizer:5] && [self getBasicModePoints] >= 1750000 && unlockedMemory != TRUE)
    {
        [self toMemoryMode];
        unlockedMemory = TRUE;
    }
    if([gLayer gestureRecognizer:5] && [self getBasicModePoints] < 1750000 && unlockedMemory != TRUE)
        [self unlockMemoryModeMessage];

    if([gLayer gestureRecognizer:3])
        [self toBasicMode];
}

-(void) addBackground
{
    CCSprite *disclaimerPic = [CCSprite spriteWithFile:@"MenuBackground.png"];
    disclaimerPic.anchorPoint = CGPointMake(0,0);
    [self addChild: disclaimerPic];
}

-(void) addCCMenu
{
    CCSprite *disclaimerPic = [CCSprite spriteWithFile:@"MenuBackground.png"];
    disclaimerPic.anchorPoint = CGPointMake(0,0);
    [self addChild:disclaimerPic];

    CCMenuItem *basicModeButton, *frenzyModeButton, *memoryModeButton, *memoryModeLockedButton, *frenzyModeLockedButton;
    basicModeButton = [CCMenuItemImage itemWithNormalImage:(@"MainMenuButton.png")
                                             selectedImage:(@"MainMenuButtonBroken.png")
                                                    target:self
                                                  selector:@selector(toBasicMode)];
    memoryModeButton = [CCMenuItemImage itemWithNormalImage:(@"memoryModeButton.png")
                                              selectedImage:(@"memoryModeButton.png")
                                                     target:self
                                                   selector:@selector(toMemoryMode)];
    frenzyModeButton = [CCMenuItemImage itemWithNormalImage:(@"frenzyModeButton.png")
                                              selectedImage:(@"frenzyModeButton.png")
                                                     target:self
                                                   selector:@selector(toFrenzyMode)];

    memoryModeLockedButton = [CCMenuItemImage itemWithNormalImage:(@"memoryModeButtonLocked.png")
                                             selectedImage:(@"memoryModeButtonLocked.png")
                                                    target:self
                                                  selector:@selector(unlockMemoryModeMessage)];

    frenzyModeLockedButton = [CCMenuItemImage itemWithNormalImage:(@"frenzyModeButtonLocked.png")
                                             selectedImage:(@"frenzyModeButtonLocked.png")
                                                    target:self
                                                  selector:@selector(unlockFrenzyModeMessage)];

    CCMenu *menu;
    basicModeButton.position = ccp(0,-160);
    memoryModeButton.position = ccp(-85, -160);
    frenzyModeButton.position = ccp(85, -160);
    memoryModeLockedButton.position = ccp(-85,-160);
    frenzyModeLockedButton.position = ccp(85, -160);

    if(([self getMemoryModePoints] >= 1000000 || [self getBasicModePoints] >= 5250000) && unlockedMemory == TRUE)
    {
        basicModeButton.isEnabled = TRUE;
        memoryModeButton.isEnabled = TRUE;
        frenzyModeButton.isEnabled = TRUE;
        menu = [CCMenu menuWithItems:basicModeButton,memoryModeButton,frenzyModeButton, nil];
    }
    else if([self getBasicModePoints] >= 1750000 && unlockedMemory != TRUE)
    {
        basicModeButton.isEnabled = TRUE;
        memoryModeButton.isEnabled = TRUE;
        frenzyModeLockedButton.isEnabled = TRUE;
        menu = [CCMenu menuWithItems:basicModeButton,memoryModeButton,frenzyModeLockedButton, nil];
    }
    else
    {
        basicModeButton.isEnabled = TRUE;
        memoryModeLockedButton.isEnabled = TRUE;
        frenzyModeLockedButton.isEnabled = TRUE;
        menu = [CCMenu menuWithItems:basicModeButton,memoryModeLockedButton,frenzyModeLockedButton, nil];
    }

    menu.enabled = YES;
    [self addChild: menu];
}

+(id) scene
{
    CCScene *scene = [CCScene node];
    startMenu *layer = [startMenu node];
    [scene addChild:layer];
    return scene;
}

-(void) addMenu
{
    CCSprite *disclaimerPic = [CCSprite spriteWithFile:@"MenuBackground.png"];
    disclaimerPic.anchorPoint = CGPointMake(0,0);
    [self addChild:disclaimerPic];

    //playBasic button
    playBasic = [[SneakyButtonSkinnedBase alloc] init];
    playBasic.defaultSprite = [CCSprite spriteWithFile:@"MainMenuButton.png"];
    playBasic.activatedSprite = [CCSprite spriteWithFile:@"MainMenuButtonBroken.png"];
    playBasic.button = [[SneakyButton alloc] init];
    playBasicButton = playBasic.button;
    playBasicButton.isToggleable = NO;

    //playMemory button
    playMemory = [[SneakyButtonSkinnedBase alloc] init];
    playMemory.defaultSprite = [CCSprite spriteWithFile:@"memoryModeButton.png"];
    playMemory.activatedSprite = [CCSprite spriteWithFile:@"memoryModeButton.png"];
    playMemory.button = [[SneakyButton alloc] init];
    playMemoryButton = playMemory.button;
    playMemoryButton.isToggleable = NO;

    //playFrenzy button
    playFrenzy = [[SneakyButtonSkinnedBase alloc] init];
    playFrenzy.defaultSprite = [CCSprite spriteWithFile:@"frenzyModeButton.png"];
    playFrenzy.activatedSprite = [CCSprite spriteWithFile:@"frenzyModeButton.png"];
    playFrenzy.button = [[SneakyButton alloc] init];
    playFrenzyButton = playFrenzy.button;
    playFrenzyButton.isToggleable = NO;

    //playMemoryLocked button
    playMemoryLocked = [[SneakyButtonSkinnedBase alloc] init];
    playMemoryLocked.defaultSprite = [CCSprite spriteWithFile:@"memoryModeButtonLocked.png"];
    playMemoryLocked.activatedSprite = [CCSprite spriteWithFile:@"memoryModeButtonLocked.png"];
    playMemoryLocked.button = [[SneakyButton alloc] init];
    playMemoryLockedButton = playMemoryLocked.button;
    playMemoryLockedButton.isToggleable = NO;

    //playMemoryLocked button
    playFrenzyLocked = [[SneakyButtonSkinnedBase alloc] init];
    playFrenzyLocked.defaultSprite = [CCSprite spriteWithFile:@"frenzyModeButtonLocked.png"];
    playFrenzyLocked.activatedSprite = [CCSprite spriteWithFile:@"frenzyModeButtonLocked.png"];
    playFrenzyLocked.button = [[SneakyButton alloc] init];
    playFrenzyLockedButton = playFrenzyLocked.button;
    playFrenzyLockedButton.isToggleable = NO;

    playBasic.position = ccp(0,-160);
    playMemoryLocked.position = ccp(-85, -160);
    playFrenzyLocked.position = ccp(85, -160);
    playMemory.position = ccp(-85, -160);
    playFrenzy.position = ccp(85, -160);

//    [self addChild:playBasicButton];
//    if([self getBasicModePoints] >= 175000)
//     {
//         [self addChild: playMemoryButton];
//         unlockedMemory = TRUE;
//     }
//     else
//     {  [self addChild: playMemoryLockedButton];    }
//    
//    if(([self getBasicModePoints] >= 5250000 || [self getMemoryModePoints] >= 1000000) && unlockedMemory == TRUE)
//    {    [self addChild: playFrenzyButton]; }
//    else
//        [self addChild: playFrenzyLockedButton];

    if(([self getBasicModePoints] >= 5250000 || [self getMemoryModePoints] >= 1000000) && unlockedMemory == TRUE)
    {
        [self addChild: playBasicButton];
        [self addChild: playMemoryButton];
        [self addChild: playFrenzyButton];
    }
    else if([self getBasicModePoints] >= 1750000 && unlockedMemory != TRUE)
    {
        [self addChild: playBasicButton];
        [self addChild: playMemoryButton];
        [self addChild: playFrenzyLockedButton];
        unlockedMemory = TRUE;
    }
    else
    {
        [self addChild: playBasicButton];
        [self addChild: playMemoryLockedButton];
        [self addChild: playFrenzyLockedButton];
    }
}

-(void) restartScene
{
    CCScene *currentScene = [CCDirector sharedDirector].runningScene;
    CCScene *newScene = [[[currentScene class] alloc] init];
    [[CCDirector sharedDirector] replaceScene:newScene];
}

-(void) unlockMemoryModeMessage
{
    CCSprite *picture = [CCSprite spriteWithFile:@"unlockMemoryModePic.png"];
    picture.position = CGPointMake(160,240);
    [self addChild: picture z:1];
}

-(void) unlockFrenzyModeMessage
{
    CCSprite *picture = [CCSprite spriteWithFile:@"unlockFrenzyModePic.png"];
    picture.position = CGPointMake(160,240);
    [self addChild: picture z:1];
}

-(void) backToStartMenu
{
    startMenu *sm = [[startMenu alloc] init];
    [[CCDirector sharedDirector] replaceScene:(CCScene*) sm];
}

-(int) getMemoryModePoints
{
    Memory_Mode* mm = [[Memory_Mode alloc] init];
    return [mm get_memory_mode_points];
}

-(int) getBasicModePoints
{
    Basic_Mode* bm = [[Basic_Mode alloc] init];
    return [bm get_basic_mode_points];
}

-(void) toBasicMode
{
    Basic_Mode *bma = [[Basic_Mode alloc] init];
    [[CCDirector sharedDirector] replaceScene:(CCScene*)bma];
}

-(void) toMemoryMode
{
    Memory_Mode *mma = [[Memory_Mode alloc] init];
    [[CCDirector sharedDirector] replaceScene:(CCScene*) mma];
}

-(void) toFrenzyMode
{
    Frenzy_Mode *fma = [[Frenzy_Mode alloc] init];
    [[CCDirector sharedDirector] replaceScene:(CCScene*) fma];
}

-(void) update:(ccTime)delta
{
//    if(playBasicButton.active == YES)
//        [self toBasicMode];
//    if(playMemoryButton.active == YES)
//        [self toMemoryMode];
//    if(playFrenzyButton.active == YES)
//        [self toFrenzyMode];
//    if(playMemoryLockedButton.active == YES)
//        [self unlockMemoryModeMessage];
//    if(playFrenzyLockedButton.active == YES)
//        [self unlockFrenzyModeMessage];

    [self fancierMenuCheck];
}

@end

这是全班。

0 个答案:

没有答案