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

游戏框架基本完成,而且能够顺利运行,但我想创建一个矩形的图层,方便未来的游戏升级或者维护工作。这些矩形会在编译时被启用,为开发者显示屏幕的聚光区(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伐。

全部回覆(1)
Peter_Zhu

答:FuzzyBunnySlippers
我覺得OpenGL ES 2.0不會支援GL_LINE_SMOOTH,只有Desktop Version版本中支援它。所以我覺得,你可以使用以下繪圖方法來繪製矩形和其它的東西:

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);

你也可以不填版本:

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

這裡有一些樣碼,可以供你參考。我創建了一個project(比cocos2d 1.01更低的版本),然後添加如下程式碼:

-(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);
}

實現的效果如下:

答:godel9
你可以使用CCLayerColor類別,加入純色的矩形,只需要輸入幾行程式碼,不需要建立其它的子類別:

CCLayerColor *rectangleNode = [CCLayerColor layerWithColor:color
                                                     width:width
                                                    height:height];
rectangleNode.position = position;
[self addChild:rectangleNode];
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!