-3

我一直在使用objective-c 和sprite 工具包,但总是使用一个巨大的类来处理所有事情。对于这个应用程序,我需要有多个类。我怎样才能拥有一个名为 MySprite.m 的类,它包含精灵的所有代码,并且能够在 GameScene.m 中的 MySprite.m 中添加精灵或调用方法?

4

1 回答 1

0

下面是一个如何在新类中创建和调用方法的示例:

1)创建一个MySpriteinit方法调用的新类

#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 回答