触摸功能在 SpriteKit 中不起作用吗
Did touch function not working in SpriteKit
import SpriteKit
class GameScene: SKScene {
var cam: SKCameraNode?
var player: SKSpriteNode?
override func didMove(to view: SKView) {
super.didMove(to: view)
cam = SKCameraNode()
self.camera = cam
self.addChild(cam!)
player = self.childNode(withName: "player") as? SKSpriteNode
}
func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch:UITouch = touches.anyObject() as! UITouch
let touchLocation = touch.location(in: self)
if touchLocation.x < self.frame.size.width / 2 {
let moveRight = SKAction.moveBy(x: 300, y: 0, duration: 1)
player?.run(moveRight)
} else {
let moveLeft = SKAction.moveBy(x: -300, y: 0, duration: 1)
player?.run(moveLeft)
}
}
override func update(_ currentTime: TimeInterval) {
super.update(currentTime)
if let camera = cam, let pl = player {
camera.position = pl.position
}
}
}
这是我的整个游戏场景和我使用 sk 场景拖放到屏幕上的精灵。如果有任何影响,我已经在场景中添加了一个相机。感谢您的帮助。
您需要覆盖 touches begind 函数。它还有助于放入打印语句以显示正在接收触摸。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
print("touch detected")
let touchLocation = touch.location(in: self)
if touchLocation.x < self.frame.size.width / 2 {
let moveRight = SKAction.moveBy(x: 300, y: 0, duration: 1)
player?.run(moveRight)
} else {
let moveLeft = SKAction.moveBy(x: -300, y: 0, duration: 1)
player?.run(moveLeft)
}
}
}
import SpriteKit
class GameScene: SKScene {
var cam: SKCameraNode?
var player: SKSpriteNode?
override func didMove(to view: SKView) {
super.didMove(to: view)
cam = SKCameraNode()
self.camera = cam
self.addChild(cam!)
player = self.childNode(withName: "player") as? SKSpriteNode
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
print("touch detected")
let touchLocation = touch.location(in: self)
if touchLocation.x < (player?.position.x)! {
let moveRight = SKAction.moveBy(x: 300, y: 0, duration: 1)
print("right")
player?.run(moveRight)
} else {
let moveLeft = SKAction.moveBy(x: -300, y: 0, duration: 1)
print("Left")
player?.run(moveLeft)
}
}
}
override func update(_ currentTime: TimeInterval) {
super.update(currentTime)
if let camera = cam, let pl = player {
camera.position = pl.position
}
}
}
我在 JohnL 的帮助下弄明白了。通过添加 if touchLocation.x < (player?.position.x)!而不是 if touchLocation.x < self.frame.size.width / 2
import SpriteKit
class GameScene: SKScene {
var cam: SKCameraNode?
var player: SKSpriteNode?
override func didMove(to view: SKView) {
super.didMove(to: view)
cam = SKCameraNode()
self.camera = cam
self.addChild(cam!)
player = self.childNode(withName: "player") as? SKSpriteNode
}
func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch:UITouch = touches.anyObject() as! UITouch
let touchLocation = touch.location(in: self)
if touchLocation.x < self.frame.size.width / 2 {
let moveRight = SKAction.moveBy(x: 300, y: 0, duration: 1)
player?.run(moveRight)
} else {
let moveLeft = SKAction.moveBy(x: -300, y: 0, duration: 1)
player?.run(moveLeft)
}
}
override func update(_ currentTime: TimeInterval) {
super.update(currentTime)
if let camera = cam, let pl = player {
camera.position = pl.position
}
}
}
这是我的整个游戏场景和我使用 sk 场景拖放到屏幕上的精灵。如果有任何影响,我已经在场景中添加了一个相机。感谢您的帮助。
您需要覆盖 touches begind 函数。它还有助于放入打印语句以显示正在接收触摸。
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
print("touch detected")
let touchLocation = touch.location(in: self)
if touchLocation.x < self.frame.size.width / 2 {
let moveRight = SKAction.moveBy(x: 300, y: 0, duration: 1)
player?.run(moveRight)
} else {
let moveLeft = SKAction.moveBy(x: -300, y: 0, duration: 1)
player?.run(moveLeft)
}
}
}
import SpriteKit
class GameScene: SKScene {
var cam: SKCameraNode?
var player: SKSpriteNode?
override func didMove(to view: SKView) {
super.didMove(to: view)
cam = SKCameraNode()
self.camera = cam
self.addChild(cam!)
player = self.childNode(withName: "player") as? SKSpriteNode
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
print("touch detected")
let touchLocation = touch.location(in: self)
if touchLocation.x < (player?.position.x)! {
let moveRight = SKAction.moveBy(x: 300, y: 0, duration: 1)
print("right")
player?.run(moveRight)
} else {
let moveLeft = SKAction.moveBy(x: -300, y: 0, duration: 1)
print("Left")
player?.run(moveLeft)
}
}
}
override func update(_ currentTime: TimeInterval) {
super.update(currentTime)
if let camera = cam, let pl = player {
camera.position = pl.position
}
}
}
我在 JohnL 的帮助下弄明白了。通过添加 if touchLocation.x < (player?.position.x)!而不是 if touchLocation.x < self.frame.size.width / 2