关于CCAction方法

本方法再初始化的时候,加入了autorelease,因此,如果把CCAction当做对象的成员变量来使用的话,也许你在初始化一个对象的时候,分配给了该变量一块内存,但是,在程序运行的时候某一时刻,如果程序发现该内存没有被使用,将会回收此空间。所以,当你下次在使用这个成员变量指针的时候,就会引发bad access错误。为了避免此类错误,以后应避免使用某个autorelease对象指针作为自定义的对象成员变量。

例如:

自定义sprite的初始化方法中

        self.bugType = bugTypes;

        isOnTouch = FALSE;

        isHavingBody = NO;

        self.tag = kBodyTagBug;

        

            [self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"woniu1.png"]];

            // move动画

            CCAnimation *animationMove= [CCAnimation animation];

            for (int i= 1; i<=4; i++) {

                [animationMove addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"woniu%d.png",i]]];

            }

            actMove = [CCRepeatForever actionWithAction:[CCAnimate actionWithDuration:0.4 animation:animationMove restoreOriginalFrame:NO]];

            // struggle 动画

            CCAnimation *animationStruggle = [CCAnimation animation];

            for (int i = 1; i <= 11; i++) {

                [animationStruggle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"woniu_zhengzha%d.png",i]]];

            }

            actStruggle = [CCRepeatForever actionWithAction:[CCAnimate actionWithDuration:0.2 animation:animationStruggle restoreOriginalFrame:NO]];

            // inbottle 动画

            CCAnimation *animationInBottle = [CCAnimation animation];

            for (int i = 2; i <=3 ;i++) {

                [animationInBottle addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"woniu%d.png",i]]];

            }

            actInBottle = [CCRepeatForever actionWithAction:[CCAnimate actionWithDuration:0.2 animation:animationInBottle restoreOriginalFrame:NO]];



actMove actStruggle actBottle已经在对象初始化的时候被初始化,我在对象初始化完毕后,立即使用了runaction:actMove,而没有使用另外两个ccaction 对象指针,在程序运行了一段时间之后,我又想使用actStruggle, 发现该指针所指的内存已经被释放,导致badaccess错误。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值