【问题标题】:CCSprite: setTexture doesn't work when the sprite is rendered using a CCSpriteBatchNodeCCSprite:当使用 CCSpriteBatchNode 渲染精灵时,setTexture 不起作用
【发布时间】:2012-09-13 05:05:46
【问题描述】:

这是我收到的错误:

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

当我尝试播放动画时。

这是我创作的动画:

_tokenAnimation = [[CCAnimation alloc] init];
int frameCount = 12;
for (int i = 1; i <= frameCount; i++)
{
    CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Token Ball 000%d.png", i]];
    [_tokenAnimation addFrame:frame delay:0.1];
}

这是我在调用动画 - 以前没看过这个吗?

GameObject *creditPickup = [_creditPickups nextSprite];

    creditPickup.position = ccp(_creditPosition.x, _creditPosition.y);
    [creditPickup revive];

    [creditPickup runAction:[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:_tokenAnimation restoreOriginalFrame:NO]]];
    [creditPickup runAction: [CCSequence actions:
                              [CCMoveBy actionWithDuration:5.0 position:ccp(-_winSize.width*1.5, 0)],
                              [CCCallFuncN actionWithTarget:self selector:@selector(invisNode:)], nil]];

我听说 CCSpriteBatchNode 不好?如果是这样,我怎样才能改变我的精灵表的阅读??

还有什么我做错了吗?

【问题讨论】:

    标签: ios cocos2d-iphone box2d


    【解决方案1】:

    你的问题就在那里:

    CCSprite:当 sprite 使用 CCSpriteBatchNode'

    这意味着作为子节点添加到 CCSpriteBatchNode 的 CCSprite 中没有一个可以运行 setTexture 方法。原因是它们都必须使用与其父 CCSpriteBatchNode 相同的纹理。所以 cocos2d 在 sprite-batched 的 sprite 上禁用该方法。

    在您的情况下,很可能动画的至少一个精灵帧不在其子精灵播放该动画的 CCSpriteBatchNode 使用的纹理中。

    I hear something about CCSpriteBatchNode's being bad?
    

    是的,这很糟糕。太糟糕了,它会进行月球漫步。 :)

    【讨论】:

    • 感谢 bud ;) 在读取 SpriteFrames 的名称时遇到问题,真的很愚蠢 - 哦,好吧!谢谢!
    【解决方案2】:

    试试这个:

    CCAnimation* animation;
    
    NSMutableArray *animFrames = [NSMutableArray array]; 
    CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
    
    for(int i=0;i<=12;i++)
    {
        CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Token Ball 000%d.png", i]]
        [animFrames addObject:frame];
    }
    
    
    animation = [CCAnimation animationWithSpriteFrames:animFrames];
    
    animation.delayPerUnit =  0.1f;
    animation.restoreOriginalFrame = NO;
    
    
    CCAnimate *AnimAction  = [CCAnimate actionWithAnimation:animation];
    
    CCRepeatForever *anim = [CCRepeatForever actionWithAction:AnimAction];
    
    [creditPickup runAction:anim];
    

    【讨论】:

    • 我在令牌获得 EXC_BAD_ACCESS:CCAnimate *token = [CCAnimate actionWithAnimation:_tokenAnimation]; CCRepeatForever *anim = [CCRepeatForever actionWithAction:token];不确定它是什么,我在预加载中初始化动画,然后在那里使用它。会不会迷路了?
    猜你喜欢
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多