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

我試圖按照O'Reilly的書,iPhone遊戲開發,但第70頁第3章我得到這個錯誤:如何訪問CGContextSetFillColorWithColor中的UIColor的CGColor屬性?

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

按照書上的errata page這是在一個未經證實的勘誤書。該行可以替換哪些功能代碼?

其他細節

示例項目可以下載here

我是按照從72頁書的指示,第73頁在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是爲了顯示從71頁的練習77的結果,但它是從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

我編譯p77例如,對於模擬器3.1.3,並沒有遇到任何編譯器警告或錯誤。

代碼:

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

感謝運行編譯。在你的幫助下,我終於看到了問題。我正在編寫一個我按照本書的說明創建的版本(在問題的其他細節中)。這應該會導致您編譯的p77示例。 「p77結果」中的渲染功能與本書在p71-p73中的說明非常不同。我想我只需要複製p77的gsMain類,而不是按照實際書中的說明操作。 – Azeworai 2010-05-02 10:08:23

+0

我開始引用示例p71項目,並且正在編譯iPhoneOS 3.1.2 – Azeworai 2010-05-02 10:13:14

6

確保你的#include <UIKit/UIKit.h>

它是-grayColor而不是-greyColor

+0

是的,謝謝。我只是複製了文本以顯示本書的說明。 – Azeworai 2010-05-02 12:25:15

21

[ [ UIColor grayColor ] CGColor ]怎麼樣?

您是否使用最新的SDK?我假設你的UIColor.h頭有一個CGColor的屬性? (我會檢查你的SDK設置 - 當我打開它被設置到iOS 3.x的示例項目)


在斯威夫特3.X:UIColor.gray.cgColor