Iphone CCLayer周围的边界

Iphone CCLayer周围的边界,iphone,ios,cocos2d-iphone,cclayer,Iphone,Ios,Cocos2d Iphone,Cclayer,我正在使用Cocos2d拖动精灵,并尝试在选中精灵时添加边框。我可以显示我的白色背景,但我的边界被证明是特别困难的。我有以下代码: if(self.selectedSprite) self.selectedSprite = nil; CCLayerColor *selectedLayer = [[CCLayerColor alloc] init]; // CCSprite *backgroundSprite = [CCSprite spriteWithFile:@"white_1

我正在使用Cocos2d拖动精灵,并尝试在选中精灵时添加边框。我可以显示我的白色背景,但我的边界被证明是特别困难的。我有以下代码:

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;
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高度:backgroundSprite.texture.contentSize.height+2];
[背景雪碧·塞特弗利比:是];
[背景色: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开始];

对于(int i=0;i您可以创建自己的类,该类将包含您的精灵,并在需要时在其上绘制边框。若要绘制边框,请覆盖类的
draw()
方法

-(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添加精灵。

您可以创建自己的类,该类将包含精灵并在其上绘制边框(如果需要)。若要绘制边框,请覆盖类的
draw()
方法

-(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添加精灵。

非常好,这正是我需要的。m_needDrawRect是用来控制笔划是否可见的。非常好,这正是我需要的。m_needDrawRect是用来控制笔划是否可见的e笔划可见。