遇到一些麻烦,弄清楚如何用触摸移动这些物体。只需触摸它们并移动它们就是我现在关注的问题。下面我将添加来自GameScene.swift的大部分代码,它应该足以帮助..我想。但是,我确实有另一个名为Tetromino.swift的课程,如果您需要帮助回答我的问题,请告诉我,我会将其添加进来!
我添加了touchesBegan,touchesMoved和touchesEnded,我一直在尝试一些不同的东西,但无法让对象移动 - 所以代码在它们中是不正确的。对象是构成不同Tetromino片段的正方形。提前谢谢!
class GameScene: SKScene {
var activeTetromino1 = Tetromino()
var activeTetromino2 = Tetromino()
var activeTetromino3 = Tetromino()
override func didMove(to view: SKView) {
// Setup scene
self.anchorPoint = CGPoint(x: 0, y: 0)
activeTetromino1 = Tetromino(drawTetrominoAtPoint(location: CGPoint(x: frame.width / 4, y: frame.height / 4)))
activeTetromino2 = Tetromino(drawTetrominoAtPoint(location: CGPoint(x: frame.width / 2, y: frame.height / 4)))
activeTetromino3 = Tetromino(drawTetrominoAtPoint(location: CGPoint(x: frame.width / 4 * 3, y: frame.height / 4)))
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in (touches) {
//let location = touch.location(in: self)
drawTetrominoAtPoint(location: touch.location(in: self))
/*
if activeTetromino1.contains(location) {
activeTetromino1.position = location
}
*/
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch: AnyObject in (touches) {
drawTetrominoAtPoint(location: touch.location(in: self))
/*
let location = touch.location(in: self)
if activeTetromino1.contains(location) {
activeTetromino1.position = location
}
*/
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
//activeTetromino1 = Tetromino(position = CGPoint(x: frame.width / 4, y: frame.height / 4))
}
override func update(_ currentTime: CFTimeInterval) {
// Called before each frame is rendered
}
func drawTetrominoAtPoint(location: CGPoint) {
let t = Tetromino()
for row in 0..<t.bitmap.count {
for col in 0..<t.bitmap[row].count {
if t.bitmap[row][col] > 0 {
let block = t.bitmap[row][col]
let square = SKSpriteNode(color: colors[block], size: CGSize(width: blockSize, height: blockSize))
square.anchorPoint = CGPoint(x: 1.0, y: 0)
square.position = CGPoint(x: col * Int(blockSize) + col, y: -row * Int(blockSize) + -row)
square.position.x += location.x
square.position.y += location.y
self.addChild(square)
}
}
}
}
}