Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何通过触摸移动这些物体?_Swift_Touch - Fatal编程技术网

Swift 如何通过触摸移动这些物体?

Swift 如何通过触摸移动这些物体?,swift,touch,Swift,Touch,在弄清楚如何用触摸来移动这些物体时遇到了一些困难。简单地触摸和移动它们,是我现在关注的问题。下面我将添加GameSecene.swift中的大部分代码,这应该足够有帮助了。。我想。然而,我有另一门课叫Tetromino.swift,如果你需要它来帮助回答我的问题,请告诉我,我会把它加进去的 我添加了touchesbeated、touchesMoved和touchesend,我尝试了一些不同的方法,但无法移动对象,因此它们中的代码不正确。这些物体是由不同的提特罗米诺碎片组成的正方形。提前谢谢 cl

在弄清楚如何用触摸来移动这些物体时遇到了一些困难。简单地触摸和移动它们,是我现在关注的问题。下面我将添加GameSecene.swift中的大部分代码,这应该足够有帮助了。。我想。然而,我有另一门课叫Tetromino.swift,如果你需要它来帮助回答我的问题,请告诉我,我会把它加进去的

我添加了touchesbeated、touchesMoved和touchesend,我尝试了一些不同的方法,但无法移动对象,因此它们中的代码不正确。这些物体是由不同的提特罗米诺碎片组成的正方形。提前谢谢

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)
            }
        }
    }
}
}
class游戏场景:SKScene{
var activeTetromino1=Tetromino()
var activeTetromino2=Tetromino()
var activeTetromino3=Tetromino()
覆盖func didMove(到视图:SKView){
//设置场景
self.anchorPoint=CGPoint(x:0,y:0)
activeTetromino1=Tetromino(drawTetrominoAtPoint(位置:CGPoint(x:frame.width/4,y:frame.height/4)))
activeTetromino2=Tetromino(drawTetrominoAtPoint(位置:CGPoint(x:frame.width/2,y:frame.height/4)))
activeTetromino3=Tetromino(drawTetrominoAtPoint(位置:CGPoint(x:frame.width/4*3,y:frame.height/4)))
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
对于触摸:触摸中的任何对象{
//让位置=触摸。位置(in:self)
drawTetrominoAtPoint(位置:触摸。位置(in:自))
/*
如果activeTetromino1.contains(位置){
activeTetromino1.position=位置
}
*/
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
对于触摸:触摸中的任何对象{
drawTetrominoAtPoint(位置:触摸。位置(in:自))
/*
让位置=触摸。位置(in:self)
如果activeTetromino1.contains(位置){
activeTetromino1.position=位置
}
*/
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
//activeTetromino1=Tetromino(位置=CGPoint(x:frame.width/4,y:frame.height/4))
}
覆盖函数更新(currentTime:CFTimeInterval){
//在渲染每个帧之前调用
}
func DRAWETTROMINOATPOINT(位置:CGPoint){
设t=Tetromino()

对于0中的行。将每个tetromino的部分绘制为场景中单独的子精灵似乎有点尴尬。如果tetromino本身有一个精灵属性,并且在正确的位置添加了所有必要的方块作为子精灵,这将使事情变得容易得多。如果您只是将此精灵添加到场景中,您可以简单地修改
tetromino.sprite.position
任何需要的时候。@T.BenjaminLarsen你是对的,这似乎是一个更好的方法。我对编码和Swift相当陌生。我应该如何使tetromino拥有一个sprite属性,并在正确的位置添加所有必要的方块作为子对象?谢谢你的回答!我可以从你那里猜到逻辑r drawTetrominoAtPoint可以移动到Tetromino类。