B
B
BonBon Slick2019-12-07 01:06:41
C++ / C#
BonBon Slick, 2019-12-07 01:06:41

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'");
  }

A piece of code from the basic demo project, the fact is that if there is no sprite or .png in the spritemap, there are also no errors, UB.
If I understood correctly, then this option cuts out every time it is accessed
auto sprite = Sprite::create("dude.png");
. And the other one, see if there is anything in the cache and returns, judging by the locales, a pointer to the sprite, an empty sprite instead of an error, as in the case of create() or createWithSpriteFrameName().
How then to do it right?
UPD. Because they want to update the tags, here's a footnote from the comment
if (sprite != cocos2d::Sprite * )so you can't check it.
The question contains 2
1 - how to make sure that the sprite from the cache always exists, because checking for nullptr is always false when the file does not exist? There lies point to the Sprite type, and it is empty.
2 - How to check in C++ if a variable is a pointer to existing data of a certain type? 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 answer(s)
M
Max Goncharenko, 2019-12-07
@reverse_kacejot

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 question

Ask a Question

731 491 924 answers to any question