私はしばらくの間、objective-c とスプライト キットを使用してきましたが、常に 1 つの巨大なクラスをすべてに使用していました。このアプリでは、複数のクラスが必要です。スプライトのすべてのコードを持ち、スプライトを追加したり、GameScene.m の MySprite.m 内でメソッドを呼び出したりできる MySprite.m という名前のクラスを作成するにはどうすればよいでしょうか?
1 に答える
0
新しいクラスでメソッドを作成して呼び出す方法の例を次に示します。
1)メソッドMySpriteで呼び出される新しいクラスを作成するinit
#import "MySprite.h"
@implementation MySprite
- (id) init {
if (self = [super init]) {
// Add initialization code here, as needed
}
return self;
}
- (void) addSprite {
// Do something here
}
@end
2) MySprite.h で、メソッドとプロパティを宣言します。
#import <SpriteKit/SpriteKit.h>
@interface MySprite : NSObject
// Declare this if you want to add nodes to the scene in your SKScene subclass
@property (nonatomic, weak) SKScene *scene;
// Declare methods to be called externally
- (void) addSprite;
@end
3) GameScene.m で、MySpriteインスタンスを割り当てて初期化する
MySprite *mySprite = [[MySprite alloc] init];
mySprite.scene = self;
// Call a MySprite method
[mySprite addSprite];
于 2015-01-01T00:23:13.307 に答える