How to draw a solid circle with cocos2d for iPhone

后端 未结 6 1911
遇见更好的自我
遇见更好的自我 2021-02-02 15:53

Is it possible to draw a filled circle with cocos2d ? An outlined circle can be done using the drawCircle() function, but is there a way to fill it in a certain color? Perhaps b

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 16:30

    I also wonder this, but haven't really accomplished doing it. I tried using CGContext stuff that Grouchal tipped above, but I can't get it to draw anything on the screen. This is what I've tried:

    -(void) draw
    {
        [self makestuff:UIGraphicsGetCurrentContext()]; 
    }
    
    -(void)makestuff:(CGContextRef)context
    {
        // Drawing lines with a white stroke color
        CGContextSetRGBStrokeColor(context, 1.0, 1.0, 1.0, 1.0);
        // Draw them with a 2.0 stroke width so they are a bit more visible.
        CGContextSetLineWidth(context, 2.0);
    
        // Draw a single line from left to right
        CGContextMoveToPoint(context, 10.0, 30.0);
        CGContextAddLineToPoint(context, 310.0, 30.0);
        CGContextStrokePath(context);
    
        // Draw a connected sequence of line segments
        CGPoint addLines[] =
        {
            CGPointMake(10.0, 90.0),
            CGPointMake(70.0, 60.0),
            CGPointMake(130.0, 90.0),
            CGPointMake(190.0, 60.0),
            CGPointMake(250.0, 90.0),
            CGPointMake(310.0, 60.0),
        };
        // Bulk call to add lines to the current path.
        // Equivalent to MoveToPoint(points[0]); for(i=1; i

    These methods are defined in a cocos node class, and the makestuff method I borrowed from a code example...

    NOTE: I'm trying to draw any shape or path and fill it. I know that the code above only draws lines, but I didn't wanna continue until I got it working.

    EDIT: This is probably a crappy solution, but I think this would at least work.

    Each CocosNode has a texture (Texture2D *). Texture2D class can be initialized from an UIImage. UIImage can be initialized from a CGImageRef. It is possible to create a CGImageRef context for the quartz lib.

    So, what you would do is:

    1. Create the CGImageRef context for quartz
    2. Draw into this image with quartz
    3. Initialize an UIImage with this CGImageRef
    4. Make a Texture2D that is initialized with that image
    5. Set the texture of a CocosNode to that Texture2D instance

    Question is if this would be fast enough to do. I would prefer if you could sort of get a CGImageRef from the CocosNode directly and draw into it instead of going through all these steps, but I haven't found a way to do that yet (and I'm kind of a noob at this so it's hard to actually get somewhere at all).

提交回复
热议问题