0

I want to create touch areas (CGMutablePathRefs) around my hexagon sprites. My aim is creating keys with names hexTouchArea1, hexTouchArea2, and so on, so I started to store them in an NSMutableDictionary. But I can't store CGMutablePathRefs in it. How would I solve the problem ?

for (int i = 0; i < hexCount; i++) {
            hexTouchAreas = [[NSMutableDictionary alloc] init];
            CGPoint touchAreaOrigin = ccp(location.x -22, location.y-40);
            NSString *touchAreaKey = [NSString stringWithFormat:@"hexTouchArea%d",i];
            CGMutablePathRef hexTouchArea = CGPathCreateMutable();
            hexTouchArea = [self drawHexagonTouchArea:touchAreaOrigin];

            [hexTouchAreas setObject:hexTouchArea forKey:touchAreaKey];
            NSLog(@"the touchareas are %@", hexTouchAreas);
}

drawHexagonTouchArea returns a CGMutablePathRef :

-(CGMutablePathRef) drawHexagonTouchArea:(CGPoint)origin
{

    CGMutablePathRef path = CGPathCreateMutable();
    CGPoint newloc = CGPointMake(origin.x, origin.y);

    CGPathMoveToPoint(path, NULL, newloc.x, newloc.y);
    CGPathAddLineToPoint(path, NULL, newloc.x -22,newloc.y + 38);
    CGPathAddLineToPoint(path, NULL, newloc.x + 0, newloc.y + 76);
    CGPathAddLineToPoint(path, NULL, newloc.x + 46,  newloc.y + 76);
    CGPathAddLineToPoint(path, NULL, newloc.x +66,newloc.y + 40);
    CGPathAddLineToPoint(path, NULL, newloc.x +44, newloc.y + 0);
    CGPathCloseSubpath(path);
    return path;
}

AND : How can I assign these touch areas to CCSprites so if the sprite rotates, they do not move separately ?

the_critic
  • 12,720
  • 19
  • 67
  • 115
  • First, you should be aware that you're leaking two CGMutablePathRefs in the above code. If you use `CGPathCreateMutable()`, you need to match that with a `CGPathRelease()` or the path will never be deallocated. Also, there's no need to create the path in your initialization of `hexTouchArea` within the loop, because you just overwrite it with the result of `-drawHexagonTouchArea:`. – Brad Larson Dec 05 '11 at 20:11
  • possible duplicate of [CGMutablePathRef to NSMutableArray](http://stackoverflow.com/questions/4133063/cgmutablepathref-to-nsmutablearray) – Brad Larson Dec 05 '11 at 20:13
  • While the linked question deals with NSMutableArray instead of NSMutableDictionary, the same principle applies here. – Brad Larson Dec 05 '11 at 20:14

2 Answers2

1

You can use NSValue to encapsulate the CGMutablePathRef, and then add it to the dictionary:

NSValue *pathAsValue = [NSValue valueWithPointer:hexTouchArea];
[dictionary setObject:pathAsValue forKey:yourKeyHere];

When you need to get it, use:

NSValue *myPathAsValue = [dictionary objectForKey:yourKeyHere];
CGMutablePathRef pathRef = [myPathAsValue pointerValue];
  • 1
    Careful with that, though -- encapsulating a pointer in `NSValue` won't automatically call `CGPathRelease` when the dictionary is destroyed. –  Dec 05 '11 at 20:18
1

Change:

[hexTouchAreas setObject:hexTouchArea forKey:touchAreaKey];

to:

[hexTouchAreas setObject:(id)hexTouchArea forKey:touchAreaKey];

CGPath and CGMutablePath are just opaque CFType object types, and those can be added (via casting to id) to any Cocoa container classes that are toll-free bridged to their CoreFoundation counter-parts.

And watch that memory leak of the result returned from drawHexagonTouchArea

gschandler
  • 3,208
  • 16
  • 16
  • Huh, did not know that CFType was bridged to NSObject, as Ken states here: http://stackoverflow.com/a/1392445/19679 . I just assumed this casting only worked for the explicitly toll-free bridged types. – Brad Larson Dec 06 '11 at 05:00