Android: Problem with loading Sprite from Spritesheet

Hi,

I am loading and using my Spritesheet as follow:

    cc.spriteFrameCache.addSpriteFrames(res.boardSheet_PLIST);

    this.batchNode          =   cc.SpriteBatchNode.create(res.boardSheet_PNG);
    this.addChild(this.batchNode);

    var bottomY             =   50; 
    this.chalkSprite        =   cc.Sprite.create('#chalk.png');
    this.chalkSprite.setPosition(cc.p(size.width * 0.912, bottomY + 10));
    this.batchNode.addChild(this.chalkSprite);

This is working fine in web. But in Android its throwing the following error:

D/cocos2d-x debug info(23983): JS: assets/src/MainMenu.js:30:TypeError: this.chalkSprite is null

Edit 1:
The error happens in the setPosition() line. this.chalkSprite is null in Device. I checked the file name case and it is set right.

Any help please :smile:

Are you sure that the asset is being included when you deploy to your device? the web will reference the resources folder but for devices make sure its properly included

Yes. The assets are being copied to APK. I extracted it and verified.

Actually, Regular sprites are getting rendered. The issue is only with Sprite Sheets.

I currently wonder about the “#”… are you sure that you need that one? How are the sprites named in your .plist file?

Btw. you usually don’t need the SpriteBatchNode - cocos2d implements auto batching which should already handle the drawing.

To access sprites inside a spriteSheet, # has to be added. And Wow, I didnt know cocos2d autobatches the Images :smile:

Anyways, I am now loading the individual sprites rather than loading them through SpriteBatchNode and it’s working fine.

This is what I’m doing:

cc.spriteFrameCache.addSpriteFrames(res.playScene_plist);
var playSceneSpriteSheet = new cc.SpriteBatchNode(res.playScene_png);
this.addChild(playSceneSpriteSheet);

And then I access sprites like this:

this.playerSprite = new cc.PhysicsSprite.create("#player.png");

So how would I do this correctly without using SpriteBatchNode? @saiy2k

Ideally it should be

playSceneSpriteSheet.addChild(this.playerSprite);

But its not working for me!