CCSpriteBatchNode。更改纹理文件 [英] CCSpriteBatchNode. change texture file

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

问题描述

在我的应用程序中,我有很多不同的动画,我的sprite,并将有更多的tham在以后的版本。这是相当困难使用纹理文件将数百帧,所以我想做几个tham。所以,我写了这段代码:

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渲染sprite时不工作

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'CCSprite: setTexture doesn't work when the sprite is rendered using a CCSpriteBatchNode'

我切换纹理文件?

推荐答案

你不能改变CCSpriteBatchNode使用的纹理。

You can't change the texture a CCSpriteBatchNode uses.

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

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顺序问题。在最坏的情况下,你只需要重新设计动画,或者不使用sprite批处理节点。

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。更改纹理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆