2014-02-25 135 views
2

最近我需要在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嗎?我希望我的改變不會造成任何錯誤。 請有人告訴我一個確切的答案。非常感謝。

+0

有什麼建議嗎?謝謝。 –

回答

1

由於您創建的精靈沒有任何紋理,並且如果沒有與其關聯的紋理,CCSprite不會更新blendFunc,因此您的第一個代碼段將不起作用。你應該做的是這個。

// add sprite frames to the sprite frame cache 
CCSprite *sprite = [CCSprite spriteWithSpriteFrame:spriteFramesArra[0]]; 
sprite.blendFunc = newBlendFunc; 
[sprite runAnimation:animation]; 

由於動畫幀使用相同的紋理和我們曾經的動畫運行時,它會被正確地設置和更新紋理與它關聯後設定精靈所需的質感。

+0

這對我很有用,謝謝:)還有我發現的更多, 如果你想播放另一個動畫,只需在動畫的第一幀設置精靈的幀,然後設置blendFunc。 – veslam

相關問題