我想用NSColorWell动态改变我的背景颜色。我在桌面上使用cocos2d框架。
我有一个显示cocos2d窗口的GLView,我有一个NSColorwell。当我点击颜色的颜色时,我希望背景能够“即时”改变为那种颜色。
我将所有变量都放在一个名为settings的类中。我的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
Settings.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface Settings : NSObject {
NSColor *_backgroundColor;
}
+ (Settings *) sharedSettings;
- (NSColor *) returnBackgroundColor;
- (void) setBackgroundColor : (NSColor *)c;
@end
Settings.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]);
}