最近我需要在CCSprite
中使用blendFunc
來改善我的應用程序的視覺效果。但我不知道如何在CCAnimation
中使用它。下面是我的實現:我可以在CCAnimation中使用blendFunc嗎?
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];
}
我得到了我想要這樣,但它不是effecient,我認爲必須有一些更好的辦法做到這一點。
謝謝。
UPDATE
我發現它是由
-(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嗎?我希望我的改變不會造成任何錯誤。 請有人告訴我一個確切的答案。非常感謝。
有什麼建議嗎?謝謝。 –