Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Custom NSView not redrawing



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:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.