setNeedsDisplay

Member
Posts: 102
Joined: 2005.01
Post: #1
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

Code:
#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

Code:
- (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

Code:
- (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.......
Quote this message in a reply
Sage
Posts: 1,231
Joined: 2002.10
Post: #2
Try looking at /Developer/Examples/AppKit/DotView

It demonstrates using an NSView subclass, bezier path drawing, and an NSColorWell, and is heavily commented.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #3
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?
Quote this message in a reply
Member
Posts: 277
Joined: 2004.10
Post: #4
Coin-o Wrote: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. Grin

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

Global warming is caused by hobos and mooses
Quote this message in a reply
Sage
Posts: 1,231
Joined: 2002.10
Post: #5
Coin Wrote: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.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #6
ah, yea ok, i was looing for it in the little window with files owner and first responder
Quote this message in a reply
Sage
Posts: 1,231
Joined: 2002.10
Post: #7
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.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #8
off subject but how do you clear off an NSBezierPath object?
Quote this message in a reply
Sage
Posts: 1,231
Joined: 2002.10
Post: #9
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.
Quote this message in a reply
Member
Posts: 102
Joined: 2005.01
Post: #10
yea sorry i asked that, i noticed it about .7 seconds after i posted and, forgot to delete/edit it away.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  how to call fathers setNeedsDisplay from child. iluzone 5 5,564 Dec 7, 2006 04:11 AM
Last Post: iluzone
  drawRect: is not executed after setNeedsDisplay:YES Moridin 7 6,694 Oct 19, 2005 12:42 AM
Last Post: Moridin