PDA

View Full Version : setNeedsDisplay


Coin
2005.01.05, 02:13 AM
i am BRAND new to trying graphics in Obj-C,
ive made otehr things... a calculator, a hangman game (interface builder only), and a few other weird things, but i dont know how to operate them custom views.
i dont really know what a first project should be so i figured a worm type game (the one on old cell phones) no inported graphics just drawing lines, i havnt even really started
so far i have the app launches and shows a brown square, then i figued i should, have some preferences for background/worm color, so i have color wells, i cant even get a button to change the color of the view to the color of the color well, here is what i have

header file

#import <Cocoa/Cocoa.h>

@interface gameView : NSView
{
IBOutlet NSColorWell* bgColor;
IBOutlet NSColorWell* wormColor;
IBOutlet NSTextField* nameField;
IBOutlet NSWindow* gameWindow;
IBOutlet NSWindow* welcomeSheet;
IBOutlet NSView* stretchView;
IBOutlet NSColorWell* test;

NSColor * wc;
NSColor * bgc;

NSBezierPath *worm;

NSString *name;

}
- (IBAction)saveColors:(id)sender;
- (IBAction)newGame:(id)sender;
- (IBAction)continue:(id)sender;
@end


here is the code for awakeFromNib and the drawRect one

- (void)awakeFromNib
{
bgc = [NSColor brownColor];
wc = [NSColor orangeColor];

[wormColor setColor:wc];
[bgColor setColor:bgc];
//wormColor is the worm colorwell
//bgColor is the background color

}

- (void)drawRect:(NSRect)rect
{
NSRect bounds = [self bounds];
[bgc set];
[NSBezierPath fillRect:bounds];
}


here is the code that is run when you press the set bacground button

- (IBAction)saveColors:(id)sender
{
bgc = [[bgColor color] retain];
wc = [wormColor color];
[stretchView setNeedsDisplay:YES];
}



this same question has been sitting on the createMacGames fourm for a week, so someone please help.......

arekkusu
2005.01.05, 05:48 AM
Try looking at /Developer/Examples/AppKit/DotView

It demonstrates using an NSView subclass, bezier path drawing, and an NSColorWell, and is heavily commented.

Coin
2005.01.05, 03:48 PM
i looked at the drawDot appl thin in the appkit examples, and in IB they didn't instantiate the DotView, how did they connect the interface to the buttons? and color wells? do i need to make a middleman class?

is that it?

BinarySpike
2005.01.05, 05:14 PM
i dont really know what a first project should be so i figured a worm type game (the one on old cell phones)

WHAT!!! "old cell phones"
I DON'T THINK SO BUSTER.
"Snake", "Dragon" etc. etc. were made on the atari and NES, and in arcades.
(long before the internet was even made)

Ok, I'll cut you a break from my MOST favorite type of "old" game. :D

I found when I had a problem that I could solve it by looking
at references (books, developer.apple.com, www.google.com)
I find A TON of help there....
so you should look around (if you haven't already :))

arekkusu
2005.01.05, 05:22 PM
i looked at the drawDot appl thin in the appkit examples, and in IB they didn't instantiate the DotView, how did they connect the interface to the buttons? and color wells? do i need to make a middleman class?
Yes, they do instantiate the dotView. It is the main view in the window, and the color well and slider are attached to it. Look closer at the connections.

Often it is good to instantiate a controller class (or use the nib's owner) to handle the glue logic, but for this simple of an application the view subclass can handle everything via the nib.

Coin
2005.01.06, 02:43 AM
ah, yea ok, i was looing for it in the little window with files owner and first responder

arekkusu
2005.01.06, 03:10 AM
It is in that window-- click the icons on the right edge of the window to toggle between icon and list view, then you can examine all of the objects and the connections.

Coin
2005.01.07, 02:29 AM
off subject but how do you clear off an NSBezierPath object?

arekkusu
2005.01.07, 05:41 AM
If you mean removing the NSBezierPath's contents (to reuse the alloc'd object), [NSBezierPath removeAllPoints]. See the documentation.

If you mean clearing the canvas after drawing a NSBezierPath (to animate), you have to erase the view with your background color. See the DotView sample code's drawRect method.

Coin
2005.01.07, 04:49 PM
yea sorry i asked that, i noticed it about .7 seconds after i posted and, forgot to delete/edit it away.