osx - Using an NSButton with SpriteKit -
i'm tyro cocoa programmer trying learn swift/cocoa/spritekit modifying default os x code. basic question haven't been able find solution.
i wanted add functionality delete spaceship sprites after clicking mouse added these methods class: gamescene.
override func keydown(theevent: nsevent!) { if (theevent.characters) == "p" { removesprites() } } func removesprites() { self.removeallchildren() println(self) // here track down problem. }
so works when press 'p' , println(self)
tells me self
<skscene> name:'(null)' frame:{{0, 0}, {1024, 768}}
so next set-up nsbutton in skview in interface builder , connected ibaction in class: appdelegate
@ibaction func buttonaction(sender: anyobject) { gamescene().removesprites() }
however time sprites aren't removed , println reports
<skscene> name:'(null)' frame:{{0, 0}, {1, 1}}
so i'm confused 'other' skscene.
how code nsbutton effect sprite?
gamescene() new instance of class, not 1 "self" referring in earlier method.
your ibaction in appdelegate, not in gamescene, need keep reference scene - if you're using default template, there should reference in applicationdidfinishlaunching... hang on property, can self.scene!.removesprites() in action.
var scene: gamescene? = nil func applicationdidfinishlaunching(anotification: nsnotification?) { if let scene = gamescene.unarchivefromfile("gamescene") as? gamescene { self.scene = scene // ... } } @ibaction func buttonaction(sender: anyobject) { self.scene!.removesprites() }
Comments
Post a Comment