【问题标题】:change the texture frame of the sprite on touch在触摸时更改精灵的纹理框架
【发布时间】:2013-10-01 14:10:47
【问题描述】:

作为 cocos2d 的新手,我现在被粉刷了,我需要在触摸开始时更改精灵的图像(从精灵表中,我也没有使用 .plist 作为坐标),因此为此我使用了以下代码

elfSheet = [CCSpriteBatchNode batchNodeWithFile:@"BowSpritesheet.png" capacity:12];
            [self addChild:elfSheet];
            elfFrame1 = [CCSpriteFrame frameWithTexture:elfSheet.texture rect:
                         CGRectMake(0, 0, 128, 140)]; //elf run00001.png
            elfFrame2 = [CCSpriteFrame frameWithTexture:elfSheet.texture rect:
                         CGRectMake(128, 0, 110, 140)]; //elf run00002.png

Bow = [CCSprite  spriteWithTexture:elfFrame1.texture rect:elfFrame1.rect];
            Bow.position=ccp(size.width/8, size.height/5);
            [elfSheet addChild:Bow z:0];

这里elfSheetCCSpriteBatchNode
现在在 cctouches 开始我需要将精灵帧更改为 elfFrame2 当我尝试设置显示帧时它会造成崩溃

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [Bow setDisplayFrame:elfFrame2];//Creates a crash
}

请帮助我解决这个问题
提前致谢

【问题讨论】:

    标签: ios cocos2d-iphone


    【解决方案1】:

    您过度释放 CCSprite ( Bow )。 spriteWithTexture 返回一个自动释放的对象,所以你不应该再次调用它的自动释放。

    【讨论】:

    • 感谢您的链接,但正如我之前所说,我没有使用 .plist,而且我正在使用精灵表,其中包含我需要在触摸时更改为 elf frame2 的图像
    • 我对您的评论感到困惑。我没有提到 plist 或 spritesheets,但现在你提到它......沿着相同的内存管理线,我猜 elfFrame1 和 elfFrame2 没有被正确保留,所以当你尝试使用 [Bow setDisplayFrame :elfFrame2]; , elfFrame2 可能已经是一个无效的引用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多