NSColorWell を使用して、その場で背景色を動的に変更しようとしています。デスクトップ用のcocos2dフレームワークを使用しています。
cocos2d ウィンドウを表示する GLView があり、横に NSColorwell があります。カラーウェルの色をクリックすると、いわば「オンザフライ」で背景をその色に変更します。
すべての変数は、設定と呼ばれる 1 つのクラスにあります。そして、私の AppDeletgate には NSColorWell の IBActions があります。Cocos2D GLview は HelloWorldLayer に存在します。
NSColorWell で色を選択すると、例外がスローされます。理由はありますか?
コード ダンプを許してください。しかし、私が何をしているかを正確に確認できるようにしたかったのです。これを実装するためにさまざまな方法を試しました。
AppDelegate.h
#import "cocos2d.h"
@interface AnimatorAppDelegate : NSObject <NSApplicationDelegate>
{
    NSWindow    *window_;
    MacGLView   *glView_;
@property (assign) IBOutlet NSWindow    *window;
@property (assign) IBOutlet MacGLView   *glView;
- (IBAction)bgColorWell:(id)sender;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "HelloWorldLayer.h"
#import "Settings.h"
@implementation AnimatorAppDelegate
@synthesize window=window_, glView=glView_;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
    [director setDisplayFPS:YES];
    [director setOpenGLView:glView_];
    [director setResizeMode:kCCDirectorResize_AutoScale];
    [window_ setAcceptsMouseMovedEvents:NO];
    [director runWithScene:[HelloWorldLayer scene]];
}
#pragma mark AppDelegate - IBActions
- (IBAction)toggleFullScreen: (id)sender
{
    CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
    [director setFullScreen: ! [director isFullScreen] ];
}
- (IBAction)bgColorWell:(id)sender {
    NSLog(@"Color Well %@", [sender color]);
    HelloWorldLayer *layer = [[HelloWorldLayer alloc] init];
    [layer setBackgroundColorForLayer:[sender color]];
}
@end
設定.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface Settings : NSObject {
    NSColor *_backgroundColor;
}
+ (Settings *) sharedSettings;
- (NSColor *) returnBackgroundColor;
- (void) setBackgroundColor : (NSColor *)c;
@end
設定.m
#import "Settings.h"
@implementation Settings
static  Settings * _sharedSettings;
- (id) init {
    if (self =  [super init]){
//_backgroundColor = [NSColor colorWithDeviceRed:102/255.0f green:205/255.0f blue:170/255.0f alpha:1.0];
        _backgroundColor = [NSColor blueColor];
    }
    return self;
}
+ (Settings *) sharedSettings {
    if (!_sharedSettings) {
        _sharedSettings = [[Settings alloc] init];
    }
    return _sharedSettings;
}
- (NSColor *) returnBackgroundColor {
    return _backgroundColor;
}
- (void) setBackgroundColor : (NSColor *)c {
    _backgroundColor = c;
}
@end
HelloWorldLayer.h
#import "cocos2d.h"
#import "Settings.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer
{
}
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
- (void) setBackgroundColorForLayer : (id) sender;
@end
HelloWorldLayer.m
// Import the interfaces
#import "AppDelegate.h"
#import "HelloWorldLayer.h"
#import  "Settings.h"
// HelloWorldLayer implementation
@implementation HelloWorldLayer
+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];
    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];
    // add layer as a child to scene
    [scene addChild: layer];
    // return the scene
    return scene;
}
// on "init" you need to initialize your instance
-(id) init
{
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
    }
    return self;
}
- (void) setBackgroundColorForLayer : (id) sender {
    Settings *mySettings = [Settings sharedSettings];
    float red = [mySettings returnBackgroundColor].redComponent * 255;
    float green = [mySettings returnBackgroundColor].greenComponent * 255;
    float blue = [mySettings returnBackgroundColor].blueComponent * 255;
    CCLayerColor* colorLayer = [CCLayerColor layerWithColor:ccc4(red, green, blue, 255)];
    [self addChild:colorLayer z:0];
    NSLog(@"Red color: %f", red);
    NSLog(@"Green color: %f", green);
    NSLog(@"Blue color: %f", blue);
    //[self setBackgroundColorForLayer:sender];
    NSLog(@"This Background color is %@" , [mySettings returnBackgroundColor]);
}