我的代码无法在我的 IPad 上运行/一般情况下无法运行
My code won't work on my IPad / won't work in general
我一直在研究一些使用按钮调用场景的代码。但是它在我的 IPad 上不起作用,我想我可能做错了几件事,有人愿意告诉我如何让它工作吗?
这是我的代码:
import SpriteKit
import UIKit
import PlaygoundSupport
class ButtonNode: SKSpriteNode {
var action: ((ButtonNode) -> Void)?
var isSelected: Bool = false{
didSet{
alpha = isSelected ? 0.8 : 1
}
}
required init(coder: NSCoder){
fatalError("NSCoding not supported")
}
init(texture: SKTexture, size: CGSize){
super.init(texture: texture, color: SKColor.white, size:size)
isUserInteractionEnabled = true
}
override func touchesBegan(with event: NSEvent){
action?(self)
}
override func mouseDown(with event: NSEvent){
action?(self)
}
}
class GameScene: SKScene{
override func didMove(to view: SKView){
}
}
let scene = GameScene(size: CGSize(width: 400, height: 640)
scene.scaleMode = .aspectFill
scene.backgroundColor = .gray
let view = SKView(frame: CGRect(x:0, y:0, width: scene.size.width, height: scene.size.height))
view.presentScene(scene)
PllygroundPage.current,liveView = view
start button = ButtonNode(texture: SKTexture(imageNamed: "start-button"), size: CGSize(width:184, height: 72))
startButton.position = CGPoint(x: 0.0, y: 0.0)
startButton.action = { (button) in
if let scene = IntroCutScene(fileNamed: "IntroCutScene"){
self.scene!.scaleMode = .aspectFill
GameVariables.sceneView.presentScene(scene, transition: SKTransition.moveIn(with: SKTransitionDirection.right, duration: 2.5))
}
}
self.addChild(startButton)
出现的错误信息是:
使用未声明的类型 'NSEvent'
您正在导入 UIKit(正确的,因为您想要 运行 在 iPad 上)。 UIKit 上的事件类型是 UIEvent,而不是 NSEvent。您从错误的平台实施了错误的方法。看起来你已经实现了这个方法:
https://developer.apple.com/documentation/appkit/nsresponder/1531151-touchesbegan
这是你想要的:
https://developer.apple.com/documentation/uikit/uiresponder/1621142-touchesbegan
(依此类推)。
我一直在研究一些使用按钮调用场景的代码。但是它在我的 IPad 上不起作用,我想我可能做错了几件事,有人愿意告诉我如何让它工作吗? 这是我的代码:
import SpriteKit
import UIKit
import PlaygoundSupport
class ButtonNode: SKSpriteNode {
var action: ((ButtonNode) -> Void)?
var isSelected: Bool = false{
didSet{
alpha = isSelected ? 0.8 : 1
}
}
required init(coder: NSCoder){
fatalError("NSCoding not supported")
}
init(texture: SKTexture, size: CGSize){
super.init(texture: texture, color: SKColor.white, size:size)
isUserInteractionEnabled = true
}
override func touchesBegan(with event: NSEvent){
action?(self)
}
override func mouseDown(with event: NSEvent){
action?(self)
}
}
class GameScene: SKScene{
override func didMove(to view: SKView){
}
}
let scene = GameScene(size: CGSize(width: 400, height: 640)
scene.scaleMode = .aspectFill
scene.backgroundColor = .gray
let view = SKView(frame: CGRect(x:0, y:0, width: scene.size.width, height: scene.size.height))
view.presentScene(scene)
PllygroundPage.current,liveView = view
start button = ButtonNode(texture: SKTexture(imageNamed: "start-button"), size: CGSize(width:184, height: 72))
startButton.position = CGPoint(x: 0.0, y: 0.0)
startButton.action = { (button) in
if let scene = IntroCutScene(fileNamed: "IntroCutScene"){
self.scene!.scaleMode = .aspectFill
GameVariables.sceneView.presentScene(scene, transition: SKTransition.moveIn(with: SKTransitionDirection.right, duration: 2.5))
}
}
self.addChild(startButton)
出现的错误信息是: 使用未声明的类型 'NSEvent'
您正在导入 UIKit(正确的,因为您想要 运行 在 iPad 上)。 UIKit 上的事件类型是 UIEvent,而不是 NSEvent。您从错误的平台实施了错误的方法。看起来你已经实现了这个方法:
https://developer.apple.com/documentation/appkit/nsresponder/1531151-touchesbegan
这是你想要的:
https://developer.apple.com/documentation/uikit/uiresponder/1621142-touchesbegan
(依此类推)。