Why won't my dot move?

nmartin
Unregistered
 
Post: #1
I'm teaching myself cocoa, and I'm trying to do some very simple animation.

I've made a few random circles appear in a window. Now I want one of them to move.

In my view, I first initialize the objects, and add a timer:
Code:
- (id)initWithFrame:(NSRect)frame
{
    size_t k;
    SmartObject *obstacle;
    NSColor *color;
    self = [super initWithFrame:frame];
    if (self == nil)
    return nil;
    
    int w = frame.size.width;
    int h = frame.size.height;
    srand(time(NULL));
    obstacles = [[NSMutableArray alloc] initWithCapacity:OBSTACLE_COUNT];
    for (k = 0; k < OBSTACLE_COUNT; k++) {
    color = [NSColor redColor];
    obstacle = [[SmartObject alloc] init];
    [obstacle setRadius:OBSTACLE_RADIUS];
        
        float sx = rand() % w;
        float sy = rand() % h;
        center.x = sx;
        center.y = sy;
        [obstacle setColor:color];
    [obstacle setCenter:center];
    [obstacles addObject:obstacle];
    [obstacle release];
    }

        color = [NSColor blueColor];
    dot = [[SmartDot alloc] init];
    [dot setColor:color];
    [dot setRadius:DOT_RADIUS];
        float sx = rand() % w;
        float sy = rand() % h;
        center.x = sx;
        center.y = sy;
    [dot setCenter:center];
    [self setNeedsDisplay:YES];
    centralTimer = [[NSTimer scheduledTimerWithTimeInterval:0.1
        target:dot
        selector:@selector(move:)
        userInfo:nil
        repeats:YES] retain];
    
    return self;
}

And I also have a drawrect method:
Code:
- (void)drawRect:(NSRect)rect {
    NSRect bounds;
    SmartObject *obstacle;
    size_t k, count;
    CGContextRef context;

    context = [[NSGraphicsContext currentContext] graphicsPort];

    CGContextSetRGBFillColor(context, 0.7, 0.7, 0.9, 1);


    count = [obstacles count];
    for (k = 0; k < count; k++) {
    obstacle = [obstacles objectAtIndex:k];
    bounds = [self boundsForObstacle:obstacle];
    if (NSIntersectsRect(bounds, rect))
        [obstacle draw];
    }
    bounds = [self boundsForObstacle:dot];
    if (NSIntersectsRect(bounds, rect))
    [dot draw];
}

In my "dot" class, I have the method that the timer calls:
Code:
- (void)move:(id)sender
{
   _center.x+=1;
   [view setNeedsDisplay:YES];
}

It displays all the dots correctly, and "move:" is getting called periodically, but it never goes back to drawrect after the first time. I thought the setNeedsDisplay:YES makes drawrect get called again. Am I missing something here?

Thanks!
Quote this message in a reply
Post Reply 


Messages In This Thread
Why won't my dot move? - nmartin - Nov 29, 2003 03:34 AM
Why won't my dot move? - Fenris - Nov 29, 2003, 05:06 AM
Why won't my dot move? - codemattic - Nov 29, 2003, 05:21 AM
Why won't my dot move? - DoG - Nov 29, 2003, 10:34 AM
Why won't my dot move? - nmartin - Nov 29, 2003, 12:11 PM
Why won't my dot move? - Fenris - Nov 29, 2003, 02:49 PM
Why won't my dot move? - nmartin - Nov 29, 2003, 03:20 PM
Why won't my dot move? - Fenris - Nov 29, 2003, 04:07 PM
Why won't my dot move? - nmartin - Nov 30, 2003, 01:44 AM
Why won't my dot move? - Entropy - Nov 30, 2003, 02:43 AM
Why won't my dot move? - DoG - Nov 30, 2003, 08:59 AM
Why won't my dot move? - nmartin - Nov 30, 2003, 10:30 AM
Why won't my dot move? - Fenris - Nov 30, 2003, 04:00 PM
Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  simple function to move an object not doing as wanted burrows111 11 3,794 Apr 16, 2010 11:33 PM
Last Post: StealthyCoin
  My pongball won't move!! clapton541 1 1,706 Jan 13, 2007 10:27 AM
Last Post: clapton541