1
\$\begingroup\$

I have just started using the Cocos2D library for Android. I am trying to make a walking bear animation.

For that I am using a CCSpriteFrameCache, but I'm getting a "frame not found" error in the log.

I have added an AnimBear.plist file and an AnimBear.png file (combined image of the various movements of the bear) to the assets folder.

My Java code is as per below :

CCSpriteFrameCache.sharedSpriteFrameCache().addSpriteFrames("AnimBear.plist");
CCSpriteSheet spriteSheet = CCSpriteSheet.spriteSheet("AnimBear.png");

scene.addChild(spriteSheet);

ArrayList<CCSpriteFrame> walkSprites = new ArrayList<CCSpriteFrame>();

for (int i = 1; i <= 8; ++i)
{
    walkSprites.add(CCSpriteFrameCache.sharedSpriteFrameCache().getSpriteFrame("bear" + i + ".png"));
    System.out.println("bear image is added");
}

CCAnimation walkAnimation = CCAnimation.animation("bear", 0.1f, walkSprites);

bear = CCSprite.sprite(walkSprites.get(0));

this.walkAction = CCRepeatForever.action(CCAnimate.action(walkAnimation, false));

bear.runAction(walkAction);

Now here are the errors I get:

  1. walkSprites.get(0) line throws a Null Pointer Exception.
  2. CCSpriteFrameCache.sharedSpriteFrameCache().getSpriteFrame("bear" + i + ".png") line prints "CCSpriteFrameCache: Frame '%s' not found" in the logcat

Can anybody help me please, what am I doing wrong?

Thanks in advance.

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$
  1. You use walkSprites.get(0) but you never set it, as your loop goes from 1 to 8.
  2. I suspect that the getSpriteFrame method is more expecting some kind of id defined in your AnimBear.plist rather than a file name.
\$\endgroup\$
0
\$\begingroup\$

I have just updated my code to below and it starts working. Don't know how it starts to work but its working :)

CCSpriteFrameCache.sharedSpriteFrameCache().addSpriteFrames(
            "AnimBear.plist");
    CCSpriteSheet spriteSheet = CCSpriteSheet.spriteSheet("AnimBear.png");

// scene.addChild(spriteSheet);

    ArrayList<CCSpriteFrame> walkSprites = new ArrayList<CCSpriteFrame>();
    for (int i = 1; i <= 8; ++i) {

        walkSprites.add(CCSpriteFrameCache.sharedSpriteFrameCache()
                .spriteFrameByName("bear" + i + ".png"));
        System.out.println("bear image is added");
    }

    CCAnimation walkAnimation = CCAnimation.animation("bear", 0.1f, walkSprites);

    CGSize winSize = CCDirector.sharedDirector().displaySize();
    bear = CCSprite.sprite(walkSprites.get(0));
    bear.setPosition(CGPoint.ccp(winSize.width / 2.0f, winSize.height / 2.0f));

    this.walkAction = CCRepeatForever.action(CCAnimate.action(walkAnimation, false));

// bear.runAction(walkAction);

    addChild(bear);

I have removed "scene.addChild(spriteSheet);" line from the code.

Anybody can use it if want.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .