2011-11-29 14 views
5

私のパスを埋めるための助けが必要です。私は例えばCGContextFillPath(path);をどこに入れるのか分かりません。私は描画(とiOSの開発)にかなり新しいです、そして、私は今、ほぼ2時間近くすべてを試しました。しかし、私はあきらめなければならないポイントに達しました。あなたが私の問題を明らかにしてくれることを願っています。私は次のエラーを取得する:invalid context私の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; 
} 

答えて

7

それは、パラメータ、ないCGPathにようCGContextを取る必要があります。

CGContextFillPath(context); 

しかし、CGContextStrokePathを呼び出すと現在のパスがクリアされるため、CGContextFillPathは何も行いません。ストロークと同じパスに記入し、あなたはCGContextDrawPath使用する必要があります。

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

(ともコンテキストを取得する方法について@Luizの変更を適用します。)

+0

申し訳ありませんが、(可能な)愚かな質問がありますが、最後の行にある「k」は何ですか? – 11684

+0

自分の質問に答えました! – 11684

+0

これはストロークの色を描画し、決して色を塗りつぶすことはありません。 – jjxtra

1

をあなたはへのパスを追加する前に、コンテキストを取得する必要がありますそれ:

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

はまた、パスを埋めるために、あなたはコンテキストの参照ではなく、パス自体合格する必要があります。

CGContextFillPath(context); 
+0

私はまだそれを埋めることができません... :( –

関連する問題