CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor);我试图遵循O‘’Reilly的书,iPhone游戏开发,但在第73页第3章我得到了这个错误:
error: request for member 'CGColor' in something not a structure or union根据这本书的errata页面,这是书中一个未经证实的错误。那行可以用什么功能代码替换呢?
附加细节
示例项目可以下载这里。
我在呈现函数中遇到了错误,我按照书中从第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
}
@endChapter3_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发布于 2013-12-31 09:05:12
那[ [ UIColor grayColor ] CGColor ]呢
您正在使用最新的SDK吗?我假设您的UIColor.h头具有CGColor的属性?(我会检查您的SDK设置-当我打开示例项目时,它被设置为iOS 3.x)
在Swift 3.x:UIColor.gray.cgColor中
发布于 2010-05-02 09:01:52
我为Simator3.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版本?
发布于 2010-05-02 10:05:43
确保你的#include <UIKit/UIKit.h>。
这是-grayColor,而不是-greyColor。
https://stackoverflow.com/questions/2752898
复制相似问题