cocos2d-x thread 1: EXC_BADACCESS

在cocos2d-x开发中遇到thread 1: EXC_BADACCESS错误,通过debug navigator定位到GameLayer::update中的sprite->runAction()行存在空指针问题。经过排查发现,由于for循环中当i=0时,对应的图片名称错误导致CCSpriteFrameCache无法找到frame,修正图片名称顺序后问题解决,强调编程时的细心检查至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

thread 1: EXC_BADACCESS(code=2, address=0x14)

CCPoint& CCPoint::operator= (constCCPoint& other)

{

    setPoint(other.x, other.y);

    return *this;

}

错误提示到这里,说明 引用了已经释放的ccpoint对象

持续更新中;

----------------------------------

调出debug navigator,发现GameLayer::update出问题,点击该行发现

sprite->runAction((CCAction *)_explosion->copy()->autorelease());该行肯定有对象为空,以至于在该行调用的时候,产生 EXC_BADACCESS,

再次查找sprite发现没有问题,检查_explosion发现

animation = CCAnimation::create();

    for (int i = 0; i <= 7; i++) {

        name = CCString::createWithFormat("explosion_small%i.png", i);

        frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(name->getCString());

        animation->addSpriteFrame(frame);

    }

    

    animation->setDelayPerUnit(0.5/7.0f);

    animation->setRestoreOriginalFrame(true);

    

    _explosion = CCSequence::create(CCAnimate::create(animation),

                                    CCCallFuncN::create(this, callfuncN_selector(GameLayer::animationDone)),

                                    NULL);

    _explosion->retain();

检查ccsppriteFrameCache也加载进来了,接着检查发现当i = 0的时候name为空,也即是frame为空,去sprite_sheet.plist里面找到该图片的描述,发现名字是从1开始的,即

explosion_small1,explosion_small2,,,,等一直到7,


至此,终于找到了错误的原因。

写代码不细心的惹的祸啊。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值