Trivial question

Hi,

I want to ask which one/both is correct way.

QUESTION (Check the statement in below code)
Creation of sprite is synchronous task(AFAIK) but just wanted to confirm it.
In other words, will it popUp Sprite be already created before executing this statement or the 2nd approach is safer?

I know separating it into multiple functions doesn’t make difference but my questions arises mainly if this->Child(popUp, 1) this could make a difference.
Eg- in 1st case, s1 s2 are being added to SOME child of this which has to exist while in 2nd case s1 s2 are being added to local popup sprite, creation of which is synchronous and which whenever gets added to this as a child, I don’t have to care.

.
.
HelloWorld::init() 
{
  .
  .
  this->createPopUp();  // Some function to add a pop up Sprite in the current layer
  // Pop up name is set as "popup" in createPopUp function

  auto s1 = Sprite::create(...);
  //Set s1 position
  auto s1 = Sprite::create(...);
  //Set s2 position

  // ***QUESTION*** ???
  this->getChildByName("popup")->addChild(s1, 1);
  this->getChildByName("popup")->addChild(s2, 1);
  .
  .
}

OR

.
.
HelloWorld::init() {
  .
  .
  this->createPopUp();  // Some function to add a pop up Sprite in the current layer
  // Pop up name is set as "popup" in createPopUp function
  .
  .
}


void HelloWorld::createPopUp()
{
  auto popupSprite = Sprite::create(...);
  //set popupSprite position
  popupSprite->SetName("popup");
  this->addChild(popupSprite, 1);

  auto s1 = Sprite::create(...);
  //Set s1 position
  auto s1 = Sprite::create(...);
  //Set s2 position

  popupSprite->addChild(s1, 1 );
  popupSprite->addChild(s2, 1 );
}

A bit confusing as your implementation of createPopUp() shown is not the implementation you talk about in the discussion.

If I understand you correctly, you are asking if
this->getChildByName(“popup”)
could fail to return the sprite previously added?

If so, then the answer is ‘no’ - it’s not multi threaded so adding the popup sprite as a child has completed before the function has exited.

Yeah… this is what I asked. Thanks. :slight_smile:

Forgive me for posting this here, but I’m new to the forums, and want to create a new topic, and for the life of me can’t see any way how. Is creating a new topic unavailable to new users? Or am I missing this somewhere?

Thanks!

-Vern

@RagingInverno

Then you should read this form post first hand too. New User Guidance

1 Like