0

我想从 HelloWorldLayer 发送消息,并在 ScoreLayer 中接收它,以便更新标签。CCLOG(@"///addNewScore");工作正常,但是在updateScoreScoreLayer 中,没有接到电话,你知道为什么吗?这是我的代码:(编辑:我尝试在@property 中使用“retain”,但没有任何改变):

@interface HelloWorldLayer : CCLayer
{
    //...
    id<ScoreDelegate>delegate;
}

@property (nonatomic,retain) id <ScoreDelegate> delegate;


@implementation HelloWorldLayer
@synthesize delegate;
//...
-(void)addNewScore:(int)num{
    CCLOG(@"///addNewScore");//works fine
    [delegate updateScore:num];
}


#import <Foundation/Foundation.h>

@protocol ScoreDelegate
-(void)updateScore:(int)num;
@end

@interface ScoreLayer : CCLayer <ScoreDelegate>{
    //...
}

-(void)updateScore:(int)num{
    CCLOG(@"hello");//DOES NOT WORK
}

@end

非常感谢

4

2 回答 2

2

我怀疑 ScoreLayer 是在您致电之前发布的。我不太熟悉assign,我只写过ARC Objective-C;但我认为它与弱(对于代表应该是)大致相同。这意味着为了使该指针有效,应用程序中的其他人需要“拥有” ScoreLayer。

现在,话虽如此,我只假设您首先正确连接了两个对象。没有发布的代码表明这一点,但是可能发布的 ScoreLayer 的问题非常重要,无论哪种方式都必须牢记。

于 2012-06-24T03:55:38.143 回答
-2

您可以在HelloWorldLayer. 然后,您将委托方法放入ScoreLayer.m

-(void)updateScore:(int)num {
    // Do something
}

现在的方式是,您在错误的类中声明了协议。

于 2012-06-24T03:55:38.363 回答