在Cocos2d android中动画精灵

时间:2013-01-30 12:17:38

标签: android sprite cocos2d-android

我想使用android为 cocos2d 中的移动sprite制作动画,请帮助 这是我的代码:

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);
    }
}

4 个答案:

答案 0 :(得分:3)

你可以这样做:

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);
}

答案 1 :(得分:3)

你可以这样做。这对我有用。 devil1.png,devil2.png像这样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));

答案 2 :(得分:2)

我使用以下代码在屏幕上显示一个带有动画的对象:

 CCSpriteFrameCache.sharedSpriteFrameCache().addSpriteFrames("ninjastar.plist");
 CCSpriteSheet projectileSheet = CCSpriteSheet.spriteSheet("ninjastar.png");
 addChild(projectileSheet);
 ArrayList<CCSpriteFrame> projectileSprites = new ArrayList<CCSpriteFrame>();

 for (int i = 1; i <= 4; i++) 
 {
     projectileSprites.add(CCSpriteFrameCache.spriteFrameByName("ninjastar" + i + ".png"));
 }



  CCAnimation projectileAnimation = CCAnimation.animation("throw", 0.1f, projectileSprites);
  CCSprite projectile = CCSprite.sprite(projectileSprites.get(0));
  CCAction projectileAction = CCRepeatForever.action(CCAnimate.action(projectileAnimation, false));
  projectile.setPosition(CGPoint.ccp(winSize.width + (projectile.getContentSize().width / 2.0f), actualY));
  actionMove = CCMoveTo.action(actualDuration, CGPoint.ccp(-      projectile.getContentSize().width / 2.0f + 320, actualY));
  projectileSheet.addChild(projectile);
  projectile.setTag(1);
  projectiles.add(projectile);

  CCCallFuncN actionMoveDone = CCCallFuncN.action(this, "spriteMoveFinished");
  CCSequence actions = CCSequence.actions(actionMove, actionMoveDone);
  projectile.runAction(actions);
  projectile.runAction(projectileAction);

我正在使用“spriteMoveFinished”来删除精灵在屏幕上完成之后:

  public void spriteMoveFinished(Object sender) 
  {
    CCSprite sprite = (CCSprite)sender;
    projectiles.remove(sprite);
    sprite.stopAllActions();
    sprite.removeFromParentAndCleanup(true);
  }

答案 3 :(得分:2)

Vaishali我正在使用这个:

 ArrayList<CCSpriteFrame> jumpForwardAnimation=jumpForwardDown();
                 CCAnimation crocodileEatFrogJumpsUpAnimation = CCAnimation.animation("throw", 0.14f, jumpForwardAnimation);

                 COUNTdOWNaCTION=CCAnimate.action(crocodileEatFrogJumpsUpAnimation, false);

    CCBezierConfig bezier = new CCBezierConfig();   

                 bezier.controlPoint_1=CGPoint.ccp(frog.getPosition().x+frog.getContentSize().width*0.5f,frog.getPosition().y+frog.getContentSize().height*0.6f);
                 bezier.controlPoint_2=CGPoint.ccp(frog.getPosition().x+frog.getContentSize().width*0.5f,frog.getPosition().y+frog.getContentSize().height);
                 bezier.endPosition=location;

                 CCBezierTo  action = CCBezierTo.action(0.5f, bezier);
                 CCFiniteTimeAction action2=CCSpawn.actions(COUNTdOWNaCTION,action );

                 CCFiniteTimeAction actionMoveDone = CCCallFunc.action(this, "frogMoveDone");
                 CCAction COUNTdOWNaCTION1=CCSequence.actions(action2, actionMoveDone);
                 frog.runAction(COUNTdOWNaCTION1);


public void frogMoveDone(){

        frog.setPosition(point);
        }

但是我的青蛙首先用beizer路径动画到下一个位置,然后在它改变位置之后返回到之前的位置..

你能告诉我这里做错了吗?