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

मैं ओ रेली की किताब, iPhone खेल विकास का पालन करने की कोशिश कर रही है, लेकिन पृष्ठ पर 73 अध्याय 3 मैं इस त्रुटि मिलती है: पुस्तक का errata page को यह में एक अपुष्ट शुद्धिपत्र है मुताबिकCGContextSetFillColorWithColor में UIColor की CGColor प्रॉपर्टी तक कैसे पहुंचे?

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

किताब। उस लाइन को किस कार्यात्मक कोड से बदला जा सकता है?

अतिरिक्त विवरण

उदाहरण परियोजना here डाउनलोड किया जा सकता।

मैं पेज 73 के लिए पृष्ठ 72 से पुस्तक के निर्देशों के gsMain.m

की

समारोह प्रस्तुत करना पर gsMain वर्ग (उदाहरण के परियोजना pg77 से अपनी अलग) का निर्माण करने के द्वारा समारोह प्रस्तुत करना पर त्रुटि का सामना कर रहा था कोड स्निपेट उस किताब gsMain वर्ग का निर्माण करने का निर्देश का पालन किया है:

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

Chapter3_Example_p77 77 के पेज 71 से अभ्यास के परिणाम दिखाने के लिए माना जाता है, लेकिन यह करने के लिए 71 में दी गई दिए गए निर्देशों से बहुत अलग है 77. निम्नलिखित कोड उपरोक्त से डाउनलोड पूर्ण, संकलित वर्ग है संपर्क।

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

उत्तर

6

मैं सिम्युलेटर 3.1.3 के लिए p77 उदाहरण संकलित और किसी भी संकलक चेतावनियों या त्रुटियों में चलाने नहीं किया।

कोड:

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

    //... 
} 

ठीक संकलित करने के लिए प्रकट होता है। शायद आप निर्दिष्ट कर सकते हैं कि आप किस तीन उदाहरण संकलित कर रहे हैं और किस लक्ष्य ओएस संस्करण के लिए?

+0

धन्यवाद। आपकी मदद से मैं अंत में समस्या देखता हूं। मैं एक संस्करण संकलित कर रहा हूं जिसे मैंने पुस्तक के निर्देशों के बाद बनाया है (प्रश्न के अतिरिक्त विवरण में)। जिसे पी77 उदाहरण में संकलित किया गया है जिसे आपने संकलित किया है। 'P77 परिणाम' में रेंडर फ़ंक्शन P71-p73 में पुस्तक के निर्देशों से बहुत अलग है। मुझे लगता है कि मुझे वास्तविक पुस्तक पर निर्देशों का पालन करने के बजाय सिर्फ पी77 के जीएस मेन क्लास की प्रतिलिपि बनाना होगा। – Azeworai

+0

मैंने उदाहरण पी 71 प्रोजेक्ट को संदर्भित करना शुरू कर दिया है और मैं आईफोनओएस 3.1.2 – Azeworai

6

सुनिश्चित करें कि आप #include <UIKit/UIKit.h>

और यह -grayColor है, -greyColor नहीं है।

+0

के लिए संकलित कर रहा हूं, धन्यवाद, धन्यवाद। मैंने बस किताब के निर्देश दिखाने के लिए पाठ को कॉपी किया। – Azeworai

21

[ [ UIColor grayColor ] CGColor ] के बारे में क्या?

क्या आप नवीनतम एसडीके का उपयोग कर रहे हैं? मुझे लगता है कि आपके UIColor.h शीर्षलेख के पास CGColor है? (मैं अपने एसडीके सेटिंग्स की जाँच होगा - जब मैं नमूना परियोजना यह आईओएस 3.x के लिए स्थापित किया गया था खोला)


स्विफ्ट 3.x में: कि संकलन चलाने के लिए UIColor.gray.cgColor

संबंधित मुद्दे