重启CCDIrector会导致奇怪的错误

时间:2011-11-28 05:57:23

标签: iphone cocos2d-iphone

我正在尝试重启我游戏的CCDirector。但我有错误。

适合所有这些是我的菜单中的代码[我的菜单与XIB]:

- (void) retryGame:(NSNotification *)notif {
    [menu2Game.view removeFromSuperview];
    [[CCDirector sharedDirector] end];
    [[CCDirector sharedDirector] release];

    [self startGame:play];
}

它给了我和EXC_BAD_ACCESS。就在我这样做两次的时候。第一个完美地工作......

但如果删除

[self startGame:play];

我将返回菜单,然后点击按钮播放,我可以点无数次,它有效!

我的退出就是这样,我试用了20次。

- (void) quitGame:(NSNotification *)notif {
    [menu2Game.view removeFromSuperview];
    [[CCDirector sharedDirector] end];
    [[CCDirector sharedDirector] release];

    //Restart Animations
    [logo setFrame:CGRectMake(239, 119, 1, 1)];
    [play setFrame:CGRectMake(-233, play.frame.origin.y, play.frame.size.width, play.frame.size.height)];
    [options setFrame:CGRectMake(-233, options.frame.origin.y, options.frame.size.width, options.frame.size.height)];
    [extras setFrame:CGRectMake(-233, extras.frame.origin.y, extras.frame.size.width, extras.frame.size.height)];
    [GC setFrame:CGRectMake(0, 300, 1, 1)];

    [self viewDidLoad];
}

//在我的viewdidLoad中我只有动画

可能是什么问题?我尝试延迟[perfomselector],但它同样的问题......就在我回到菜单并点击播放我可以再次播放。无论速度如何,你都可以点击播放,暂停,退出,播放,暂停,退出等...游戏完美无缺

由于

2 个答案:

答案 0 :(得分:1)

CCDirector是一个单例,你不应该发送它的发布消息!

[[CCDirector sharedDirector] release];

不这样做完全没问题。对于它的价值,CCDirector startAnimation和stopAnimation是暂时停止Cocos2D的更可靠的方法。

答案 1 :(得分:0)

同样的问题

- (void) retryGame:(NSNotification *)notif {
    [menu2Game.view removeFromSuperview];
    [[CCDirector sharedDirector] end];
    //[[CCDirector sharedDirector] release];

    [self startGame:play];
}

开始游戏

 - (IBAction)startGame:(id)sender {


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(quitGame:) name:@"quitGame" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(retryGame:) name:@"retryGame" object:nil];

    CCDirector *director = [CCDirector sharedDirector];

    // Init the View Controller
    menu2Game = [[Menu2Game alloc] initWithNibName:@"Menu2Game" bundle:nil];
    menu2Game.wantsFullScreenLayout = YES;

    //
    // Create the EAGLView manually
    //  1. Create a RGB565 format. Alternative: RGBA8
    //  2. depth format of 0 bit. Use 16 or 24 bit for 3d effects, like CCPageTurnTransition
    //
    //
    EAGLView *glView = [EAGLView viewWithFrame:[self.view bounds]
                                   pixelFormat:kEAGLColorFormatRGB565   // kEAGLColorFormatRGBA8
                                   depthFormat:0                        // GL_DEPTH_COMPONENT16_OES
                        ];

    // attach the openglView to the director
    [director setOpenGLView:glView];

    // Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
    //  if( ! [director enableRetinaDisplay:YES] )
    //      CCLOG(@"Retina Display Not supported");


    [director setAnimationInterval:1.0/60];
    [director setDisplayFPS:YES];


    // make the OpenGLView a child of the view controller
    [menu2Game setView:glView];

    // make the View Controller a child of the main window
    [self.view addSubview: menu2Game.view];

    // Default texture format for PNG/BMP/TIFF/JPEG/GIF images
    // It can be RGBA8888, RGBA4444, RGB5_A1, RGB565
    // You can change anytime.
    [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];

    // Assusme PVR images have the alpha channel premultiplied
    [CCTexture2D PVRImagesHavePremultipliedAlpha:YES];

    // Run the intro Scene
    if ([director runningScene] == NULL) {
        [[CCDirector sharedDirector] runWithScene:[Game scene]];
    }else{
        [[CCDirector sharedDirector] replaceScene:[Game scene]];
    }
}

如果我发表评论[s​​elf startGame:play];错误永远不会出现

这里的错误

CCTextureAtlas.m [Cocos 2d]

#if CC_TEXTURE_ATLAS_USE_TRIANGLE_STRIP
    glDrawElements(GL_TRIANGLE_STRIP, (GLsizei) n*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(indices_[0])) );
#else
    glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, (GLvoid*) (start*6*sizeof(indices_[0])) );