转换期间崩溃cocos2d

时间:2013-12-30 11:50:18

标签: ios xcode cocos2d-iphone

我正在使用cocos2d来创建应用。我正在使用以下代码的滑动手势来触发转换:

-(void) handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer  {
    CCLOG(@"Swiped Left");
    [[CCDirector sharedDirector] replaceScene:[CCTransitionPageTurn transitionWithDuration:1 scene:[Page1 scene]]];
    [[SimpleAudioEngine sharedEngine]unloadEffect:@"Page 1.mp3"];
}

我的问题是,如果用户向左滑动两次(也许他们非常热衷于更改场景),应用程序将崩溃。我不是一个专业的程序员,但似乎transitionWithDuration允许额外的触摸来调用其他方法并在调用dealloc之前执行其他操作。谁能告诉我我做错了什么?

我的完整代码是:

#import "Page1.h"
#import "Page2.h"
#import "OptionsMenu.h"
#import "MainMenu.h"

//activate music

#import "SimpleAudioEngine.h"
#import "CDAudioManager.h"

@implementation Page1

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    Page1 *layer = [Page1 node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super's" return value
    if( (self=[super init]) ) {

        CCLOG(@"Entering the Page 1 init method");

        //getting the screen dimensions
        CGSize size = [[CCDirector sharedDirector] winSize];

        //adding the background
        CCSprite *menuBackground = [CCSprite spriteWithFile:@"Page 1.png"];

        menuBackground.position = ccp(size.width/2,size.height/2);
        [self addChild:menuBackground];

        //pause button
        CCMenuItemImage *pause = [CCMenuItemImage itemWithNormalImage: @"optionsButton.png" selectedImage: @"optionsButton.png" target:self selector:@selector(openMenu)];

        CCMenu *pauseButton = [CCMenu menuWithItems:pause, nil];
        [self addChild:pauseButton];
        pauseButton.position = ccp(40,30);
        pauseButton.scale = 1;

        //page label
        CCLabelTTF *label = [CCLabelTTF labelWithString:@"Page 1" fontName:@"Arial" fontSize:24];
        label.position = ccp(500,20);
        [self addChild:label];

        //using menus as the text label
        CCLabelTTF *one = [CCLabelTTF labelWithString:@"I" fontName:@"Arial" fontSize:24];
        CCLabelTTF *two = [CCLabelTTF labelWithString:@"see" fontName:@"Arial" fontSize:24];
        CCLabelTTF *three = [CCLabelTTF labelWithString:@"that" fontName:@"Arial" fontSize:24];
        CCLabelTTF *four = [CCLabelTTF labelWithString:@"I" fontName:@"Arial" fontSize:24];
        CCLabelTTF *five = [CCLabelTTF labelWithString:@"hold" fontName:@"Arial" fontSize:24];
        CCLabelTTF *six = [CCLabelTTF labelWithString:@"a" fontName:@"Arial" fontSize:24];
        CCLabelTTF *seven = [CCLabelTTF labelWithString:@"sanctuary" fontName:@"Arial" fontSize:24];
        CCLabelTTF *eight = [CCLabelTTF labelWithString:@"in" fontName:@"Arial" fontSize:24];
        CCLabelTTF *nine = [CCLabelTTF labelWithString:@"their" fontName:@"Arial" fontSize:24];
        CCLabelTTF *ten = [CCLabelTTF labelWithString:@"hearts" fontName:@"Arial" fontSize:24];

        CCMenuItemLabel *w1 = [CCMenuItemLabel itemWithLabel:one target:self selector:@selector(w1)];
        CCMenuItemLabel *w2 = [CCMenuItemLabel itemWithLabel:two target:self selector:@selector(w2)];
        CCMenuItemLabel *w3 = [CCMenuItemLabel itemWithLabel:three target:self selector:@selector(w3)];
        CCMenuItemLabel *w4 = [CCMenuItemLabel itemWithLabel:four target:self selector:@selector(w4)];
        CCMenuItemLabel *w5 = [CCMenuItemLabel itemWithLabel:five target:self selector:@selector(w5)];
        CCMenuItemLabel *w6 = [CCMenuItemLabel itemWithLabel:six target:self selector:@selector(w6)];
        CCMenuItemLabel *w7 = [CCMenuItemLabel itemWithLabel:seven target:self selector:@selector(w7)];
        CCMenuItemLabel *w8 = [CCMenuItemLabel itemWithLabel:eight target:self selector:@selector(w8)];
        CCMenuItemLabel *w9 = [CCMenuItemLabel itemWithLabel:nine target:self selector:@selector(w9)];
        CCMenuItemLabel *w10 = [CCMenuItemLabel itemWithLabel:ten target:self selector:@selector(w10)];

        //upper line
        CCMenu *textMenu1 = [CCMenu menuWithItems:w1,w2,w3,w4,w5,w6,w7, nil];
        [self addChild:textMenu1];
        textMenu1.position = ccp(size.width/2,64);
        [textMenu1 alignItemsHorizontallyWithPadding:7];

        //bottom line
        CCMenu *textMenu2 = [CCMenu menuWithItems:w8,w9,w10, nil];
        [self addChild:textMenu2];
        textMenu2.position = ccp(size.width/2,40);
        [textMenu2 alignItemsHorizontallyWithPadding:7];

        //play music
        [SimpleAudioEngine sharedEngine].effectsVolume = 1;

        [[SimpleAudioEngine sharedEngine]playEffect:@"Page 1.mp3"];

        //enabling swipe gestures            
        [self addSwipeGestures];   
    }
    return self;
}

//definiing swipe gesture parameters    
#pragma mark Add UISwipeGestureRecognizer and Handlers

-(void) addSwipeGestures {

    swipeUp =[[ UISwipeGestureRecognizer alloc] initWithTarget: self action:@selector( handleSwipeUp:)]; // colon after method
    swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
    [ [ [CCDirector sharedDirector] view] addGestureRecognizer: swipeUp]; //  add swipe up
    [swipeUp release];

    swipeDownGR =[[ UISwipeGestureRecognizer alloc] initWithTarget: self action:@selector( handleSwipeDown:)]; // colon after method
    swipeDownGR.direction = UISwipeGestureRecognizerDirectionDown;
    [ [ [CCDirector sharedDirector] view] addGestureRecognizer: swipeDownGR]; //  add swipe down
    [swipeDownGR release];

    swipeLeft =[[ UISwipeGestureRecognizer alloc] initWithTarget: self action:@selector( handleSwipeLeft:)]; // colon after method
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [ [ [CCDirector sharedDirector] view] addGestureRecognizer: swipeLeft]; //  add swipe left
    [swipeLeft release];

    swipeRight =[[ UISwipeGestureRecognizer alloc] initWithTarget: self action:@selector( handleSwipeRight:)]; // colon after method
    swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
    [ [ [CCDirector sharedDirector] view]  addGestureRecognizer: swipeRight]; //  add swipe right
    [swipeRight release];        
}

//adding swipe gesture functions    
-(void) handleSwipeUp:(UISwipeGestureRecognizer *)recognizer  {
    CCLOG(@"Swiped Up");
}

-(void) handleSwipeDown:(UISwipeGestureRecognizer *)recognizer  {
    CCLOG(@"Swiped Down");  
}

-(void) handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer  {
    CCLOG(@"Swiped Left");
    //[[CCDirector sharedDirector] replaceScene:[Page2 scene]];
    [[CCDirector sharedDirector] replaceScene:[CCTransitionPageTurn transitionWithDuration:1 scene:[Page1 scene]]];
    [[SimpleAudioEngine sharedEngine]unloadEffect:@"Page 1.mp3"];
}

-(void) handleSwipeRight:(UISwipeGestureRecognizer *)recognizer  {
    CCLOG(@"Swiped Right");
}

-(void)openMenu
{
    [[CCDirector sharedDirector] pushScene:[OptionsMenu scene]];
    [[SimpleAudioEngine sharedEngine]unloadEffect:@"Page 1.mp3"];
}

//methods for the text menus    
-(void)w1 {

}

-(void)w2 {

}

-(void)w3 {

}

-(void)w4 {

}

-(void)w5 {

}

-(void)w6 {

}

-(void)w7 {

}

-(void)w8 {

}

-(void)w9 {

}

-(void)w10 {

}

-(void) dealloc {
    CCLOG( @"removing Page 1 instance" );
    [super dealloc];
}

@end

2 个答案:

答案 0 :(得分:0)

您必须删除手势识别器。如果他们留在身边,下次进行滑动时,它将无效。例如,添加以下内容:

-(void) onExit
{
    [[[CCDirector sharedDirector] view] removeGestureRecognizer:swipeUp];
    [[[CCDirector sharedDirector] view] removeGestureRecognizer:swipeDownGR];
    [[[CCDirector sharedDirector] view] removeGestureRecognizer:swipeLeft];
    [[[CCDirector sharedDirector] view] removeGestureRecognizer:swipeRight];

    [super onExit];
}

答案 1 :(得分:-1)

  

我不是一名专业的程序员,但似乎是[...] dealloc [...]。谁能告诉我我做错了什么?

你做错了是试图在不启用ARC的情况下进行编程。每个人都应该启用ARC,特别是缺乏经验的开发人员,他们希望避免像这样的错误。

为您的项目启用ARC,它会给您一堆编译错误(例如,您不允许在启用ARC的情况下调用[super dealloc],它会告诉您删除该行代码),然后你的错误肯定会消失。

如果遇到无法使用ARC完成的操作,可以基于每个文件禁用它,因此没有理由不为项目启用ARC。您可能永远不需要关闭ARC。

这些天避免ARC的唯一原因是你想支持真正的旧硬件。但它现在已经很老了,它可能在野外不存在(电池会失效等)。