【问题标题】:create shapes layer in cocos2D在 cocos2D 中创建形状图层
【发布时间】:2012-02-29 20:04:22
【问题描述】:

在我的 UIkit 应用程序中,我使用了以下代码:

boxPath = CGPathCreateMutable();

CGPathMoveToPoint(boxPath, nil, center.x , center.y - 35);
CGPathAddArcToPoint(boxPath, nil, center.x + 35, center.y - 35, center.x + 35, center.y + 35, 4.7);
CGPathAddArcToPoint(boxPath, nil, center.x + 35, center.y + 35, center.x - 35, center.y + 35, 4.7);
CGPathAddArcToPoint(boxPath, nil, center.x - 35, center.y + 35, center.x - 35, center.y, 4.7);
CGPathAddArcToPoint(boxPath, nil, center.x - 35, center.y - 35, center.x, center.y - 35, 4.7);

CGPathCloseSubpath(boxPath);

创建一个形状,因此我有一个具有我绘制的精确形状的图层。请问如何在 cocos2D 中做同样的事情?谢谢你。对不起我的英语我是法国人:/

【问题讨论】:

    标签: iphone xcode cocos2d-iphone calayer shape


    【解决方案1】:

    您需要做的是将该路径绘制为CGContext,并将生成的图像设置为CCSprite

    CGSize size = CGSizeMake(width, height); // width and height should be predefined
    CGPoint center = CGPointMake(size.width/2, size.height/2); // change this to change where the shape is positioned inside the image (normally will be this value)
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 1); // Replace 1 with your desired line width
    CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0); // Change RGBA values if needed
    
    CGMutablePathRef boxPath = CGPathCreateMutable();
    
    CGPathMoveToPoint(boxPath, nil, center.x , center.y - 35);
    CGPathAddArcToPoint(boxPath, nil, center.x + 35, center.y - 35, center.x + 35, center.y + 35, 4.7);
    CGPathAddArcToPoint(boxPath, nil, center.x + 35, center.y + 35, center.x - 35, center.y + 35, 4.7);
    CGPathAddArcToPoint(boxPath, nil, center.x - 35, center.y + 35, center.x - 35, center.y, 4.7);
    CGPathAddArcToPoint(boxPath, nil, center.x - 35, center.y - 35, center.x, center.y - 35, 4.7);
    
    CGPathCloseSubpath(boxPath);
    
    CGContextAddPath(context, boxPath);
    CGContextStrokePath(context);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CCTexture2D *texture = [[CCTexture2D alloc] initWithImage:image];
    CCSprite *sprite = [CCSprite spriteWithTexture:texture];
    [texture release];
    
    [self addChild:sprite];
    

    【讨论】:

    • 请问如何定义宽度和高度?太好了,但我是 cocos2D 的新手
    • CGFloat width = 45; // Whatever you want
    • 并且CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 1.0); 将其设置为纯黑色。
    • 我有一些错误:Request for member center in something 和 Too little arguments to function 'CGContextAddPath and expected ] 在这行代码的纹理之前:CCSprite *sprite = [CCSprite spriteWithTexture texture];跨度>
    • 抱歉,屏幕上什么也没有出现,我有警告:/你在 xcode 上试过了吗?你会看到我的问题。我有这样的警告:传递'CGPathMoveToPoint'的参数1会丢弃指针目标类型的限定符
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 2017-05-03
    • 1970-01-01
    • 2012-12-09
    相关资源
    最近更新 更多