2011-11-29 22 views
5

Tôi cần trợ giúp để điền vào đường dẫn của mình. Tôi không có đầu mối nơi để đặt CGContextFillPath(path); ví dụ. Tôi khá mới để vẽ (và iOS phát triển) và tôi thực sự đã thử tất cả mọi thứ trong gần hai giờ. Nhưng tôi đã đạt đến điểm mà tôi chỉ phải từ bỏ ... Hy vọng bạn có thể làm sáng tỏ vấn đề của tôi. Tôi nhận được lỗi sau: invalid contextTôi không thể điền vào CGPath

-(CGMutablePathRef) drawHexagon:(CGPoint)origin 
    { 
     //create mutable path 
     CGMutablePathRef path = CGPathCreateMutable(); 

     CGPathMoveToPoint(path, nil, origin.x, origin.y); 

     CGPoint newloc = CGPointMake(origin.x - 20, origin.y + 42); 
     CGPathMoveToPoint(path, NULL, newloc.x, newloc.y); 
     CGPathAddLineToPoint(path, NULL, newloc.x + 16,newloc.y + 38); 
     CGPathAddLineToPoint(path, NULL, newloc.x + 49, newloc.y + 0); 
     CGPathAddLineToPoint(path, NULL, newloc.x + 23, newloc.y - 39); 
     CGPathAddLineToPoint(path, NULL, newloc.x - 25,newloc.y - 40); 
     CGPathAddLineToPoint(path, NULL, newloc.x -43, newloc.y + 0); 
     CGPathCloseSubpath(path); 
     CGContextAddPath(context, path); 
     context = UIGraphicsGetCurrentContext(); 
     CGContextSaveGState(context); 
     CGContextSetLineWidth(context, 2.0); 
     CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
     CGContextAddPath(context, path); 
     CGContextStrokePath(context); 
     CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
     CGContextFillPath(path); 



      return path; 

    } 

    -(id) init 
    { 
     // always call "super" init 
     // Apple recommends to re-assign "self" with the "super" return value 
     if((self=[super init])) { 

      clickedHexagonsUpToNow = [[NSMutableArray alloc]init ]; 
      CGSize screenSize = [CCDirector sharedDirector].winSize; 

      background = [CCSprite spriteWithFile:@"background.png"]; 
      background.anchorPoint= ccp(0,0); 
      [self addChild:background z:-1 tag:0]; 

      hex1TouchArea = [self drawHexagon:ccp(82,120)]; 
      hex2TouchArea = [self drawHexagon:ccp(50,157)]; 
      hex3TouchArea = [self drawHexagon:ccp(220 ,196)]; 
      hex4TouchArea = [self drawHexagon:ccp(280,153)]; 
      hex5TouchArea = [self drawHexagon:ccp(84,118)]; 
      hex6TouchArea = [self drawHexagon:ccp(82,120)]; 
      hex7TouchArea = [self drawHexagon:ccp(82,120)]; 
      hex8TouchArea = [self drawHexagon:ccp(82,120)]; 
      hex9TouchArea = [self drawHexagon:ccp(82,120)]; 


      self.isTouchEnabled = YES; 

      } 
    return self; 
} 

Trả lời

7

Cần tham số CGContext làm tham số chứ không phải CGPath.

CGContextFillPath(context); 

Tuy nhiên, cuộc gọi tới CGContextStrokePath xóa đường dẫn hiện tại, vì vậy CGContextFillPath sẽ không làm gì cả. Để đột quỵ và điền trên cùng một con đường, bạn nên sử dụng CGContextDrawPath:

... 
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor); 
    CGContextAddPath(context, path); 
// CGContextStrokePath(context); 
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor); 
// CGContextFillPath(path); 
    CGContextDrawPath(context, kCGPathFillStroke); 

(và cũng có thể áp dụng @Luiz của sự thay đổi về nhận được ngữ cảnh.)

+0

Xin lỗi vì câu hỏi ngớ ngẩn (có thể), nhưng 'k' ở dòng cuối cùng làm gì ở đó? – 11684

+0

Đã trả lời câu hỏi của riêng tôi! – 11684

+0

Điều này chỉ vẽ màu đột quỵ, không bao giờ tô màu ... – jjxtra

1

Bạn cần phải nhận được bối cảnh trước khi thêm một đường dẫn đến nó:

context = UIGraphicsGetCurrentContext();    
CGContextAddPath(context, path); 

Ngoài ra, để điền vào đường dẫn, bạn cần phải vượt qua tham chiếu bối cảnh, không phải là con đường riêng của mình:

CGContextFillPath(context); 
+0

Tôi vẫn không thể lấp đầy nó ... :( –

Các vấn đề liên quan