首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CCLayer周围的边界

CCLayer周围的边界
EN

Stack Overflow用户
提问于 2012-06-06 01:48:42
回答 1查看 3.2K关注 0票数 1

我使用Cocos2d拖拽精灵,并尝试在精灵被选中的情况下添加一个边框。我可以显示我的白色背景,但是我的边框特别困难。我有这样的代码:

代码语言:javascript
复制
if(self.selectedSprite)
    self.selectedSprite = nil;

CCLayerColor *selectedLayer = [[CCLayerColor alloc] init];
//    CCSprite *backgroundSprite = [CCSprite spriteWithFile:@"white_1x1.gif" rect:CGRectMake(2,2,self.boundingBox.size.width-4,self.boundingBox.size.height-4)];
CCSprite *backgroundSprite = [CCSprite spriteWithFile:@"white_1x1.gif" rect:CGRectMake(0,0,self.boundingBox.size.width,self.boundingBox.size.height)];
[backgroundSprite setContentSize:CGSizeMake(self.contentSize.width-4, self.contentSize.height-4)];
backgroundSprite.anchorPoint = ccp(0,0);

CCRenderTexture* rt = [CCRenderTexture renderTextureWithWidth:backgroundSprite.texture.contentSize.width + 2  height:backgroundSprite.texture.contentSize.height+2];

[backgroundSprite setFlipY:YES];
[backgroundSprite setColor:ccc3(0,0,0)];
ccBlendFunc originalBlendFunc = [backgroundSprite blendFunc];
[backgroundSprite setBlendFunc:(ccBlendFunc) { GL_SRC_ALPHA, GL_ONE }];

CGPoint bottomLeft = ccp(backgroundSprite.texture.contentSize.width * backgroundSprite.anchorPoint.x + 1, backgroundSprite.texture.contentSize.height * backgroundSprite.anchorPoint.y + 1);
CGPoint position = ccpSub([backgroundSprite position], ccp(-backgroundSprite.contentSize.width / 2.0f, -backgroundSprite.contentSize.height / 2.0f));

[rt begin];

for (int i=0; i<360; i++) // you should optimize that for your needs
{
    [backgroundSprite setPosition:ccp(bottomLeft.x + sin(CC_DEGREES_TO_RADIANS(i))*1, bottomLeft.y + cos(CC_DEGREES_TO_RADIANS(i))*1)];
    [backgroundSprite visit];
}

[backgroundSprite setPosition:bottomLeft];
[backgroundSprite setBlendFunc:originalBlendFunc];
[backgroundSprite setColor:ccc3(255,255,255)];
[backgroundSprite visit];

[rt end];

[rt setPosition:position];

[selectedLayer addChild:rt];
[selectedLayer addChild:backgroundSprite];
self.selectedSprite = selectedLayer;

我试过各种咒语,但似乎没有一种是有边界的。我只需要一个矩形的黑色边框,它是由白色填充在我的图层上的所有其他东西的背面。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-06 02:54:57

您可以创建自己的类,它将包含您的sprite,并在其上绘制边界,如果需要的话。绘制边框重写类的draw()方法

代码语言:javascript
复制
-(void) draw
{
    if( m_needDrawRect == YES )
    {
        CGSize selfSize = [self contentSize];
        float selfHeight = selfSize.height;
        float selfWidth = selfSize.width;
        CGPoint vertices[4] = {ccp(0.f, 0.f), ccp(0.f, selfHeight), ccp(selfWidth, selfHeight), ccp(selfWidth, 0.f)};
        ccDrawPoly(vertices, 4, YES);
    }

}

您在此方法中绘制的所有内容都将使用zOrder 0绘制,因此,要查看您的边框,请使用zOrder -1添加子画面。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10902274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档