问题描述
在我的应用程序中,我的精灵有大量不同的动画,在以后的版本中还会有更多.使用纹理文件将数百帧是相当困难的,所以我想制作几个.所以,我写了这段代码:
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 渲染精灵时不起作用
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'CCSprite: setTexture doesn't work when the sprite is rendered using a 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.更改纹理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)