CGContextSetFillColorWithColor(g, [UIColor greyColor].CGColor);
Tôi đang cố gắng làm theo cuốn sách của O'Reilly, iPhone Phát triển trò chơi, nhưng trên trang 73 Chương 3 Tôi nhận được lỗi này:Cách truy cập thuộc tính CGColor của UIColor trong CGContextSetFillColorWithColor?
error: request for member 'CGColor' in something not a structure or union
Theo cuốn sách của errata page đây là một errata chưa được xác nhận trong sách. Có thể thay thế dòng mã chức năng nào bằng?
Chi tiết bổ sung
Ví dụ dự án có thể được tải về here.
tôi đã gặp phải lỗi tại render chức năng bằng cách làm theo hướng dẫn của cuốn sách từ trang 72 đến trang 73 để xây dựng các lớp gsMain (khác nhau của nó từ pg77 dụ dự án) tại render chức năng của gsMain.m
đoạn mã rằng cuốn sách chỉ thị để xây dựng lớp gsMain là như sau:
//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
các Chapter3_Example_p77 là nghĩa vụ phải hiển thị các kết quả của các bài tập từ trang 71-77 nhưng nó rất khác so với những hướng dẫn được đưa ra trong 71 đến 77. Mã sau đây là lớp hoàn chỉnh, có thể biên dịch được tải xuống từ phần trên liên kết.
//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
Nhờ mềm dùng để chạy mà biên dịch. Với sự giúp đỡ của bạn, tôi cuối cùng đã thấy vấn đề. Tôi đang biên soạn một phiên bản mà tôi đã tạo theo hướng dẫn của cuốn sách (trong các chi tiết bổ sung của câu hỏi). Mà được cho là kết quả trong ví dụ p77 mà bạn đã biên soạn. Hàm render trong 'kết quả p77' rất khác với hướng dẫn của sách trong p71-p73. Tôi nghĩ rằng tôi sẽ phải sao chép lớp gsMain của p77 thay vì làm theo hướng dẫn trên cuốn sách thực tế. – Azeworai
Tôi bắt đầu tham khảo dự án ví dụ p71 và tôi đang biên soạn cho iPhoneOS 3.1.2 – Azeworai