2012-06-05 4 views
1

J'utilise Cocos2d pour faire glisser l'image-objet et essayer d'ajouter une bordure si une image-objet est sélectionnée. Je peux afficher mon fond blanc, mais ma frontière s'avère particulièrement difficile. J'ai ce code:Bordure autour de CCLayer

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; 

J'ai essayé différentes incantations, mais rien ne semble montrer une bordure. J'ai juste besoin d'une bordure noire rectangulaire qui est remplie de blanc à l'arrière de tout le reste sur ma couche.

Répondre

5

Vous pouvez créer votre propre classe, qui contiendra votre sprite et la tracera si nécessaire. Pour dessiner la méthode override à la frontière draw() de votre classe

-(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); 
    } 

} 

tout ce que vous dessinez dans cette méthode sera établi avec Zorder 0, donc, pour voir votre frontière, ajoutez votre sprite Zorder -1.

+0

Excellent, c'est exactement ce dont j'avais besoin. m_needDrawRect est ce que vous utilisez pour contrôler si le trait est visible ou non. – Echilon

+0

oui, ce code vient d'être copié de ma classe =) – Morion

Questions connexes