2010-05-02 12 views
15
CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor); 

que estoy tratando de seguir el libro de O'Reilly, iPhone desarrollo del juego, pero en la página 73 Capítulo 3 me sale este error:¿Cómo acceder a la propiedad CGColor de UIColor en CGContextSetFillColorWithColor?

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

De acuerdo con el libro de errata page se trata de una errata sin confirmar en el libro. ¿Con qué código funcional puede reemplazarse esa línea?

detalles adicionales

El proyecto de ejemplo se pueden descargar here.

me estaba encontrando el error en la función de hacer siguiendo las instrucciones del libro desde la página 72 a la página 73 para construir la clase gsMain (su diferente del pg77 ejemplo de proyecto) en la función de procesamiento de gsMain.m

el fragmento de código que el libro da instrucciones para construir la clase gsMain es como sigue:

//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 

la Chapter3_Example_p77 se supone que muestra el resultado de los ejercicios de la página 71 a 77, pero es muy diferente de las instrucciones dadas en 71 a 77. El siguiente código es la clase compilada completa descargada de la lista anterior enlace.

//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 

Respuesta

6

compilé el ejemplo p77 para Simulador 3.1.3 y no surge algún advertencias del compilador o errores.

El código:

- (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]]]; 

    //... 
} 

aparece para compilar bien. ¿Tal vez podría especificar cuál de los tres ejemplos está compilando y para qué versión del sistema operativo objetivo?

+0

Gracias por correr que compilan. Con tu ayuda, finalmente veo el problema. Estoy compilando una versión que creé siguiendo las instrucciones del libro (en los detalles adicionales de la pregunta). Lo cual se supone que da como resultado el ejemplo p77 que compiló. La función de renderizado en el 'resultado p77' es muy diferente de las instrucciones del libro en p71-p73. Creo que tendré que copiar la clase gsMain de p77 en lugar de seguir las instrucciones en el libro real. – Azeworai

+0

Empecé haciendo referencia al proyecto de ejemplo p71 y estoy compilando para iPhoneOS 3.1.2 – Azeworai

6

Asegúrese de que #include <UIKit/UIKit.h>.

Y es -grayColor, no -greyColor.

+0

Sí, lo tengo gracias. Acabo de copiar el texto para mostrar las instrucciones del libro. – Azeworai

21

¿Qué hay de [ [ UIColor grayColor ] CGColor ]?

¿Está utilizando el último SDK? Supongo que su encabezado UIColor.h tiene una propiedad para CGColor? (Me gustaría comprobar la configuración del SDK - cuando abrí el proyecto de ejemplo se establece en iOS 3.x)


En 3.x Swift: UIColor.gray.cgColor

Cuestiones relacionadas