Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

marcelb

macrumors newbie
Original poster
Apr 5, 2007
6
0
Netherlands
Hello guys,

I'm new to the world of programming in mac.
En after lots of reading I decided to write my own program.
It's a program where you can fill in a word, and another person has to guess letters or the whole word.
And if your guessing wrong then NSView draws an image, if the image is finished the game is ended en your Game Over.

But it won't work.
The individual classes are working great, but not together.
I think the problem is in the way I included the class en called the redraw function.

The code:
galgje.h
Code:
/* Galgje */

#import <Cocoa/Cocoa.h>
#import "Drawer.h"

@interface Galgje : NSObject
{
    IBOutlet id galgjeWoord;
    IBOutlet id geradenLetters;
    IBOutlet id gokWoord;
    IBOutlet id message;
    IBOutlet id raadLetter;
    IBOutlet id woord;
	//I know that I have to declare the class Drawer in some way but I don't know how!
	
	//declaring variables
	NSString *galgje;
	NSMutableString *dots;
	int count;
	NSMutableString *letters;
}
- (IBAction)Gok:(id)sender;
- (IBAction)Raad:(id)sender;
- (IBAction)Reset:(id)sender;
- (IBAction)Set:(id)sender;
@end
galgje.m
Code:
#import "Galgje.h"

@implementation Galgje

- (IBAction)Gok:(id)sender
{

	NSRange range = [galgje rangeOfString: [gokWoord stringValue]];
	int location = range.location;
	int length	 = range.length;
	

	
	if(length > 0 ){
		[letters appendString:[[NSString alloc] initWithString: [gokWoord stringValue]]];
		[geradenLetters setStringValue:letters];
		[dots replaceCharactersInRange: NSMakeRange(location, length) withString:[[NSString alloc] initWithString: [gokWoord stringValue]]];
		[woord setStringValue:dots];
		[message setStringValue:@"Goed geraden!"];
	
	}else{
	
		
		[letters appendString:[[NSString alloc] initWithString: [gokWoord stringValue]]];
		[geradenLetters setStringValue:letters];
		[woord setStringValue:dots];
		[message setStringValue:@"Helaas verkeerd gegokt"];
		count++;
		[[Drawer alloc] redraw:count];
	
		
	}


}

- (IBAction)Raad:(id)sender
{

	NSRange range = [galgje rangeOfString: [raadLetter stringValue]];
	int location = range.location;
	int length	 = range.length;
	

	
	if(length > 0 && length <= 1){
		[letters appendString:[[NSString alloc] initWithString: [raadLetter stringValue]]];
		[geradenLetters setStringValue:letters];
		[dots replaceCharactersInRange: NSMakeRange(location, length) withString:[[NSString alloc] initWithString: [raadLetter stringValue]]];
		[woord setStringValue:dots];
		[message setStringValue:@"Goed geraden!"];
	
	}else{
	
		
		[letters appendString:[[NSString alloc] initWithString: [raadLetter stringValue]]];
		[geradenLetters setStringValue:letters];
		[woord setStringValue:dots];
		[message setStringValue:@"Helaas verkeerd gegokt"];
		count++;
		[[Drawer alloc] redraw:count];
		
	}
	




}

- (IBAction)Reset:(id)sender
{

/*	galgje = [[NSString alloc] initWithString:@""];
	count = 0;
	letters = [[NSMutableString alloc] initWithString:@""];
	[galgjeWoord setStringValue:@""];
	[message setStringValue:@"Welkom bij galgje"];
	*/
	
	[galgjeWoord setStringValue:@"Resetteeen!!!"];

}

- (IBAction)Set:(id)sender
{

	galgje = [[NSString alloc] initWithString: [galgjeWoord stringValue]]; 
	count = 0;
	letters = [[NSMutableString alloc] init];
	NSMutableString *tmp = [[NSMutableString alloc] init];
	
	NSRange range2 = [galgje rangeOfString:[galgjeWoord stringValue]];
	int length2 = range2.length;
	int x;
	for ( x = 0; x < length2; x++ ){
		
		[tmp appendString:@"."];
		
	}
	
		dots = [[NSMutableString alloc] initWithString: tmp];
		
		[galgjeWoord setStringValue:@""];


}

@end


drawer.h:
Code:
/* Drawer */

#import <Cocoa/Cocoa.h>

@interface Drawer : NSView
{

int count;

}

-(void)redraw:(int)x;
@end

drawer.m
Code:
#import "Drawer.h"

@implementation Drawer

- (id)initWithFrame:(NSRect)frameRect
{
	if ((self = [super initWithFrame:frameRect]) != nil) {
		// Add initialization code here
	}
	return self;
}

- (void)drawRect:(NSRect)rect
{

if(count == 0){
//setup basic size and color properties
	//setup basic size and color properties
	float dm = 81 * 2;
	float rd = dm * 0.5;
	float qt = dm * 0.25;
	
	NSColor * white = [NSColor whiteColor];
	NSColor * black = [NSColor blackColor];
	
	NSBezierPath *path1, *path2, *path3, *path4;
	
	//find the center
	float center = [self bounds].size.width * 0.50;
	float middle = [self bounds].size.height * 0.50;
	
	//create a rect in the center
	NSPoint origin		= { center - rd, middle - rd};
	NSRect  mainOval	= { origin.x, origin.y, dm, dm};
	
	//create a oval bezier path using the rect
	path1 = [NSBezierPath bezierPathWithOvalInRect: mainOval];
	[path1 setLineWidth:2.18];
	
	//draw the path
	[white set];[path1 fill];
	[black set];[path1 stroke];
	
	//overlay a new path to draw the right side
	path2 = [NSBezierPath bezierPath];
	
	//arc from the center to construct right side
	NSPoint mainOvalCenter = { center, middle};
	[path2 appendBezierPathWithArcWithCenter: mainOvalCenter
									radius	: rd
								startAngle	: 90
								endAngle	: 270
								clockwise	: YES];
								
	//add a half-size arc at the top: counter clockwise
	NSPoint curveOneCenter	= { center, origin.y+qt };
	[path2 appendBezierPathWithArcWithCenter: curveOneCenter
									radius	: qt
								startAngle	: 270
								endAngle	: 90
								clockwise	: NO];
	
	//add a half-size arc in the bottom half
	NSPoint curveTwoCenter = { center, origin.y+rd+qt};
	[path2 appendBezierPathWithArcWithCenter: curveTwoCenter
									radius	: qt
								startAngle	: 270
								endAngle	: 90
								clockwise	: YES];
	
	//fil the path on the right side with black
	[black set];[path2 fill];
	
	//calculate the size for each ellipses
	float dotDm = ( qt * 0.618 );
	float dotRd = ( dotDm * 0.5 );
	
	//create a rect at the center of the top selection
	NSRect rect3 = NSMakeRect ( center - dotRd,
								origin.y +  ( qt - dotRd),
								dotDm,
								dotDm );
	
	//create an oval with the rect and fill it with black
	path3 = [NSBezierPath bezierPathWithOvalInRect: rect3];
	[black set]; [path3 fill];
	
	//copy the rect for the top ellipse and adjust the y axis
	NSRect rect4 = rect3;
	rect4.origin.y = NSMaxY(mainOval) -  ( qt + dotRd);
	
	// create an oval with the rect and fill it with white
	path4 = [NSBezierPath bezierPathWithOvalInRect: rect4];
	[white set];[path4 fill];
}else{

}


}

-(void)redraw:(int)x
{

	count = x;
	[self setNeedsDisplay:YES];

}

@end

[edit]
//that's the way I called the method redraw in the method Raad in the class galgje
[Drawer redraw:count];
//the correct code
[[Drawer alloc] redraw:count];
[/edit]

I hope you can help me.

Greetz

Marcel
 

HiRez

macrumors 603
Jan 6, 2004
6,250
2,576
Western US
First of all, you really need to post your code inside
Code:
 tags. Or at the very least turn off check the "Disable smilies in text" box when posting. It is very hard to read and the indentation also does not get shown.

Then, when you subclass NSView, you want to override the method [FONT="Courier New"]-(void)drawRect:(NSRect)aRect;[/FONT]. That's where Cocoa does its drawing of an NSView, and where you do yours if you subclass it. I can't really tell if you call that somewhere else in your code.
 

marcelb

macrumors newbie
Original poster
Apr 5, 2007
6
0
Netherlands
I'm sorry, I edited my post with the code tags.
But the problem was when I launched my program, the graphics were already drawn. And that means you lost the game.
To solve this problem I build a counter, every time when you guess a letter the counter will raise with 1, only if the guess is wrong.
En I post the counter value to the method -(void)redraw: (int)x;
And send a setNeedsDisplay:Yes message so the drawer knows that he must redraw.
The x value has been copied to the count variable that can be accessed by the draw method.
When the draw method has been recalled because I've set the setNeedsDisplay then the counter indicates the steps to be drawn.
I don't know any better solutions to solve the problem.
 

marcelb

macrumors newbie
Original poster
Apr 5, 2007
6
0
Netherlands
Finally I know how to call the redraw function, I didn't load the class in the memory....
But the next problem is when I call the redraw function the output is :NSView not correctly initialized. Did you forget to call super?
And I don't know what to do with this message, if tried lots of things
[super setNeedsDisplay:YES];
[[NSView alloc] setNeedsDisplay:YES];
[[Drawer alloc] setNeedsDisplay:YES];
and more....
But nothing is working, and I'm getting crazy because it won't work.
I hope somebody can help me with this problem!?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.