0

I have a CCScene and several CCSprites added as nodes to it. I then have a subclass (also added to the SAME CCScene class instance), which we call here Whatever, of CCSprite that has some clever beheaviour. I'd like to allow this class to reference the CCSprite nodes added to the main scene and modify some properties of them (e.g. visibility). My current solution is to add a pointer to a NSMutableArray as member variable of Whatever class.

I use the CCScene class to trigger those action via the Whatever class. For instance I call the "TriggerAction" method in the Whatever node at specific times. This action modifies the CCSprites belonging to the same parent CCScene.

I am a newbie but it sounds like something potentially dangerous to me and I am wondering if this is a known design pattern or, worse, a known mistake.

Any experience in using a pattern like this? I have added "Flash" as tag as Cocos2D inherits some concepts from Flash.

EDIT:

Whatever is a special sprite (e.g. planet) that when touched is supposed to show LevelIcon sprites. My problem is that I want to position the levelIcons in respect to the planet position on the screen and the easiest way to do so was by adding them to the scene that contained the planets. I will now explore the solution where they are added only to the planet as Whatever sublcasses.. the problem is in this way I couple them a lot and I cannot recycle the Whatever class in other scenes where I don't require the LevelIcons.

Is this pattern valid

4

1 回答 1

1

这是一个不错的模式,但由于兄弟姐妹保留了其他兄弟姐妹,因此存在泄漏内存的可能性。

更好的解决方案是使精灵成为控制节点的子节点。这样您就不会冒保留循环的风险,并且您不必有一个单独的数组来保留对精灵的引用,因为它们已经在子数组中。您可以使用标签来识别单个精灵。

如果“Whatever”实际上(不需要)显示任何内容,您应该使用 CCNode 作为超类而不是 CCSprite。

于 2012-12-08T09:55:00.113 回答