我正在玩 Swift Playground Brick Breaker。示例代码桨正在通过鼠标移动。现在我想使用键盘左键和右键移动桨。你能给我一些快速的代码吗?
这是 Paddle.swift 代码(通过鼠标移动)
import Foundation
public class Paddle: Sprite {
var collisionSound: GameSound = .warpMono
/// Allows you to move the bar back and forth with touch.
public func enableHorizontalTracking(in scene: Scene) {
scene.trackTouch(withSprite: self)
}
/// Disable touch control of the bar.
public func disableHorizontalTracking(in scene: Scene) {
scene.stopTrackingTouch(withSprite: self)
}
/// Set the bar to its initial value setting.
public init(image: Image) {
super.init(graphicType: .sprite, name: "paddle")
self.image = image
xScale = 1.30
friction = 0.1
interactionCategory = .paddle
collisionCategories = [.ball]
collisionNotificationCategories = [.ball]
setOnCollisionHandler { collision in
let position = collision.spriteB.position
self.playCollisionSound(self.collisionSound, at: position, using: self.collisionSoundSource)
}
disablesOnDisconnect = true
}
var collisionSoundSource = Graphic(shape: .circle(radius: 4), color: .clear, name: "impact")
}