I've been using objective-c and sprite kit for a while, but always used one enormous class for everything. For this app, I need to have multiple classes. How can I have a class, lets say named MySprite.m, which would have all the code for the sprite, and the be able to add the sprite or call methods inside MySprite.m in GameScene.m?
Asked
Active
Viewed 68 times
-3
-
You should find a good book or a series of online tutorials. Have a look at [Good resources for learning ObjC](http://stackoverflow.com/q/1374660). The Big Nerd Ranch books are excellent, and lots of people like the Stanford iOS course on iTunes U. Good luck! – jscs Jan 01 '15 at 19:39
1 Answers
0
Here's an example of how to create and call methods in a new class:
1) Create a new class called MySprite
with an init
method
#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) In MySprite.h, declare methods and properties
#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) In GameScene.m, allocate and initialize a MySprite
instance
MySprite *mySprite = [[MySprite alloc] init];
mySprite.scene = self;
// Call a MySprite method
[mySprite addSprite];

0x141E
- 12,613
- 2
- 41
- 54
-
Thanks! I must say that's probably the best answer I've ever gotten – TheCommonGiraffe Jan 01 '15 at 01:52
-
I just have one problem. When I try to add the sprite inside addSprite method using [self addChild:sprite];, i get an error. I know that the error is because I'm using that function outside the GameScene.m file, but how can I get around this? – TheCommonGiraffe Jan 01 '15 at 06:01
-