5

私は自分の道を埋めるのに助けが必要です。CGContextFillPath(path);たとえば、どこに置くべきかわかりません。私は描画(およびiOS開発)にかなり慣れていないので、2時間近くすべてを実際に試しました。しかし、私はただあきらめなければならないところに到達しました...あなたが私の問題にいくらかの光を当てることができることを願っています。次のエラーが発生します:invalid context

-(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;
}
4

2 に答える 2

9

パラメータとして、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の変更を適用します。)

于 2011-11-29T15:13:24.657 に答える
1

パスを追加する前に、コンテキストを取得する必要があります。

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

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

CGContextFillPath(context);
于 2011-11-29T15:17:57.813 に答える