gpt4 book ai didi

iphone - CCLayer 周围的边框

转载 作者:行者123 更新时间:2023-12-01 18:00:18 26 4
gpt4 key购买 nike

我正在使用 Cocos2d 拖动 Sprite ,并在选择 Sprite 时尝试添加边框。我可以显示我的白色背景,但事实证明我的边框特别困难。我有这个代码:

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;

我尝试了各种咒语,但似乎没有任何东西显示边界。我只需要一个矩形黑色边框,在我图层上其他所有内容的后面填充白色。

最佳答案

您可以创建自己的类,该类将包含您的 Sprite 并在需要时在其上绘制边框。绘制边框覆盖draw()你类(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 。

关于iphone - CCLayer 周围的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10902274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com