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
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
J'ai commencé à référencer l'exemple du projet p71 et je compile pour iPhoneOS 3.1.2 – Azeworai