关于CCAction方法

来源:互联网 发布:明星拼图软件 编辑:程序博客网 时间:2024/06/10 19:37

本方法再初始化的时候,加入了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错误。


原创粉丝点击