Reputation: 3021
I'm in the process of writing a fairly simple Math's puzzle game for one of my children.
I used the standard XCode SpriteKit Game template which creates an SKView with an SKScene and ViewController.m and MyScene.m files.
I then added a simple UIView to act as a container for a NumberPad 0-9 with UIButtons.
I'm tempted to target MyScene.m as the target for the IBActions as it will use the changes in state from the button presses.
However I'm wondering which class is a better target for the IBActions the ViewController or MyScene. In particular are there any performance implications to either choice.
My main concern is some articles I've seen about issues people have encountered when mixing SpriteKit and UIKit.
Upvotes: 1
Views: 1901
Reputation: 3021
I took Theis' advice and so my view controller, which creates the scene, contains the following code.
ViewController.m
- (IBAction)numberPressed:(id)sender {
UIButton *pressedButton = (UIButton*) sender;
NSString *button = pressedButton.currentTitle;
[scene.keysPressed addObject:button];
}
- (IBAction)clearPressed:(id)sender {
UIButton *pressedButton = (UIButton*) sender;
NSString *button = pressedButton.currentTitle;
[scene.keysPressed addObject:button];
}
And then in my scene code I have declared the keysPressed property, being somewhat paranoid I've made it atomic in case the view controller and scene ever run in different threads.
MyScene.h
@property (strong, atomic) NSMutableArray *keysPressed;
And then in the scenes update method I just check the mutable array I'm using as a stack to see anything has been added and get its value and delete it.
MyScene.m
-(void)update:(CFTimeInterval)currentTime {
...
NSString *keyNum = nil;
if([_keysPressed count] > 0) {
keyNum = [_keysPressed firstObject];
[_keysPressed removeObjectAtIndex:0];
}
So far everything is behaving itself.
Upvotes: 0
Reputation: 2576
For your purposes the performance implications are completely negligible. However from a code point of view I would probably target the viewcontroller, in case you want to swap out the scene but reuse your numberpad. It sounds like a likely development in the future for a math type game.
Upvotes: 1