快转:打出一个球并将其垂直放在屏幕底部。然后快速按动球时垂直移动球

时间:2018-09-17 05:28:40

标签: swift sprite-kit user-controls collision

你们好!我在这里是新手,也是新程序员。我试图自学敏捷,但遇到了一些问题。我试图使球仅在按球时才垂直移动,所以如果您按屏幕而不是球,球将不会上升。这是我到目前为止所拥有的。

  //Function on vetical movement
func moveVertically (up:Bool) {
    let moveAction = SKAction.moveBy(x: 0, y: 3, duration: 0.50)
    let repeatAction = SKAction.repeatForever(moveAction)
    if up {
        player?.run(repeatAction)
    }else{
        let moveAction = SKAction.moveBy(x: 0, y: -3, duration: 0.01)
        let repeatAction = SKAction.repeatForever(moveAction)
        player?.run(repeatAction)
    }
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let location = touch.previousLocation(in: self)
        let node = self.nodes(at: location).first

        if node?.name == "player"{
            moveVertically(up: true)
        }else {moveVertically(up: false)

    }

}

}

func createPlayer(){

        //create a ball
        let path = CGMutablePath()
        path.addArc(center: CGPoint.zero,
                    radius: 15,
                    startAngle: 0,
                    endAngle: CGFloat.pi * 2,
                    clockwise: true)
        let ball = SKShapeNode(path: path)
        ball.lineWidth = 1
        ball.fillColor = .blue
        ball.strokeColor = .white
        ball.glowWidth = 0.5
        //set the player to ball and manipulate the player position.
        player = ball
        player?.position = CGPoint(x: 0, y: (scene?.size.height)! / -2.5)



        self.addChild(player!)
    }

P.S。我试图使球垂直移动,但是当您按球的右侧时,球也向左移动,反之亦然,但是当我向左说时,我的意思是垂直向上和向左移动。

0 个答案:

没有答案