xcode cocos2d 创建一个圆形并将其用作精灵

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10128249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:57:40  来源:igfitidea点击:

cocos2d create a circle shape and use it as a sprite

xcodecocos2d-iphoneshapegeometryprimitive

提问by greg rock

so I would like to create a circle shape with primitives on cocos2d and then use it as a sprite, How can I do it please ? I know that I have to use something like this :

所以我想在 cocos2d 上用图元创建一个圆形,然后将它用作精灵,我该怎么做?我知道我必须使用这样的东西:

glLineWidth(16); glColor4ub(0, 255, 0, 255); drawCircle( ccp(s.width/2, s.height/2), 100, 0, 10, NO); But it's hard for me to understand how it works and How to use it as a sprite

glLineWidth(16); glColor4ub(0, 255, 0, 255); drawCircle( ccp(s.width/2, s.height/2), 100, 0, 10, NO); 但是我很难理解它是如何工作的以及如何将它用作精灵

回答by Morion

do you really need CCSprite instance? you can create a subclass of CCNode, then in its

你真的需要 CCSprite 实例吗?你可以创建一个 CCNode 的子类,然后在它的

- (void) draw

method put your code there. your circle will have it's center position (0.f, 0.f)

方法把你的代码放在那里。你的圆圈将有它的中心位置 (0.f, 0.f)

@implementation MyScene

- (void) onEnter
{
    [super onEnter];
    CCNode* myNode = [MyNodeSubclass node];
    [node setPosition: someRandomPosition ];
    [self addChild: node];
}

@end

@implementation MyNodeSubclass

- (void) draw
{
    glColor4f(255, 255, 255, 255);
    CCPoint center = ccp(0.f, 0.f);
    CGFloat radius = 10.f;
    CGFloat angle = 0.f;
    NSInteger segments = 10;
    BOOL drawLineToCenter = YES;

    ccDrawCircle(center, radius, angle, segments, drawLineToCenter);    
}

@end

wrote this piece of code right here, didn't copy from xcode, but it should work as you want. ccDrawCircle is a cocos2d function, declared in CCDrawingPrimitives.h

在这里写了这段代码,不是从 xcode 复制的,但它应该可以正常工作。ccDrawCircle 是一个 cocos2d 函数,在 CCDrawingPrimitives.h 中声明