1
votes

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.

2
Bit saddened by the down check. As I tried to explain a number of articles I've read say its ok to mix the two. Other articles that its a definite no-no. At least Theis took pity on me.TJA
I'd like to see the article that says mixing Sprite Kit with UIKit is a no-no. There are certain things you can't do (ie display a SK node above a UIView but below another UIView). As for your question there's really only one answer: the view controller. Don't let yourself be fooled by what other people are saying. Unless they say it here and get upvotes for it. ;)LearnCocos2D

2 Answers

1
votes

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.

0
votes

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.