Question about spriteframes

I want to be able to do the following:

auto frameName = sprite->getSpriteFrame()->getFrameName();

but getting the name of the current sprite frame seems impossible. All I can do is:

auto curFrame = sprite->getSpriteFrame();
if (curFrame == SpriteFrameCache::getInstance()->getSpriteFrameByName(thatFrameYouWant)) 
{
    // do something
}

Is there a better way that I’m missing?

I wonder too, there is Tag and Name and last is empty, well it’s can be used as png name by default… but it’s not. Or I’m missing something too :smiley:

getResourceName() gives me nothing, but I’m using SBX and loading of levels… however it should help you.

Can you show more code about the Sprite and anything else you may be doing?

Are you using a spritesheet?

Have you checked what func I gave returns? It’s solved.

Its not solved. The name and tag should work. They do for me. So the OP posting more code for us to take a look at is important.

Like by getName() is should return it, so I called it as not works. But as I see you didn’t checked getResourceName.

I’l mock up a test case and post so we can see what is going on.

This doesn’t work?

auto alien = cocos2d::Sprite::create("Blue_Front1.png");
alien->setName("MyAlien");
alien->setTag(10011001);
    
std::cout << "Name: " << alien->getName() << std::endl;
std::cout << "Tag: " << alien->getTag() << std::endl;
std::cout << "Resource Name: " << alien->getResourceName() << std::endl;
Name: MyAlien
Tag: 10011001
Resource Name: Blue_Front1.png

You don’t understand the question.

But as again I’ve already answered, you can just use getResourceName()

Yup, you did. I was just making sure everything worked as expected. Thanks for answering too.

Hi and thanks for your responses.

getName, and getResourceName doesn’t work for me (getResourceName is just a “”). I’m using a spritesheet + plist.

SpriteFrameCache::getInstance()->addSpriteFramesWithFile("images/jasmine.plist");
jasmine = Sprite::createWithSpriteFrameName("jasmine0");
auto n = jasmine->getResourceName();
// n is ""

and another thing, in another class which extends a Sprite class and runs a repeating animation like so:

// also uses sprite frames from a spritesheet
sprite->runAction(RepeatForever::create(Animate::create(flyAnim)));

and then in the update do…

auto n = this->getResourceName();

is says n has the name of the png file I put inside sprite->initWithSpriteFrameName("butterfly04.png") even though it’s constantly animated with 4 different images. The output I was expecting was:

"butterfly04.png"
"butterfly03.png"
"butterfly02.png"
"butterfly01.png"
... // repeat

why are line breaks ignored in code in “preformatted text”

what tool did you use to make the spritesheet?

I actually made my own “tool” this time in python because I wanted to be able to use a spritesheet consisting of a bunch of images of a certain size, while being able to decide in what order they appear in the spritesheet (makes it easier to see what frame order they should be animated in later). I’ll attach it here if anyone wants to use it :slight_smile: makeplist.zip (2.1 KB)

Can you send me your resources, plist and the necessary code so I can try this using TexturePacker to create the spritesheet?

It’s cool that you made your own!

it’s late here so I’ll do it tomorrow. where/how should I send it?

btw, I like to use http://free-tex-packer.com/ for creating sprite sheets where the sprite placement does not matter to me. it’s free and works good.

Yea, I now understand why.

bool Sprite::initWithFile(const std::string& filename) {
...
_fileName = filename;
...
}

But if you create from createWithSpriteFrameName it will run only:

Sprite* Sprite::createWithSpriteFrame(SpriteFrame *spriteFrame)
{



}

Because there is no fileName passed into this method, there is no way to return it, so it’s empty.

So this wrong realization in the engine. I think SpriteFrame class should have also own _fileName
What cocos2d-x developers think about this?

I think this should be the way it works too.

another thing related to spriteframes.
if you do…

sprite->setContentSize(newSize);

and then

auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(name); setSpriteFrame(frame);

it resets the size of the sprite… I don’t know if it’s by design, though ¯ \ _ (ツ)_/¯