Having issues with a custom class
Having issues with a custom class
- Subject: Having issues with a custom class
- From: Geoffrey Lessel <email@hidden>
- Date: Wed, 2 Mar 2005 00:24:26 -0600
Greetings to the list. I've been on and off (mostly off) this list for
a couple of years now and finally have a question! =)
I am currently going through a book on AI. I started coding in Perl,
moved to PHP and finally decided that I wanted to move the coding I was
doing to Cocoa. I know it's a strange progression of languages, but
that's what it's been -- strange. I have worked in Cocoa quite a bit
before but have never implemented my own classes. This is also
something I am beginning to learn.
The program that I am trying to get running at this point is a simple
chase-and-evade type AI program. The code seems to work just fine when
I just export the variables to NSLog() and have the program run in
main() with a while(targetNotCaught) loop. The problem comes when I try
to give the code some graphics. The code snippets that I will paste
below keep giving me crashes sending signal 10 or 11, depending on
something that I can't figure out. I have done some looking and
querying and can only surmise that the problem is one of memory. When I
look at the variables that are still available at the crash, the
numbers I used during initialization were not there.
Here's some code. I have just put in what I think you would need to see
what's going on. If you need more, please let me know. If you need less
(or much less), please go easy on the noob. =)
################
in CocoaView.h
/* CocoaView */
#import <Cocoa/Cocoa.h>
#import "flyingObject.h"
@interface CocoaView : NSView
{
int i;
NSRect airplaneDrawing, missileDrawing;
NSBezierPath *airplaneBall, *missileBall;
flyingObject *airplane, *missile;
}
- (IBAction)startSim:(id)sender;
- (void)updateView;
- (void)stepAnimation:(NSTimer *)timer;
@end
###########################
In CocoaView.m
@implementation CocoaView
- (id)initWithFrame:(NSRect)frameRect
{
NSTimer *timer;
if ((self = [super initWithFrame:frameRect]) != nil) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
airplane = [[flyingObject alloc] init];
[airplane setPosition:[NSNumber numberWithFloat:0] Y:[NSNumber
numberWithFloat:0]];
[airplane setSpeed:[NSNumber numberWithFloat:1]];
[airplane setHeadingByDegree:[NSNumber numberWithFloat:45]];
[airplane setMaxTurnSpeedByDegree:[NSNumber numberWithFloat:10]];
missile = [[flyingObject alloc] init];
// For space, I'll just say that it is initialized exactly the same
as airplane with different numbers
timer = [NSTimer scheduledTimerWithTimeInterval:0.04 target:self
selector:@selector(stepAnimation:) userInfo:nil repeats:YES];
}
return self;
}
- (void) stepAnimation:(NSTimer *)timer {
// it gets to the next line (NSLog()) and crashes during [airplane
getXPosition]
NSLog(@"airplane: (%.3f, %.3f)", [[airplane getXPosition]
floatValue], [[airplane getYPosition] floatValue]);
[airplane movePositionBasedOnVelocity];
[missile aimForObject:airplane];
[missile movePositionBasedOnVelocity];
[self setNeedsDisplay:YES];
}
################
So after the values in [airplane] and [missile] have been set in the
initWithFrame (which, according to some placements of NSLog(), I can
see that they are correctly), once the program moves to the next
function, in this case stepAnimation, [airplane] no longer has its
variables. I know it has something to do with the way I am creating or
storing those objects, but for the life of me, I can't figure it out. I
thought that since the variables were declared in CocoaView.h, they
would be globally accessable by all functions in CocoaView.m.
Any insights?
Thanks so much for reading all my presumably nasty code.
-geoffrey
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden