【发布时间】:2014-02-25 09:22:44
【问题描述】:
最近我需要通过在CCSprite 中使用blendFunc 来改善我的应用程序的视觉效果。但我不知道如何在CCAnimation 中使用它。这是我的实现:
CCSprite * test=[CCSprite node];
test.position=CGPointMake(100, 200);
test.scale=3;
test.blendFunc=(ccBlendFunc){GL_SRC_ALPHA, GL_ONE};//it's not working
[scene addChild:test];
self.textureArray=[[NSMutableArray alloc]init];
for(int x=0;x<=18;x++){
[self.textureArray addObject:[CCSpriteFrame frameWithTextureFilename:[NSString stringWithFormat:@"skill_01_%d.png",x] rect:CGRectMake(0, 0, 91, 61)]];
}
CCAnimation * animation=[CCAnimation animationWithSpriteFrames:self.textureArray delay:0.1];
[test runAction:[CCAnimate actionWithAnimation:animation]];
但是blendFuc不适用于这种情况,只有CCSprite可以设置blendFunc。如果我需要动画中的所有帧都具有 blendFunc 的效果,我该怎么办?
顺便说一句,我尝试使用计时器手动为帧设置动画:
self.skllTimer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(animating:) userInfo:nil repeats:YES];
-(void)animating:(NSTimer *)t{
CCSprite * tesx=[self.textureArray objectAtIndex:f];//f is a increasing value
CCSprite * ok =[CCSprite spriteWithTexture:tesx.texture];
ok.blendFunc=(ccBlendFunc){GL_SRC_ALPHA, GL_ONE};
[self.aniLayer removeAllChildrenWithCleanup:NO];
[self.aniLayer addChild:ok];
}
我通过这种方式得到了我想要的,但是效率不高,我认为必须有更好的方法。
谢谢。
更新
我发现这是由于
-(void) updateBlendFunc
{
NSAssert( ! _batchNode, @"CCSprite: updateBlendFunc doesn't work when the sprite is rendered using a CCSpriteBatchNode");
// it is possible to have an untextured sprite
if( !_texture || ! [_texture hasPremultipliedAlpha] ) {
_blendFunc.src = GL_SRC_ALPHA;
_blendFunc.dst = GL_ONE_MINUS_SRC_ALPHA;
[self setOpacityModifyRGB:NO];
} else {
_blendFunc.src = CC_BLEND_SRC;//here
_blendFunc.dst = CC_BLEND_DST;//and here
[self setOpacityModifyRGB:YES];
}
}
每次CCSprite更新它的displayFrame,这个函数都会改变它自己的_blendFunc。我不知道为什么会这样,但是在我删除以下两行之后:
//_blendFunc.src = CC_BLEND_SRC;//here
//_blendFunc.dst = CC_BLEND_DST;//and here
问题解决了。我可以这样修复cocos2d吗?我希望我的更改不会引起错误。 请有人告诉我一个确切的答案。非常感谢。
【问题讨论】:
标签: ios iphone cocos2d-iphone