Iphone 在cocos2D中创建形状图层
在我的UIkit应用程序中,我使用了以下代码:Iphone 在cocos2D中创建形状图层,iphone,xcode,cocos2d-iphone,calayer,shape,Iphone,Xcode,Cocos2d Iphone,Calayer,Shape,在我的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,
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中做同样的事情?非常感谢。对不起,我的英语是法语://您需要做的是将该路径绘制到
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];
您需要做的是将该路径绘制到
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;//无论您想要什么
和CGContextSetRGBStrokeColor(上下文,0.0,0.0,1.0)代码>将其设置为纯黑色。我有一些错误:在某些内容中请求成员中心,并且在这行代码的纹理之前函数“CGContextAddPath和expected]的参数太少:CCSprite*sprite=[CCSprite spriteWithTexture];抱歉,屏幕上没有显示任何内容,我有警告:/n您在xcode上尝试过吗?你会明白我的问题。我有这样的警告:传递'CGPathMoveToPoint'的参数1会丢弃指针目标类型的限定符。我如何定义宽度和高度?很快,但我对cocos2DCGFloat width=45;//无论您想要什么
和CGContextSetRGBStrokeColor(上下文,0.0,0.0,1.0)代码>将其设置为纯黑色。我有一些错误:在某些内容中请求成员中心,并且在这行代码的纹理之前函数“CGContextAddPath和expected]的参数太少:CCSprite*sprite=[CCSprite spriteWithTexture];抱歉,屏幕上没有显示任何内容,我有警告:/n您在xcode上尝试过吗?你会明白我的问题。我有如下警告:传递'CGPathMoveToPoint'的参数1将丢弃指针目标类型的限定符