【问题标题】:Can I use blendFunc in CCAnimation?我可以在 CCAnimation 中使用 blendFunc 吗?
【发布时间】: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


    【解决方案1】:

    您的第一个代码 sn-p 不起作用,因为您创建的精灵没有任何纹理,如果没有关联的纹理,CCSprite 不会更新 blendFunc。你应该做的是这个。

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

    由于动画帧使用相同的纹理,并且我们在将纹理与其关联后在精灵上设置所需的纹理,一旦动画运行,它将被正确设置和更新。

    【讨论】:

    • 这对我有用,谢谢:) 而且我发现,如果你想播放另一个动画,只需将精灵的帧设置为动画的第一帧,然后设置 blendFunc。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 2011-12-12
    • 2014-12-01
    • 2016-11-07
    • 2015-07-10
    • 2012-07-09
    • 2013-10-21
    相关资源
    最近更新 更多