CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor);
我正在尝试关注O'Reilly的书,iPhone游戏开发,但是在第73页第3章我得到了这个错误:
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
答案 0 :(得分:23)
[ [ UIColor grayColor ] CGColor ]
怎么样?
您使用的是最新的SDK吗?我假设您的UIColor.h
标头有CGColor
的属性? (我会检查你的SDK设置 - 当我打开示例项目时,它被设置为iOS 3.x)
在Swift 3.x中:UIColor.gray.cgColor
答案 1 :(得分:6)
我为Simulator 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]]];
//...
}
似乎编译好了。也许您可以指定您正在编译的三个示例中的哪一个以及目标OS版本?
答案 2 :(得分:5)
确保#include <UIKit/UIKit.h>
。
它是-grayColor
,而不是-greyColor
。