本文介绍了CCSpriteBatchNode.更改纹理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我的精灵有大量不同的动画,在以后的版本中还会有更多.使用纹理文件将数百帧是相当困难的,所以我想制作几个.所以,我写了这段代码:

In my app I have a great number of different animations of my sprite, and there will be more of tham in later versions. It is quite difficult to use texture file will hundreds of frames, so I whould like to make several of tham. So, I wrote this code:

    if (running)
    {
        NSArray * animations = [[physicalBody getAnimationList] objectForKey:ANIMATION_RUN];
        if (!inAir)
        {
            currentFrame++;
            if (currentFrame>=[animations count]*ANIMATION_ITERATION)
                currentFrame = 0;
        }
        else
        {
            currentFrame = 2;
        }
        PhysicsSprite *sprite = (PhysicsSprite*)[physicalBody getSprite];
        [sprite setFlipX:(moveingDirection)];
        [sprite setDisplayFrame:[animations objectAtIndex:currentFrame/ANIMATION_ITERATION]];
    }
    else
    {
        NSArray * animations = [[physicalBody getAnimationList] objectForKey:ANIMATION_STAND];
        currentFrame++;
        if (currentFrame>=[animations count]*ANIMATION_ITERATION)
            currentFrame = 0;
        PhysicsSprite *sprite = (PhysicsSprite*)[physicalBody getSprite];
        [sprite setFlipX:(moveingDirection)];
        [sprite setDisplayFrame:[animations objectAtIndex:currentFrame/ANIMATION_ITERATION]];   //error here
    }

动画数组是从不同的 *.png 创建的,当它尝试切换 tham 时,我收到了这个错误:

Array of animations was created from different *.png, and when it tries to switch tham, i got this error:

* 由于未捕获的异常NSInternalInconsistencyException"而终止应用程序,原因:CCSprite: setTexture"使用 CCSpriteBatchNode 渲染精灵时不起作用

如何切换纹理文件?或者可能有其他解决方案?

How can I switch texture files? Or may be there is another solution?

推荐答案

您无法更改 CCSpriteBatchNode 使用的纹理.

You can't change the texture a CCSpriteBatchNode uses.

但是,您可以并且应该为您使用的每个纹理创建一个 CCSpriteBatchNode.然后,每当您设置显示帧时,首先检查帧的纹理并使用它从当前精灵批处理节点中删除精灵,并将其添加到设置为使用与精灵帧相同的纹理的节点.

However you can and should create a CCSpriteBatchNode for each texture that you are going to use. Then, whenever you set a display frame first check the frame's texture and use that to remove the sprite from the current sprite batch node and add it to the one that is setup to use the same texture as the sprite frame.

但是,这可能会导致 z 排序问题.在最坏的情况下,您只需重新设计动画或不使用精灵批处理节点.

However this can lead to z ordering issues. In the worst case you'll simply have to either redesign your animations or don't use sprite batch nodes.

这篇关于CCSpriteBatchNode.更改纹理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:04