I have created a game using SpriteKit (GameScene and GameViewController) and Objective-C. Everything there works fine. Within the same App I have created a UIViewController and
Couple of ways to do this. One of them is using NSUserDefaults.
To store data in NSUserDefaults, do this:
// object can be almost anything. Strings, arrays, dictionaries...
NSString *object = @"WellDone";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:object forKey:@"Hamburger"];
[defaults synchronize];
To read NSUserDefaults, do this:
NSString *myString = [[NSUserDefaults standardUserDefaults] objectForKey:@"Hamburger"];
The key being that you use the same key as when you created it.
And of course, read the docs (blah,blah, blah).
I approached this in a different way. Basically:
Here is in code:
Step 1:
class GameCell: UICollectionViewCell {
@IBOutlet weak var myskview: SKView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
if let scene = SKScene(fileNamed: "MyScene") as? MyScene{
scene.scaleMode = .aspectFill
myskview.presentScene(scene)
}
}
}
Step 2:
class MyScene: SKScene {
var gameLabel: SKLabelNode!
var monthValue: String?
var bird: SKSpriteNode?
var floor: SKSpriteNode?
var bgColor: UIColor?
let birdCategory: UInt32 = 0x1 << 0
let floorCategory: UInt32 = 0x1 << 1
override func didMove(to view: SKView) { }
Step 3:
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GameCell", for: indexPath) as! GameCell
let scene = cell.myskview.scene as! MyScene
scene.gameLabel.text = monthComponents[indexPath.row]
Just don't forget to create the label inside the scene using:
gameLabel = scene?.childNode(withName: "gameLabel") as! SKLabelNode
You can check the final result in my git: https://github.com/tennydesign/spriteKitAndCollectionView
Just pardon my dust. =)