ios - 如何在CCScene中添加矩形?
伊谢尔伦
伊谢尔伦 2017-04-17 12:04:23
0
1
725

游戏框架基本完成,而且能够顺利运行,但我想创建一个矩形的图层,方便未来的游戏升级或者维护工作。这些矩形会在编译时被启用,为开发者显示屏幕的聚光区(scene hotspots)。
我尝试创建一个新的CCLayer,然后将它的绘图方法变为:

- (void)draw
{
    glEnable(GL_LINE_SMOOTH);
    glColor4ub(255, 255, 255, 255);
    glLineWidth(2);
    CGPoint vertices2[] = { ccp(79,299), ccp(134,299), ccp(134,229), ccp(79,229) };
    ccDrawPoly(vertices2, 4, YES);
}

但是Xcode显示:"Use of Undeclared identifier GL_LINE_SMOOTH"。
我不想因为这个问题再单独创建一个精灵的image,有没有其他的方法,类似于使用"CGContextAddRect",来解决这种问题?

原问题:Adding rectangles to a CCScene - cocos2d

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(1)
Peter_Zhu

Answer: FuzzyBunnySlippers
I think OpenGL ES 2.0 will not support GL_LINE_SMOOTH, only the Desktop Version supports it. So I think, you can use the following drawing methods to draw rectangles and other things:

void ccDrawPoint (CGPoint point);
void ccDrawPoints (const CGPoint *points, NSUInteger numberOfPoints);
void ccDrawLine (CGPoint origin, CGPoint destination);
void ccDrawRect (CGPoint origin, CGPoint destination);

void ccDrawPoly (const CGPoint *vertices, NSUInteger numOfVertices, BOOL closePolygon);
void ccDrawCircle (CGPoint center, float radius, float angle, NSUInteger segments, BOOL drawLineToCenter);

You can also leave the version blank:

void ccDrawSolidRect (CGPoint origin, CGPoint destination, ccColor4F color);
void ccDrawSolidPoly (const CGPoint *poli, NSUInteger numberOfPoints, ccColor4F color);

Here are some sample codes for your reference. I created a project (a version lower than cocos2d 1.01) and added the following code:

-(void) visit
{
   [super visit];

   ccDrawPoint(ccp(240, 160));
   ccDrawLine(ccp(100, 100), ccp(400, 300));
   ccDrawCircle(ccp(310, 150), 50, 1, 10, YES);
   ccDrawQuadBezier(ccp(50, 300), ccp(450, 250), ccp(400, 30), 15);
}

The effect achieved is as follows:

Answer: godel9
You can use the CCLayerColor class to add a solid color rectangle with just a few lines of code and no need to create other subclasses:

CCLayerColor *rectangleNode = [CCLayerColor layerWithColor:color
                                                     width:width
                                                    height:height];
rectangleNode.position = position;
[self addChild:rectangleNode];
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template