• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Custom NSView not redrawing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Custom NSView not redrawing


  • Subject: Custom NSView not redrawing
  • From: Steven E Barnes <email@hidden>
  • Date: Tue, 18 Dec 2007 13:58:30 -0800 (PST)

I am attempting to write a simple game using
Objective-C and Cocoa, currently on Tiger.  I have a
simple game thread using NSThread which looks like
this:

- (void) doAnim {
	NSDate *date = nil;
	@try {
		while (animating) {
			[world advanceClock:ANIM_TICK_INTERVAL];

			[[GameView instance] setNeedsDisplay:YES];

			date = [NSDate
dateWithTimeIntervalSinceNow:ANIM_TICK_INTERVAL];
			[NSThread sleepUntilDate:date];

		}
	}
	@catch(NSException *e) {
		NSLog(@"Caught %@: %@", [e name], [e reason]);
	}
}

GameView is an NSView subclass embedded in a window
created using Interface Builder.

This was working fine, then it stopped working.  I
searched the net for answers and eventually got it
working again (I think by deleting my GameView view
instance from the NIB, reading the header into IB, and
adding it back in).  Then I was cheerfully coding
until it mysteriously stopped working again.

The behavior is: the game state is correctly being
updated, but the view does not change.  If I resize my
window then I can force a redraw and see that my game
objects have moved to the expected positions.  I can
watch the game animate by continually resizing the
window.

I've simplified the drawRect to this, and it still
update properly:

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

I've read that -setNeedsDisplay is not thread-safe,
and I have tried following the advice as described
here:

http://www.cocoabuilder.com/archive/message/cocoa/2005/5/24/136890

Calling -setNeedsDisplay or -display from the Cocoa
main thread does not change the behaviour.  I have
also tried using NSTimer instead of NSThread, to no
avail.

And help will be appreciated,

Steven Barnes
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Prev by Date: Re: Making custom key-commands
  • Next by Date: NSOutlineView or NSTreeController flat list
  • Previous by thread: Re: parsing NSString into an array...
  • Next by thread: RE: [Bulk] Re: Custom NSView not redrawing
  • Index(es):
    • Date
    • Thread