views:

896

answers:

2

CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor);

I'm trying to follow O'Reilly's iPhone Game Development book on page 73 Chapter 3 but I'm running into a compile error that states

error: request for member 'CGColor' in something not a structure or union

According to the book's errata page this is an unconfirmed errata in the book. I'm hoping that someone could help me replace that line of code to something that works. The example project can be downloaded from sourceforge at this link.

--Additional Details--

I was encountering the error at the render function by following the book's instructions from page 72 to page 73 to build the gsMain class (its different from the example project pg77) at the render function of gsMain.m

The code snippet that the book instructs to build gsMain class is as followed:

//gsMain.h
@interface gsTest : GameState { }  
@end

//gsMain.m 
@implementation gsMain 

-(gsMain*) initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager 

    { 
        if (self = [super initWithFrame:frame andManager:pManager]) { 
        NSLog(@"gsTest init"); 
    } 
return self; 
} 

-(void) Render 
{ 
    CGContextRef g = UIGraphicsGetCurrentContext(); 
    //fill background with gray 
    CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor); //Error Occurs here
    CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, 
    self.frame.size.height)); 
//draw text in black 
CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor); 
[@"O'Reilly Rules!" drawAtPoint:CGPointMake(10.0,20.0) 
        withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]]; 
} 
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    UITouch* touch = [touches anyObject]; 
    NSUInteger numTaps = [touch tapCount]; 
    //todo: implement touch event code here 
} 
@end

The Chapter3_Example_p77 is supposed to show the result of the exercises from page 71 to 77 but it's very different from the given instructions given in 71 to 77. The following code is the completed, compilable class downloaded from the above link.

//gsMain.h
#import <Foundation/Foundation.h>
#import "GameState.h"
@interface gsMain : GameState {

}

@end

//  gsMain.m
//  Example
//  Created by Joe Hogue and Paul Zirkle

#import "gsMain.h"
#import "gsTest.h"
#import "Test_FrameworkAppDelegate.h"

@implementation gsMain

-(gsMain*) initWithFrame:(CGRect)frame andManager:(GameStateManager*)pManager {
if (self = [super initWithFrame:frame andManager:pManager]) {
    //do initializations here.
}
return self;
}

- (void) Render {
[self setNeedsDisplay]; //this sets up a deferred call to drawRect.
}

- (void)drawRect:(CGRect)rect {
CGContextRef g = UIGraphicsGetCurrentContext();
//fill background with gray
CGContextSetFillColorWithColor(g, [UIColor grayColor].CGColor);
CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
//draw text in black.
CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor);
[@"O'Reilly Rules!" drawAtPoint:CGPointMake(10.0, 20.0) withFont:
    [UIFont systemFontOfSize:   [UIFont systemFontSize]]];

//fps display from page 76 of iPhone Game Development
int FPS = [((Test_FrameworkAppDelegate*)m_pManager) getFramesPerSecond];
NSString* strFPS = [NSString stringWithFormat:@"%d", FPS];
[strFPS drawAtPoint:CGPointMake(10.0, 60.0) withFont:[UIFont systemFontOfSize:
    [UIFont systemFontSize]]];
}

//this is missing from the code listing on page 77.
-(void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
    UITouch* touch = [touches anyObject];
    NSUInteger numTaps = [touch tapCount];
    if( numTaps > 1 ) {
    [m_pManager doStateChange:[gsTest class]];
}
}

@end
+1  A: 

I compiled the p77 example for Simulator 3.1.3 and did not run into any compiler warnings or errors.

The code:

- (void)drawRect:(CGRect)rect {
    CGContextRef g = UIGraphicsGetCurrentContext();
    //fill background with gray
    CGContextSetFillColorWithColor(g, [UIColor grayColor].CGColor);
    CGContextFillRect(g, CGRectMake(0, 0, self.frame.size.width, self.frame.size.height));
    //draw text in black.
    CGContextSetFillColorWithColor(g, [UIColor blackColor].CGColor);
    [@"O'Reilly Rules!" drawAtPoint:CGPointMake(10.0, 20.0) withFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];

    //...
}

appears to compile okay. Perhaps you might specify which of the three examples you're compiling and for what target OS version?

Alex Reynolds
Thanks for running that compile. With your help I finally see the problem. I'm compiling a version that I created following the book's instructions(in the additional details of the question). Which is supposed to result in the p77 example that you've compiled. The render function in the 'p77 result' is very different from the book's instructions in p71-p73. I think I'll just have to copy p77's gsMain class instead of following the instructions on the actual book.
Azeworai
I started off referencing the example p71 project and I'm compiling for iPhoneOS 3.1.2
Azeworai
+2  A: 

Make sure you #include <UIKit/UIKit.h>.

And it is -grayColor, not -greyColor.

KennyTM
Yep got that thanks. I just copied off the text to show the book's instructions.
Azeworai