0

我想使用 NSColorWell 动态更改我的背景颜色。我正在使用 cocos2d 桌面框架。

我有一个显示 cocos2d 窗口的 GLView,我在旁边有一个 NSColorwell。当我单击颜色井颜色时,我希望背景可以“即时”更改为该颜色。

我将所有变量都放在一个名为设置的类中。我的 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]);


}
4

0 回答 0