Ios 在SpriteKit Swift中检测用户触摸
我在检测用户触摸时遇到问题,或者在场景中插入任何节点,并由用户触摸屏幕或节点来表示和使用。 这是我的档案,你能看到我哪里出错了吗Ios 在SpriteKit Swift中检测用户触摸,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,我在检测用户触摸时遇到问题,或者在场景中插入任何节点,并由用户触摸屏幕或节点来表示和使用。 这是我的档案,你能看到我哪里出错了吗 // // FrontMenu.swift // import Foundation import SpriteKit import GameplayKit import UIKit class FrontMenu : SKScene { var button = SKSpriteNode(imageNamed: "pbut")
//
// FrontMenu.swift
//
import Foundation
import SpriteKit
import GameplayKit
import UIKit
class FrontMenu : SKScene {
var button = SKSpriteNode(imageNamed: "pbut")
var cred = SKSpriteNode(imageNamed: "cbut")
override func didMove(to view: SKView) {
button.name = "play"
self.addChild(button)
cred.name = "credit"
self.addChild(cred)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let node : SKNode = self.atPoint(location)
if node.name == "play" {
print("play tapped.")
let gameScene = GameScene(fileNamed: "GameScene")
gameScene?.scaleMode = .aspectFill
self.view?.presentScene(gameScene!, transition: SKTransition.fade(withDuration: 0.86))
}
else if node.name == "credit" {
NSLog("credits tapped")
let frontscreen = SKSpriteNode(imageNamed: "Credit")
frontscreen.position = CGPoint(x: 0, y: 0)
frontscreen.zPosition = 1.9
addChild(frontscreen)
}
if location.x <= -90 && location.y >= -160 {
NSLog("Area hit for play")
}
if location.x >= 80 && location.y <= -40 {
NSLog("Credit area hit")
}
}
}
}
//
//FrontMenu.swift
//
进口基金会
进口SpriteKit
导入游戏工具包
导入UIKit
类FrontMenu:SKScene{
var按钮=SKSpriteNode(图像名称:“pbut”)
var cred=SKSpriteNode(图像名为:“cbut”)
覆盖func didMove(到视图:SKView){
button.name=“播放”
self.addChild(按钮)
cred.name=“信贷”
self.addChild(cred)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
let节点:SKNode=self.atPoint(位置)
如果node.name==“播放”{
打印(“播放点击”)
让GameSecene=GameSecene(文件名为:“GameSecene”)
游戏场景?.scaleMode=.aspectFill
self.view?.presentScene(游戏场景!过渡:SKTransition.fade(持续时间:0.86))
}
如果node.name==“credit”,则为else{
NSLog(“信用分录”)
让frontscreen=SKSpriteNode(图像名为:“信用”)
frontscreen.position=CGPoint(x:0,y:0)
frontscreen.zPosition=1.9
addChild(前屏幕)
}
如果location.x=-160{
NSLog(“游戏区域命中”)
}
如果location.x>=80&&location.y