使用NSButton和SpriteKit

使用NSButton和SpriteKit

本文介绍了使用NSButton和SpriteKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个可口可乐程序员,试图通过修改默认的OS X代码来学习Swift / Cocoa / SpriteKit。所以这可能是一个基本问题,但我还没有找到一个解决方案。



我想添加功能,删除所有的Spaceship精灵,鼠标所以我添加这些方法到类:GameScene。

  override func keyDown(theEvent:NSEvent!){
if(theEvent.characters)==p{
removeSprites()
}
}

func removeSprites(){
self.removeAllChildren )
println(self)//在这里跟踪我的问题。
}



因此,当我按'p'和 println(self)告诉我 self
< SKScene> name:'(null)'frame:{{0,0},{1024,768}}






接下来我在Interface Builder中在SKView中设置一个NSButton并将它连接到 class:AppDelegate


$中的IBAction b $ b

  @IBAction func buttonAction(sender:AnyObject){
GameScene()。removeSprites()
}



但是这次sprite不会删除,println报告

 < SKScene> name:'(null)'frame:{{0,0},{1,1}} 

所以我很困惑这个'其他'SKScene。



如何编码NSButton以实现sprite?

解决方案

GameScene()是你的类的一个新的实例,而不是self在你的早期方法中引用的那个。



您的IBAction位于AppDelegate中,而不是在GameScene中,因此您需要保留对场景的引用 - 如果您使用默认模板,在applicationDidFinishLaunching中引用它...继续作为属性,然后你可以在你的action中说self.scene!.removeSprites()。

  var scene:GameScene? = nil 

func applicationDidFinishLaunching(aNotification:NSNotification?){
如果let scene = GameScene.unarchiveFromFile(GameScene)as? GameScene {
self.scene = scene
// ...
}
}
@IBAction func buttonAction(sender:AnyObject){
self。 scene!.removeSprites()
}


I'm a tyro Cocoa programmer trying to learn something about Swift/Cocoa/SpriteKit by modifying the default OS X code. So this is probably a basic question but I haven't been able to find a solution.

I wanted to add functionality to delete all the Spaceship sprites you get after clicking with the mouse so I added these methods to class: GameScene.

override func keyDown(theEvent: NSEvent!) {
    if (theEvent.characters) == "p" {
        removeSprites()
    }
}

func removeSprites() {
    self.removeAllChildren()
    println(self)  // Here to track down my problem.
}

So it works when I press 'p' and println(self) tells me self is <SKScene> name:'(null)' frame:{{0, 0}, {1024, 768}}


So next I set-up an NSButton in SKView in Interface Builder and connected it to an IBAction in class: AppDelegate

@IBAction func buttonAction(sender: AnyObject) {
   GameScene().removeSprites()
    }

However this time the sprites aren't removed and the println reports

<SKScene> name:'(null)' frame:{{0, 0}, {1, 1}}

So I'm confused about this 'other' SKScene.

How to I code the NSButton to effect a sprite?

解决方案

GameScene() is a new instance of your class, not the one that "self" is referring to in your earlier method.

Your IBAction is in the AppDelegate, not in the GameScene, so you need to keep a reference to the scene - if you're using the default template, there should be a reference to it in applicationDidFinishLaunching... Hang on to that as a property, then you can say self.scene!.removeSprites() in your 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()
}

这篇关于使用NSButton和SpriteKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 03:00