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