简体   繁体   English

如何在屏幕上的某个点与用户的手指位置之间建立一条线连接?

[英]How do I make a line connect between a certain point on the screen, to the user's finger location?

I'm making a game, but haven't been able to figure this out yet. 我正在做一个游戏,但还没弄清楚。

So, there's a little zappy ball of electricity at the bottom of the screen, and I want to make it so that – when you touch the screen – it makes a line of electricity, and connects it between the zappy ball and your finger. 因此,屏幕底部有一个小小的Zappy球,我想这样做,以便–触摸屏幕时,它形成一条电线,并将其连接在Zappy球和您的手指之间。 When you move your finger, the line shrinks or enlarges to fit the distance between the two. 当您移动手指时,线条会缩小或放大以适合两者之间的距离。

Think of it like those electricity balls in real life. 就像现实生活中的那些电球一样。 You touch your finger to it, the electricity redirects to your finger. 您用手指触摸它,电流重新定向到您的手指。 When you move your finger, the line of electricity move with it, so it can find the electricity in your finger and pair up with it. 当您移动手指时,电力线随之移动,因此它可以在您的手指中找到电力并与之配对。

You can use an SKShapeNode to draw a line between two points. 您可以使用SKShapeNode在两点之间画一条线。 Create an SKShapeNode in touchesBegan . touchesBegan创建一个SKShapeNode You can then modify its path based on touch location in touchesMoved . 然后,您可以根据touchesMoved touch location修改其路径。

var currentLine : SKShapeNode!

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    let touch = touches.anyObject() as UITouch?
    if let pointOfTouch = touch?.locationInNode(self) {
        currentLine = SKShapeNode()
        let pathToDraw = CGPathCreateMutable();
        CGPathMoveToPoint(pathToDraw, nil, 100.0, 100.0); // Change to zap
        CGPathAddLineToPoint(pathToDraw, nil, pointOfTouch.x, pointOfTouch.y);
        currentLine.path = pathToDraw
        self.addChild(currentLine)
    }
}

override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {

    let touch = touches.anyObject() as UITouch?
    if let pointOfTouch = touch?.locationInNode(self) {
        let pathToDraw = CGPathCreateMutable();
        CGPathMoveToPoint(pathToDraw, nil, 100.0, 100.0); // Change to zap
        CGPathAddLineToPoint(pathToDraw, nil, pointOfTouch.x, pointOfTouch.y);
        currentLine.path = pathToDraw
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM