質問

I need to create an object that contains several sprites (2 for simplicity: the game object and its' shadow). I need to compose them into the same class, so having the following questions:

  1. what is the best ancestor class to the described one? I used CCNode for this purpose. I overridden its draw method in the following way:

    - (void)draw {
        [super draw];
        [_item draw];
        [_itemShadow draw];
    }
    

and suddenly found that need to change all other CCNode methods in the same way. For ex. change position, visible, etc. in order to change these properties in both Sprites my custom container aggregates:

@interface NBChecker : CCNode {
    CCSprite *_item;
    CCSprite *_itemShadow;
}

@end

There is the other way I see - to make both sprites parent property pointing to self. This should synchronise positions, visibility, etc. for these sprites.

Who has better ideas? Would like to use aggregation and not sure I'm right in my thoughts.

Thanks!

役に立ちましたか?

解決

Scrap the draw code. All you need to do is to add the two sprites as children of the CCNode subclass. Write your logic code in the CCNode subclass and have two ivars for each sprite for easier access (like you already have).

You don't need to draw the sprites yourself as long as they are part of the scene graph.

The sprites will move and rotate relative to their parent automatically. Move the node to move the sprites in unison, etc.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top