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

Je suis en train de suivre le livre de O'Reilly, iPhone Game Development, mais à la page 73 Chapitre 3 J'obtiens cette erreur:Comment accéder à la propriété CGColor de UIColor dans CGContextSetFillColorWithColor?

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

Selon errata page c'est un errata non confirmé de livre dans la livre. Quel code fonctionnel peut-on remplacer avec cette ligne?

Détails supplémentaires

L'exemple de projet peut être téléchargé here.

Je rencontrais l'erreur à la rendre fonction en suivant les instructions du livre de la page 72 à la page 73 pour construire la classe gsMain (son différent de l'exemple de projet pg77) à la fonction render de gsMain.m

l'extrait de code que le livre instruit de construire la classe gsMain est comme suit:

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

le Chapter3_Example_p77 est censé montrer le résultat des exercices de la page 71 à 77, mais il est très différent des instructions données dans 71 à 77. Le code suivant est la classe complétée et compilable téléchargée à partir de ce qui précède lien.

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

Répondre

6

Je compilé l'exemple p77 Simulator 3.1.3 et n'a pas couru dans tous les avertissements du compilateur ou des erreurs.

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

    //... 
} 

semble compiler bien. Peut-être pourriez-vous spécifier lequel des trois exemples vous compilez et pour quelle version de système d'exploitation cible?

+0

Merci pour la course qui compilent. Avec votre aide, je vois enfin le problème. Je compile une version que j'ai créée en suivant les instructions du livre (dans les détails supplémentaires de la question). Ce qui est supposé aboutir à l'exemple p77 que vous avez compilé. La fonction de rendu dans le 'résultat p77' est très différente des instructions du livre dans p71-p73. Je pense que je vais devoir copier la classe gsMain de p77 au lieu de suivre les instructions du livre. – Azeworai

+0

J'ai commencé à référencer l'exemple du projet p71 et je compile pour iPhoneOS 3.1.2 – Azeworai

6

Assurez-vous de #include <UIKit/UIKit.h>.

Et c'est -grayColor, pas -greyColor.

+0

Yep a eu ça merci. Je viens de copier le texte pour montrer les instructions du livre. – Azeworai

21

Qu'en est-il de [ [ UIColor grayColor ] CGColor ]?

Utilisez-vous le dernier SDK? Je suppose que votre UIColor.h en-tête a une propriété pour CGColor? (Je voudrais vérifier vos paramètres SDK - quand j'ai ouvert l'exemple de projet, il a été réglé sur iOS 3.x)


3.x Swift: UIColor.gray.cgColor