围绕CCLayer [英] Border around CCLayer

查看:246
本文介绍了围绕CCLayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Cocos2d拖动精灵,如果选择精灵,则尝试添加边框。我可以得到我的白色背景来显示,但我的边界证明是特别困难。我有这个代码:

I'm using Cocos2d to drag sprite around, and trying to add a border if a sprite is selected. I can get my white background to display, but my border is proving particularly difficult. I have this code:

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;

我尝试了各种咒语,但似乎没有显示边框。

I've tried various incantations, but nothing seems to show a border. I just need a rectangular black border which is filled with white at the back of everything else on my layer.

推荐答案

您可以创建一个你自己的类,将包含你的sprite和绘制边框,如果需要的话。要绘制边框覆盖 c> c> c

You can create your own class, that will contain your sprite and draw border over it if is needed. To draw border override draw() method of your class

-(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添加您的sprite。

all you draw in this method will be drawn with zOrder 0, so, to see your border, add your sprite with zOrder -1.

这篇关于围绕CCLayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆