0

CCNode のサブクラスである "Button" クラスがあります。ボタン (コンピューターのボタンなど) は CCLabelBMFont または CCSprite ではないため、CCNode としてサブクラス化しました。さらに、CCLabelBMFont を子 "CCLabelCustom" で拡張しました。これには独自の凝ったロジックがあります。そこで、CCLabelCustom *buttonLabel と CCSprite *buttonSprite を「ボタン」の iVar として移動しました。デフォルトのbuttonLabelまたはbuttonSpriteを使用せずに、「ボタン」をできるだけ抽象的にしたいと考えています。問題は、CCLabelCustom と CCSprite の両方を開始するにはどうすればよいですか? CCLabelCustom には、5 つ以上のパラメーターを持つ長い初期化がいくつかあり、追加のプロパティを割り当てることができます。

// Rough Idea to help elaborate my classes
@interface Button : CCNode

@property (nonatomic, strong) CCLabelCustom *buttonLabel;
@property (nonatomic, strong) CCSprite *buttonSprite;

@end


// Rough Idea to help elaborate my classes
@interface CCLabelCustom : CCLabelBMFont

@property (nonatomic, strong) SomeProperty *someVar;
@property (nonatomic, strong) AnotherProperty *anotherVar;

-(void)fancyMethod;
-(void)initWithSomething:(Something*)something andAnother:(Another*)another alongWith:(Fun*)fun;

@end

私が知らないアーキテクチャはありますか?クラス外から2つのiVarで「ボタン」を初期化できる方法はありますか? 「ボタン」クラス内で定義できるデフォルト値は必要ありません。外層からやりたい。前もって感謝します。

4

1 に答える 1

1

メソッドinitWithSomething:(Something*)something andAnother:(Another*)another alongWith:(Fun*)fun;Buttonクラスに配置します。そして、あなたのこの初期化で、これらのパラメータButtonで も初期 化します。CCLabelCustom

次に、クラス外でボタンを初期化するときに、このメソッドを使用してパラメーターを渡します。

PS .: メソッドinitWithSomethingは ID オブジェクトを返す必要があります。-(id)initWithSomething:

例:

// Outside class
Button *button = [[Button alloc] initWithSomething:something andAnother:another alongWith:fun];

...
// Your Button custom init
- (id)initWithSomething:(Something*)something andAnother:(Another*)another alongWith:(Fun*)fun
{
    self = [self init];
    if (self) {
        self.buttonLabel = [[CCLabelCustom alloc] initWithSomething:something andAnother:another alongWith:fun]
    }
    return self;
}
于 2013-07-24T16:42:24.107 に答える