创建一个包含两个CGPoints SpriteKit Swift的行 [英] Create a line with two CGPoints SpriteKit Swift

查看:81
本文介绍了创建一个包含两个CGPoints SpriteKit Swift的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的应用程序,你触摸一个点,一个精灵跟随一条线穿过该点到屏幕的边缘,无论你在哪里触摸。我想绘制连接精灵的原点(它开始的点)和你触摸的点之间的线段,以及精灵的原点和屏幕边缘的终点之间的线段,这样我就可以看到精灵以及原点,触点和终点的x和y偏移之间的关系。

I am trying to make a simple app where you touch a point, and a sprite follows a line through that point to the edge of the screen, no matter where you touch. I want to draw line segments connecting the origin of the sprite (point where it starts) and the point where you touched, and between the origin of the sprite and the end point at the edge of the screen, so I can visualize the path of the sprite and the relationship between the x and y offsets of the origin, touch point and end point.

希望这不会太混乱。

TL; DR:我需要在两点之间划一条线,我不知道如何使用SpriteKit Swift。

TL;DR: I need to draw a line between two points and I don't know how to do that using SpriteKit Swift.

在此先感谢。

推荐答案

这可以使用CGPath和SKShapeNode完成。

This can be done using CGPath and SKShapeNode.

让我们从CGPath开始。当我们需要使用一系列形状或线条构造路径时,使用CGPath。路径是连接两个点的线。所以要划线:

Lets start with CGPath. CGPath is used when we need to construct a path using series of shapes or lines. Paths are line connecting two points. So to make a line:


  1. moveToPoint:它将路径的当前点设置为指定点。

  2. addLineToPoint:它从当前点到指定点绘制一条直线。

    addCurveToPoint:它根据某些切线和控制点绘制从当前点到指定点的曲线。

您可以在此处查看文档:
http://developer.apple.com/library/mac/#documentation/graphicsimaging/Reference/CGPath/Reference/reference.html

You can check the documentation here: http://developer.apple.com/library/mac/#documentation/graphicsimaging/Reference/CGPath/Reference/reference.html

你是什么需要做的是:

    var path = CGPathCreateMutable()
    CGPathMoveToPoint(path, nil, 100, 100)
    CGPathAddLineToPoint(path, nil, 500, 500)

现在让路径可见,给它像笔画颜色,线宽等属性你在SpriteKit中创建一个SKShapeNode并添加它的路径。

Now to make the path visible, and give it attributes like stroke color, line width etc. you create a SKShapeNode in SpriteKit and add the path to it.

    let shape = SKShapeNode()
    shape.path = path
    shape.strokeColor = UIColor.whiteColor()
    shape.lineWidth = 2
    addChild(shape)

Hope这有助于:)。

Hope this helps :).

这篇关于创建一个包含两个CGPoints SpriteKit Swift的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆