###前提・実現したいこと
初めまして。swiftのSpritekitで簡単なゲームを作っているものです。
SpriteNodeを配列に保存し、その配列をappDelegateのNSUserDefaultsに保存して
取り出すということをしたいのですがエラーが出てしまいます。
どなたかご教授いただけませんでしょうか?
よろしくお願いいたします。
###該当のソースコード
import SpriteKit class GameScene:SKScene { var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate let ud = NSUserDefaults.standardUserDefaults() var array:[SKSpriteNode] = [] override func didMoveToView(view: SKView) { self.backgroundColor = UIColor.blackColor() node() print(appDelegate.array1) } override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { } func node() { let node1 = SKSpriteNode(color: UIColor.blueColor(), size: CGSize(width: 100, height: 100)) node1.name = "1" self.addChild(node1) array.append(node1) let node2 = SKSpriteNode(color: UIColor.blueColor(), size: CGSize(width: 100, height: 100)) node2.name = "2" self.addChild(node2) array.append(node2) let node3 = SKSpriteNode(color: UIColor.blueColor(), size: CGSize(width: 100, height: 100)) node3.name = "3" self.addChild(node3) array.append(node3) ud.setObject(array, forKey: "spriteArray") ud.synchronize() } }
import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var array1 = [] func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { array1 = NSUserDefaults.standardUserDefaults().arrayForKey("spriteArray")! return true }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/09 12:13