Search code examples
androidcocos2d-android

Cocos2d image animation not being repeatad


This is a code which I used for animation in cocos2d android, but it doesn't repeat, what to do for that?

 public void animWaterSplash() {    

        OpenPuzzleAnim = CCAnimation.animation("", 0.18f);
        for (int i = 1; i < 3; i++) {
            CCSpriteFrameCache.sharedSpriteFrameCache();
            OpenPuzzleAnim.addFrame(String.format("waterSplash%02d.png", i));
        }
        action1 = CCAnimate.action(OpenPuzzleAnim);
        waterSplash.runAction(action1);
    }

Solution

  • I also faced same problem but than i made small change and solved the problem. firstly you use CCRepeatForever for repetition and add (_) underscore in your image name. In my case i was using same process but when i change the images name.. it worked.

    I modify your code with this. Just check it out , may this help you.

    public void animWaterSplash() {    
    
            OpenPuzzleAnim = CCAnimation.animation("run", 0.18f);
            OpenPuzzleAnim.addFrame(String.format("waterSplash_01.png")); 
            OpenPuzzleAnim.addFrame(String.format("waterSplash_02.png"));
            OpenPuzzleAnim.addFrame(String.format("waterSplash_03.png"));
            CCAnimate animate = CCAnimate.action(OpenPuzzleAnim);  
            CCRepeatForever repeat =  CCRepeatForever.action(animate);
            waterSplash.runAction(repeat); 
    }
    

    and if you want to move your object than add one more action like this

     public void animWaterSplash() {    
    
            OpenPuzzleAnim = CCAnimation.animation("run", 0.18f);
            OpenPuzzleAnim.addFrame(String.format("waterSplash_01.png")); 
            OpenPuzzleAnim.addFrame(String.format("waterSplash_02.png"));
            OpenPuzzleAnim.addFrame(String.format("waterSplash_03.png"));
            CCAnimate animate = CCAnimate.action(OpenPuzzleAnim);  
            CCRepeatForever repeat =  CCRepeatForever.action(animate);
            CCAction moveAction = CCMoveBy.action(6.0f,CGPoint.ccp(yourpoints);
            waterSplash.runAction(repeat);
            waterSplash.runAction(moveAction);
    }
    

    May be its silly but worked for me.you can also try.