0

我在 Mac 上玩 Cocos2D。

我正在使用带有 NSColorWell 按钮的 IntefaceBuilder。我的 AppDelegate 具有 IBAction 以在名为 Settings 的 Singleton 中设置背景颜色。然后我希望它调用 AnimationLayer 的方法updateBackgroundColor来更新它。但它失败并崩溃。

为什么我不能向 AnimationLayer 方法发送消息?

由于 AppDelegate 已经知道 AnimationLayer 这不是调用方法的正确方法吗?[(AnimationLayer*) self methodNameHere];

AppDeletgate.m

- (IBAction)colorwellBackground:(id)sender {
    [mySettings setBackgroundColor:[sender color]];
    [(AnimationLayer*) self updateBackgroundColor];
}

动画层.m

// Import the interfaces
#import "AnimationLayer.h"

// HelloWorldLayer implementation
@implementation AnimationLayer

+(CCScene *) scene {

    CCScene *scene = [CCScene node];
    AnimationLayer *layer = [AnimationLayer node];
    [scene addChild: layer];
    return scene;
}

// on "init" you need to initialize your instance
-(id) init {

    if( (self=[super init]) ) {

        mySettings = [Settings sharedSettings]; 

        //DEFAULT BACKGROUND COLOR
        _backgroundColorLayer = [mySettings returnBackgroundColor];
        [self addChild:_backgroundColorLayer];


    }
    return self;
}
- (void) updateBackgroundColor{
    NSLog(@"UPDATE BACKGROUND COLOR %@", [mySettings returnBackgroundColor]);
    [_backgroundColorLayer setColor:[mySettings returnBackgroundColor]];
}
4

1 回答 1

0

首先,如果您的应用程序崩溃,您总是可以在控制台中看到崩溃原因。始终将此消息添加到您的“为什么会崩溃”问题中。实际上,很有可能在阅读此消息后,您将能够自己解决问题。

我假设您的崩溃消息类似于“尝试从 appdelegate 实例调用未知方法 updateBackgroundColor”。在您的 AppDelegate 类中,self 指向 AppDelegate 实例,而不是您的层。如果您想从您的应用程序委托中调用此方法(这对我来说实际上很奇怪),那么您必须以某种方式获取动画层的当前实例。

于 2013-04-12T11:40:20.437 回答