【发布时间】:2025-12-01 16:50:01
【问题描述】:
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 场景拖放到屏幕上的精灵。如果这会影响任何事情,我已经在场景中添加了一个相机。感谢您的帮助。
【问题讨论】:
-
我认为你应该重写 touchesBegan 函数而不是创建你自己的函数。
-
@JohnL 给我这个错误 - 方法不会覆盖其超类中的任何方法
标签: ios swift xcode sprite-kit