What is the purpose .parent in this Mtd?

Go To StackoverFlow.com

0

I am trying to figure out the purpose of what the .parent property achieves in this method and the "get[ClassNameHere]" methods in general that feature this property being called.

-(HudLayer*) getHud
{
return (HudLayer*)[self.parent.parent.parent getChildByTag:kTagHudLayer];
}
2012-04-04 23:22
by GPP
I'd say the main purpose is to convey bad programming practice. "My parent's parent's parent" is making a bold assumption about the layout of the nodes. As soon you change the hierarchy of nodes, the assumption may be wrong and this indirection will fail. That means the getHUD function will silently return nil, leaving you to figure out why - LearnCocos2D 2012-04-06 19:45
Could you explain what exactly he is saying? What does it mean when he says return(HUDLayer*) is he calling the HUDLayer to return the parent node? That whole syntax is just really weird - GPP 2012-04-07 07:37


2

It's not possible to know the details from just this method shown here. But the .parent does return the Cocos parent, sprite or layer, to which the current cocos object belongs as a child. Essentially this code suggests you have a sprite or layer with a child that has more children and more children of those children, and this is a way to find out who the big parent up the stream is, and then get a new child from that parent.

Personally it seems pretty sloppy to me. I'd never code that way myself. Hard to read (as you can see) and having so many parent properties strung together like this is opening a lot of room for bugs. It would be wiser if the top parent did what it needs to do rather than a distant grandchild going up the chain; it breaks the MVC model a bit to do it as shown here.

2012-04-05 04:09
by johnbakers
+1 for the "Personally ..." comment. A highway to inextricably obscure behaviours and bugs - YvesLeBorg 2012-04-05 20:01
Signed and agreed. - LearnCocos2D 2012-04-06 19:45


1

Probably class HudLayer has a parent property that points to another object of the same kind, thus having a parent property pointing to another HudLayer and so on, climbing up for three levels. Then it just sends a getChildByTag message to it.

2012-04-05 01:09
by Manlio
Ads