0

使用下面的代码,我可以连续更改 CCSprite 的颜色,但我需要使用此代码更改我的 CCSprite 图像。我怎样才能做到这一点?

注意:不想只显示帧变化的动画,我需要选择稍后在我的代码中不断变化的精灵之一。

 id delayTime1 = [CCDelayTime actionWithDuration:0.05f];
    id calFun1   = [CCCallBlock actionWithBlock:^{
        //HERE SET BLUE TEXTURE..
       // m_spriteBubbleWeapon= [CCSprite spriteWithFile:[NSString stringWithFormat:@"firstimage.png"]];// //kill when i uncomment it 

        m_spriteBubbleWeapon.color = ccc3(255,0,255); // works current

    }];
    id delayTime2 = [CCDelayTime actionWithDuration:0.05f];
    id calFun2   = [CCCallBlock actionWithBlock:^{
        //HERE SET RED TEXTURE..
        //m_spriteBubbleWeapon= [CCSprite spriteWithFile:[NSString stringWithFormat:@"second image.png"]];//kill when i uncomment it

        m_spriteBubbleWeapon.color = ccc3(255,0,0);//work correct

    }];

    id sequece = [CCSequence actions:delayTime1, calFun1, delayTime2, calFun2, nil];
    id repeate = [CCRepeatForever actionWithAction:sequece];

    [sprite runAction:repeate];
4

1 回答 1

4

在类的前面定义一个属性 tex1 和 tex2(例如在 init 中):

CCTexture2D* tex1 = [[CCTextureCache sharedTextureCache] addImage:@"firstimage.png"];
CCTexture2D* tex2 = [[CCTextureCache sharedTextureCache] addImage:@"secondimage.png"];

然后在代码中将纹理替换为现有精灵:

id delayTime1 = [CCDelayTime actionWithDuration:0.05f];
id calFun1   = [CCCallBlock actionWithBlock:^{
    //HERE SET BLUE TEXTURE..
    [m_spriteBubbleWeapon setTexture:tex1];
    m_spriteBubbleWeapon.color = ccc3(255,0,255); // works current
}];

...

    //HERE SET RED TEXTURE..
    [m_spriteBubblWeapon setTexture:tex2];
于 2013-04-08T14:58:55.307 回答