Why need Node::getScene()

32 views
Skip to first unread message

Xiaoming Zhang

unread,
May 28, 2014, 4:52:33 AM5/28/14
to cocos2d-js-devel
Hi all,

I found there is a function "Node::getScene()".
Why do we need this function?

Best Regards
Minggo




Ricardo Quesada

unread,
May 29, 2014, 9:23:22 AM5/29/14
to Xiaoming Zhang, cocos2d-js-devel
There is not Scene node per scene.
And Scene is a kind of controller for the scene. As an example, it has Physics logic in it.

So, if you are sprite and want to access the scene of your scene graph, you call "getScene()".


--
You received this message because you are subscribed to the Google Groups "cocos2d JS development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cocos2d-js-dev...@googlegroups.com.
To post to this group, send email to cocos2d-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dawid Drozd

unread,
May 29, 2014, 11:21:48 AM5/29/14
to Ricardo Quesada, Xiaoming Zhang, cocos2d-js-devel
getRoot() ? More flexible
--
Pozdrawiam
Dawid Drozd.

Ricardo Quesada

unread,
May 29, 2014, 11:32:46 AM5/29/14
to Dawid Drozd, Xiaoming Zhang, cocos2d-js-devel
Why do you need a getRoot() ? The semantics are different.

The getRoot() of an orphan sprite is the sprite.
The getScene() of an orphan sprite is nill.

Scene is always root. But root could be a Scene or something else.

Dawid Drozd

unread,
May 30, 2014, 3:40:29 AM5/30/14
to Ricardo Quesada, Xiaoming Zhang, cocos2d-js-devel
Yeah and thats the point. You can check always is this root is scene or orphan or other node. getScene() is limited 

if(dynamic_cast<Scene>(getRoot()) != nullptr)
...

This is only suggestion that making limited method is useless or even better make something like NodeUtils and there put those functions. So now we don't have thousand methods in Node. Also i think that cocos should go to component system because node currently has a lot of information that we don't use.

Poor example

now i can rotate a Node but i don't use it in 95% of time. 
so in component system i could add component responsible for that and still be happy. Currently cocos forbid data driven development in my opinion.
--
Pozdrawiam
Dawid Drozd.

Xiaoming Zhang

unread,
May 30, 2014, 3:59:55 AM5/30/14
to Dawid Drozd, Ricardo Quesada, cocos2d-js-devel
Yep.
Component model will resolve the problem.
But i think it is not the urgent requirement now because it will make a big modification of engine.
May be we can do it in 4.0.

Best Regards
Minggo



Dawid Drozd

unread,
May 30, 2014, 4:07:40 AM5/30/14
to Xiaoming Zhang, Ricardo Quesada, cocos2d-js-devel
It would be great. 
Returning to getScene(). In my opinion it would be better to have such functions in NodeUtils or something because such things are simply Utils because everyone can access to children of Node
--
Pozdrawiam
Dawid Drozd.

Hao Wu

unread,
May 30, 2014, 5:28:28 AM5/30/14
to Dawid Drozd, Xiaoming Zhang, Ricardo Quesada, cocos2d-js-devel
Normally, i would just call Director::getInstance()->getRunningScene();

I don't see the usecase of sprite::getScene()

Dawid Drozd

unread,
May 30, 2014, 5:45:08 AM5/30/14
to Hao Wu, cocos2d-js-devel, Ricardo Quesada, Xiaoming Zhang

Running scene can be diferen than getScene

Pozdrawiam Dawid Drozd.
Wiadomość wysłana z telefonu.

Ricardo Quesada

unread,
May 30, 2014, 12:44:47 PM5/30/14
to Dawid Drozd, Hao Wu, cocos2d-js-devel, Xiaoming Zhang
Yes, a component-based system is something that we are considering. There are pros and cons for it. But yes, we are considering it.
And I have some

And as Dawid said getScene() != Director::getRunningScene(). You can have multiple scenes at the time, but only one will be running.

Regarding getScene() vs getRootNode():
 - If we have a Use Case, flexibility is desired.
 - If we don't have a Use Case, flexibility is not desired ( over-design / over-engineer )

What is the Use Case for getRootNode() ? (a real-world scenario, not an hypothetical Use Case).
If we have a Use Case for it, then we can use this function.
But if there is not Use Case for it, then there is no point in making the API more flexible since:
   - there is not Use Case
   - And it makes the API harder to use and understand.

As an example: What code is easier to understand ? (BTW, dynamic_casts<> are a necessary evil, but I hate them :) )

a)

if(dynamic_cast<Scene>(getRoot()) != nullptr)
{}

b)
if(getScene() != nullptr )
{}


Dawid Drozd

unread,
May 31, 2014, 5:39:36 PM5/31/14
to Ricardo Quesada, Hao Wu, cocos2d-js-devel, Xiaoming Zhang
UC:

I have node which is super cape for my character and now i want make world shake when i use it so world is root node maybe currently it is Scene maybe not. Only what i know i whant to shake root node and this doesn't change if something changes in my tree.

UC2:

I have (in simple way) 

Node root;
Node hudLayer;
Node gameLayer;
Node staticLayer; // for example for showing rhings that don't have to follow our character.
root.addChild(hudLayer);
root.addChild(gameLayer);
root.addChild(staticLayer);

Node character;
Node laser;
gameLayer.addChild(character);
staticLayer.addChild(laser);

Now thanks to getRoot() i can easily transform positions to detect collision or other things. Ok you can say that i can transform positions to world position and then detect stuff.


Also i see 1 problem why we see difference between Scene and Node? I mean Scene doesn;t do much more stuff than Node. Even more on Nodes i can make more fun stuff than on scenes. In my game i use only 1 Scene and i write my own "Scene" manager because i can make the same stuff like with Scene but much more.

Also i don't insist about getRoot() or getScene() i'm only talking about it. As i sad i think such methods should be in utils because it is only easier way to do that than writing over and over again loops jumping on parents.

Also thanks cocos2dx team you do a great job. I love this engine for your openness to the community.
--
Pozdrawiam
Dawid Drozd.
Reply all
Reply to author
Forward
0 new messages