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
galgje.m
drawer.h:
drawer.m
[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
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
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