Initialize sprite by file name or frame name (abstract code)

Hi! I’ve just installed cocos2d v3.0. Great code, guys, thank you for developing this awesome game engine!

I think, client game code should not know details if sprite is initialized with file name or frame name. It allows to prototype game rapidly with image files. After that you can create a single spritesheet. This detail should not change the game code (all those new cc.Sprite(“example.png”) to new cc.Sprite("#example.png")).

So, I wrote a function createSprite:

SpriteManager = cc.Class.extend({

    basePath: "resources/",

    getBasePath: function()
    {
         return this.basePath;
    },

    createSprite: function(name)
    {
         var frame = cc.spriteFrameCache.getSpriteFrame(name);
         if (frame)
               return new cc.Sprite(frame);
         else
               return new cc.Sprite(this.getBasePath() + name);
    }
});

The problem is that cc.spriteFrameCache.getSpriteFrame(name) writes error to the log that there is no such frame.
And there is no standard function to explicitly check if a frame with such name exists.

So I have a very simple proposal. Please, add a function cc.spriteFrameCache.hasSpriteFrame(name).

Thank you for your proposal.

When we designed the new construction API, we used “#” for sprite frame name and file name without “#”, because it’s possible to have the same name for a sprite frame and a file, and their content can be different. We can’t decide for user which one have higher priority, so we add the “#”.

Thank you for a detailed answer, pandamicro. I agree with that consideration. It would be nice to have cc.spriteFrameCache.hasSpriteFrame method, though. :smiley: That’s only three lines of code. :smiley: