Moridin
2005.09.01, 07:46 AM
Hi,
this is my first try at cocoa graphics programming. I have, however, already had a look at DotView and I managed to rewrite it, so that it got to become a kind of painting programm.
This time, I wanted to create a kind of snake game - from scratch. I have put a custom View in my window and created a class MyView for it. Below is the implementation of this class - all methods are also declared in the MyView.h.
The problem is: When I resize the window, drawRect is executed. This is okay. But I have a timer, that calls a method "redrawIt", which does a [self setNeedDisplay:YES]. This method redrawIt is correctly called, but drawRect is not. So i tried it with a button and an action "stepForward", which should make the view redraw itself just one time. It does not.
I have no idea what´s wrong. Please help me!!
P.S.: Of course my class is a subclass of NSView.
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(NSRect)frameRect
{
[super initWithFrame:frameRect];
snake.x = 5.0;
snake.y = 5.0;
radius = 1.0;
color = [[NSColor redColor] retain];
direction = 'R';
return self;
}
- (BOOL)isOpaque {
return YES;
}
- (void)drawRect:(NSRect)rect
{
NSLog (@"Drawing...");
// removed the code - I would be happy if I would just just see the log output
}
- (IBAction)stepForward:(id)sender{
[self setNeedsDisplay:YES];
NSLog(@"Stepping...");
}
- (void)redrawIt{
[self setNeedsDisplay:YES];
NSLog (@"redrawing %@...",self);
}
- (IBAction)start:(id)sender
{
NSLog (@"Started timer...");
timer = [[NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(redrawIt)
userInfo:nil
repeats:YES]retain];
}
- (IBAction)stop:(id)sender
{
[timer invalidate];
[timer release];
}
@end
this is my first try at cocoa graphics programming. I have, however, already had a look at DotView and I managed to rewrite it, so that it got to become a kind of painting programm.
This time, I wanted to create a kind of snake game - from scratch. I have put a custom View in my window and created a class MyView for it. Below is the implementation of this class - all methods are also declared in the MyView.h.
The problem is: When I resize the window, drawRect is executed. This is okay. But I have a timer, that calls a method "redrawIt", which does a [self setNeedDisplay:YES]. This method redrawIt is correctly called, but drawRect is not. So i tried it with a button and an action "stepForward", which should make the view redraw itself just one time. It does not.
I have no idea what´s wrong. Please help me!!
P.S.: Of course my class is a subclass of NSView.
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(NSRect)frameRect
{
[super initWithFrame:frameRect];
snake.x = 5.0;
snake.y = 5.0;
radius = 1.0;
color = [[NSColor redColor] retain];
direction = 'R';
return self;
}
- (BOOL)isOpaque {
return YES;
}
- (void)drawRect:(NSRect)rect
{
NSLog (@"Drawing...");
// removed the code - I would be happy if I would just just see the log output
}
- (IBAction)stepForward:(id)sender{
[self setNeedsDisplay:YES];
NSLog(@"Stepping...");
}
- (void)redrawIt{
[self setNeedsDisplay:YES];
NSLog (@"redrawing %@...",self);
}
- (IBAction)start:(id)sender
{
NSLog (@"Started timer...");
timer = [[NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:@selector(redrawIt)
userInfo:nil
repeats:YES]retain];
}
- (IBAction)stop:(id)sender
{
[timer invalidate];
[timer release];
}
@end