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

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

      [[GameView instance] setNeedsDisplay:YES];

      date = [NSDate
      [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

    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

    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

    And help will be appreciated,

    Steven Barnes
previous month december 2007 next month
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today