在CCLayer周围边界

我使用Cocos2d拖动精灵,并尝试添加一个边界,如果一个精灵被选中。 我可以看到我的白色背景,但我的边界certificate特别困难。 我有这个代码:

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; 

我尝试了各种咒语,但似乎没有任何边界。 我只需要一个矩形的黑色边框,在我的图层上的其他所有元素的背面都填充了白色的边框。

你可以创build你自己的类,它将包含你的精灵,并在需要的时候绘制边界。 绘制类的边框重写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来添加你的精灵。

    Interesting Posts