Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating sprites in Cocos2d android

I want to animate a moving sprite in cocos2d using android, help please here is my code:

public void setMoveAnimation(CCSprite target) {
    //CCSprite target = CCSprite.sprite("Target.png");
    CCAnimation OpenAnim = CCAnimation.animation("", 0.1f);

    for (int i = 1; i <= 2; i++) {
        OpenAnim.addFrame(CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName("target.0" + i + ".png"));

        CCAction MoveAction = CCAnimate.action(1, OpenAnim, false);
        target.runAction(MoveAction);
    }
}
like image 746
hanif s Avatar asked Dec 26 '22 10:12

hanif s


2 Answers

you can do like this. this is working for me. devil1.png,devil2.png like this upto devil4.png

CCAnimation animation = CCAnimation.animation("dance", 0.2f);
    for (int i = 1; i < 5; i++) {
        animation.addFrame(CCFormatter.format(devil%d.png, i));
    }

    CCIntervalAction action = CCAnimate.action(animation);
    target.runAction(CCRepeat.action(action, 9));
like image 61
Bebin T.N Avatar answered Dec 28 '22 22:12

Bebin T.N


You can do this:

public void openScrPpr(float duration, CCSprite player) {
    CCAnimation scrPprAnim = CCAnimation.animation("", 2 / 20f);
    CGSize size = CCDirector.sharedDirector().displaySize();
    CCMoveTo moveTo = CCMoveTo.action(1,
            CGPoint.ccp(size.getWidth(), (size.getHeight() - 40)));

    for (int i = 1; i <= 7; i++) {
        scrPprAnim.addFrame(CCSpriteFrameCache.sharedSpriteFrameCache()
                .spriteFrameByName(i + ".png"));

        if (i == 7) {
            for (int j = 7; j >= 1; j--) {
                scrPprAnim.addFrame(CCSpriteFrameCache
                        .sharedSpriteFrameCache().spriteFrameByName(j + ".png"));
            }
        }
    }

    CCIntervalAction scrPprAction = CCAnimate.action(duration, scrPprAnim, false);
    CCRepeatForever forever = CCRepeatForever.action(scrPprAction);
    CCRepeatForever forever2 = CCRepeatForever.action(moveTo);
    player.runAction(forever);
}
like image 35
Rishabh Bhardwaj Avatar answered Dec 29 '22 00:12

Rishabh Bhardwaj