Answer the question
In order to leave comments, you need to log in
Check variable of type auto for type?
auto newspriteFrame = cocos2d::SpriteFrameCache::getInstance()->getSpriteFrameByName("dude.png"); // берем из кеша предварительно создав его
auto sprite = cocos2d::Sprite::createWithSpriteFrame(newspriteFrame);
//auto sprite = Sprite::create("dude.png"); // создает каждый раз
//auto sprite = Sprite::createWithSpriteFrameName("dude.png"); // вырезает каждый раз
if (sprite == nullptr)
{
problemLoading("'dude.png'");
}
auto sprite = Sprite::create("dude.png");
if (sprite != cocos2d::Sprite * )
so you can't check it. if (sprite != cocos2d::Sprite * )
because mentioned earlier, the pointer returned a certain type, but there is no data, no picture.
Answer the question
In order to leave comments, you need to log in
1. I did not understand the connection of the question with the body of the question.
2. I didn’t understand what UB had to do with it.
3. I didn't understand who cuts whom :)
4. Answering the question in the subject
SpriteFrame* - the type that is derived for newspriteFrame
Sprite* - the type that is derived for sprite (in any case)
It is very easy to check - by the initializing object. In this case, you need to look at the function signature. The type deduced by auto (like any type in pluses) is determined during compilation and does not change at runtime.
The question, as I understand it, is what method of creating a sprite is better to use, right?
As far as I can see from the documentation , getSpriteFrameByName does not create anything, it only looks into the cache. If there is no such name in the cache, nullptr is returned.
Accordingly, from the same documentation it is clear that Sprite::createWithSpriteFrameName will take the value from the cache, if it exists, otherwise it will throw an exception.
Sprite::create - reads and creates each time as you said.
Based on all of the above, if you need to cache a sprite, then read the doc that I threw off and see how to add the sprite to the cache and read it from there (addSpriteFramesWithFile seems to work).
If it makes no sense to cache the sprite, then Sprite::create is your option
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question