【问题标题】:Swift - Call a SKAction (in class) from an IBAction ButtonSwift - 从 IBAction 按钮调用 SKAction(在类中)
【发布时间】:2017-03-22 12:56:50
【问题描述】:

我是 Swift 的新手 - 经过这么多小时的搜索,我需要各位专业人士的帮助 :D

用Swift规划一个小的IOS App,是这样的结构:

  • 游戏视图控制器
  • 游戏场景

在 GameScene 中,我放置了一个小图像:

class GameScene: SKScene {
private var tanzschuhR: SKSpriteNode!
override func didMove(to view: SKView) {

    self.size = CGSize(width: 1024, height: 1024)
    self.backgroundColor = .gray
    self.scaleMode = .aspectFill

    let startPt = CGPoint(x: 500, y: 400)
    tanzschuhR = SKSpriteNode(imageNamed: `SchuhR`)
    tanzschuhR.position = startPt
    tanzschuhR.zPosition = 1

    self.addChild(tanzschuhR)

    func swingPattern1() {
        // move it
        let negDelta = CGVector(dx: -50, dy: -50)
        let action = SKAction.move(by: negDelta, duration: 4)
        tanzschuhR.run(action)
    }
}
}

现在,我只想从 GameViewController 中的 IBAction 按钮启动函数 swingPattern1(),以便开始移动图像同步到正在播放的音频。

这是按钮的一部分:

@IBAction func playIt(_ sender: UIButton) {
    playMultipleSound()
// no idea to call the function swingPattern1()
// always get an error
}

那么,谁能帮帮我?

如果点击按钮,如何调用该函数?

或者还有其他更好的方法来通过点击按钮来启动音频和移动吗?

非常感谢您的帮助...


更新:

我将 var 放在 IBAction 中:

@IBAction func playIt(_ sender: UIButton) {
    playMultipleSound()
    let gameScene = GameScene()
    gameScene.swingPattern1()

}

不知道,如果它是正确的语法 - 抱歉...:D

BUILD&RUN 后,我收到此错误:

应用停止...

【问题讨论】:

  • tanzschuhRnil- 它分配在哪里?
  • 在一开始的课堂上...
  • 所以,swingPattern1() 似乎在 didMove(to view: SKView) 之前被调用,所以 tanzschuhR 没有被赋值。
  • 好的,听起来合乎逻辑。我是否必须将所有内容都放入函数中,或者您有其他解决方案吗?
  • 也许我的 swift 结构从一开始就错了......我只是想在开始音频(跳舞鞋,音乐开始时)时开始动画(移动 spritekit 图像)。按下 IBAction 按钮时音频开始......一切都在一个带有单独 GameScene 的 GameViewcontroller 中。还有其他解决方案吗?

标签: ios swift avaudioplayer ibaction skview


【解决方案1】:

首先,将swingPattern1移出didMove(to view: SKView)

class GameScene: SKScene {
    private var tanzschuhR: SKSpriteNode!
    override func didMove(to view: SKView) {

        self.size = CGSize(width: 1024, height: 1024)
        self.backgroundColor = .gray
        self.scaleMode = .aspectFill

        let startPt = CGPoint(x: 500, y: 400)
        tanzschuhR = SKSpriteNode(imageNamed: `SchuhR`)
        tanzschuhR.position = startPt
        tanzschuhR.zPosition = 1

        self.addChild(tanzschuhR)
    }

    func swingPattern1() {
        // move it
        let negDelta = CGVector(dx: -50, dy: -50)
        let action = SKAction.move(by: negDelta, duration: 4)
        tanzschuhR.run(action)
    }
}

然后在你的视图控制器中……

@IBAction func playIt(_ sender: UIButton) {
    playMultipleSound()

    // Assuming you already have a var gameScene: GameScene
    gameScene.swingPattern1()
}

【讨论】:

  • 非常感谢您的快速帮助,但是:我该如何假设这个变量,在哪个地方?像这样:让 gameScene = GameScene() gameScene.swingPattern1() ?!?我试过这种方法,但函数中出现另一个错误:-> tanzschuhR.run(action) --> AQDefaultDevice (173): skipping input stream 0 0 0x0 致命错误:在展开可选值时意外发现 nil跨度>
猜你喜欢
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 2015-12-09
  • 2021-11-13
  • 2016-10-23
  • 1970-01-01
  • 2016-10-18
  • 1970-01-01
相关资源
最近更新 更多