简体   繁体   English

画直线瞄准

[英]Draw straight line for aiming

If you played Angry Birds or any Pool game before you would be familiar with what I'm trying to do, basically what I want is: 如果您在玩《愤怒的小鸟》或任何台球游戏之前不熟悉我想做的事情,那么基本上我想要的是:

1- Draw a line in touchesMoved so that the player can know in which direction he is aiming. 1-在touchesMoved中画一条线,以便玩家知道他瞄准的方向。

2- I want the line to reflect in the right direction when it hits other objects. 2-我希望线条在碰到其他物体时能向正确的方向反射。 So far I have this: 到目前为止,我有这个:

Update: 更新:

   var ref = CGPathCreateMutable()

        override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

            for touch in touches {
                let location = touch.locationInNode(self)

                let path = CGPathCreateMutable()
                CGPathMoveToPoint(ref, nil, position.x - 100 , position.y - 100 )
                CGPathAddLineToPoint(ref, nil, -location.x, -location.y)

                line.removeFromParent()
                line.path = ref

                line.strokeColor = UIColor.redColor()
                line.lineWidth = 20
                line.antialiased = true
                line.fillColor = UIColor.blueColor()
                addChild(line)
            }
        }

I added the - before location so that the line gets drawn in front of the node ao it gives better aiming sense, but that leave me with 2 problems: 我添加了-before位置,以便将线条画在节点的前面,这样可以更好地瞄准,但这给我带来了2个问题:

1- I want the line to have a limit in which it gets in front of the node, but at the same time, I want its end to be the current touch location. 1-我希望这条线有一个限制,使其在节点的前面,但是同时,我希望它的末端是当前的触摸位置。 ( Basically, I want it to go through the node) (基本上,我希望它通过节点)

2- I still don't know how can I make it reflect to the right angle when it hit a wall or something so it gives an idea of the node would bounce off that wall. 2-我仍然不知道当它碰到墙或其他物体时如何使它以正确的角度反射,因此它给出了节点会从该墙反弹的想法。

SpriteKit isn't drawing API. SpriteKit没有绘制API。 In SpriteKit you basically add elements to the scene and animate the elements. 在SpriteKit中,您基本上可以将元素添加到场景并为元素设置动画。 What you need to do is to delete previous line (if any) then add a new line to the scene every frame. 您需要做的是删除上一行(如果有的话),然后每帧向场景中添加一条新行。 Here is the pseudo code to give you idea how to do it: 以下是伪代码,可让您了解如何进行操作:

class ParentNode: SKNode {

   private var lines = [SKShapeNode]()

    /// Called before each frame is rendered
    override func update(currentTime: CFTimeInterval) {
      self.drawLine()
    }

    func drawLine() {
      let _ = self.lines.map { $0.removeFromParent() }
      self.lines.removeAll()

      if let path = self.createDrawingPath() {
         let lineNode = SKShapeNode()
         lineNode.path = path
         lineNode.strokeColor = UIColor.whiteColor()
         lineNode.lineWidth = 3.0

         self.addChild(lineNode)
         self.lines.append(lineNode)
      }
   }
}

In your case just do the similar thing, I've used update to delete/add new line, but I guess you can use touchesMoved(touches:withEvent) to do it. 就您而言,做类似的事情,我已经使用update来删除/添加新行,但是我想您可以使用touchesMoved(touches:withEvent)来做到这一点。

An alternative to deleting and adding a line node every frame, make a SKShapeNode of 1 pixel by 1 pixel, then scale on the x axis the distance of the from pt to the to pt, then rotate it. 一种替代方法是每帧删除和添加一个线节点,制作1个像素乘1个像素的SKShapeNode,然后在x轴上缩放从pt到pt的距离,然后旋转它。 Use let angle = arctan2(P2_y - P1_y , P2_x - P1_x) (I believe arctan2 handles division by 0 for you) to get the angle 使用let angle = arctan2(P2_y - P1_y , P2_x - P1_x) (我相信arctan2会为您除以0)以获取角度

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

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