ios 如何在 Sprite-kit 中画一条线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19092011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:52:29  来源:igfitidea点击:

How to draw a line in Sprite-kit

iosios7sprite-kitskshapenode

提问by Waruna

How can one draw a line in Sprite-kit? For example if I want to draw a line in cocos2d, I could easily using ccDrawLine();

如何在 Sprite-kit 中画一条线?例如,如果我想在 cocos2d 中画一条线,我可以很容易地使用 ccDrawLine();

Is there an equivalent in sprite-kit?

sprite-kit 中是否有等价物?

回答by Rajneesh071

Using SKShapeNodeyou can draw line or any shape.

使用SKShapeNode您可以绘制线条或任何形状。

SKShapeNode *yourline = [SKShapeNode node];
CGMutablePathRef pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, 100.0, 100.0);
CGPathAddLineToPoint(pathToDraw, NULL, 50.0, 50.0);
yourline.path = pathToDraw;
[yourline setStrokeColor:[SKColor redColor]];
[self addChild:yourline];

Equivalent for Swift 4:

相当于 Swift 4:

var yourline = SKShapeNode()
var pathToDraw = CGMutablePath()
pathToDraw.move(to: CGPoint(x: 100.0, y: 100.0))
pathToDraw.addLine(to: CGPoint(x: 50.0, y: 50.0))
yourline.path = pathToDraw
yourline.strokeColor = SKColor.red
addChild(yourline)

回答by Waruna

Using SKShapeNodeI was able to do this.

使用SKShapeNode我能够做到这一点。

// enter code here
SKShapeNode *line = [SKShapeNode node];

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 50.0, 40.0);
CGPathAddLineToPoint(path, NULL, 120.0, 400.0);

line.path = path;
[line setStrokeColor:[UIColor whiteColor]];

[self addChild:line];

回答by John Riselvato

If you only want a line, sort of how people use UIViews for lines (only), then you can just use a SKSpriteNode

如果你只想要一条线,比如人们如何使用 UIViews 作为线(仅),那么你可以只使用 SKSpriteNode

SKSpriteNode* line = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(160.0, 2.0)];
[line setPosition:CGPointMake(136.0, 50.0))];
[self addChild:line];

回答by Crashalot

Swift 3 for drawing line via SKShapeNode:

Swift 3 通过 SKShapeNode 绘制线条:

            // Define start & end point for line
            let startPoint = CGPoint.zero
            let endPoint = CGPoint.zero

            // Create line with SKShapeNode
            let line = SKShapeNode()
            let path = UIBezierPath()
            path.move(to: startPoint)
            path.addLine(to: endPoint)
            line.path = path.cgPath
            line.strokeColor = UIColor.white
            line.lineWidth = 2

回答by nocarrier

Here is the equivalent code in SWIFT:

这是 SWIFT 中的等效代码:

    let pathToDraw:CGMutablePathRef = CGPathCreateMutable()
    let myLine:SKShapeNode = SKShapeNode(path:pathToDraw)

    CGPathMoveToPoint(pathToDraw, nil, 100.0, 100)
    CGPathAddLineToPoint(pathToDraw, nil, 50, 50)

    myLine.path = pathToDraw
    myLine.strokeColor = SKColor.redColor()

    self.addChild(myLine)

Converted from to @Rajneesh071's objective c code sample.

转换为 @Rajneesh071 的目标 c 代码示例。

回答by Voltaire's Ghost

I found this post while trying to draw a line on each mouseDown, of the example xCode / OS X/ Game (aka SpriteKit)/ Application.

我在尝试在每个 mouseDown 上画一条线时发现了这篇文章,例如 xCode / OS X/ Game (aka SpriteKit) / Application。

You can copy/paste this code into GameScene.swift. It should draw a line on each mouse-down event by user. Looks 'etch-a-sketch'-style.

您可以将此代码复制/粘贴到 GameScene.swift 中。它应该在用户的每个鼠标按下事件上画一条线。看起来像“蚀刻草图”风格。

enter image description here

在此处输入图片说明

import SpriteKit

var lastPoint: CGPoint = CGPoint(x: 0.0, y: 0.0)
var newPoint: CGPoint = CGPoint(x: 100.0, y: 100.0)


class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        self.backgroundColor = SKColor.blackColor()
        let myLabel = SKLabelNode(fontNamed:"default")
        myLabel.text = "SKSpriteNode Draw Lines";
        myLabel.fontSize = 15;
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

        self.addChild(myLabel)
    }

    override func mouseDown(theEvent: NSEvent) {
        /* Called when a mouse click occurs */

        let location = theEvent.locationInNode(self)
        newPoint = location

        let pathToDraw:CGMutablePathRef = CGPathCreateMutable()
        let myLine:SKShapeNode = SKShapeNode(path:pathToDraw)


        CGPathMoveToPoint(pathToDraw, nil, lastPoint.x, lastPoint.y)
        CGPathAddLineToPoint(pathToDraw, nil, newPoint.x, newPoint.y)
        lastPoint = newPoint

        myLine.path = pathToDraw
        myLine.strokeColor = SKColor.whiteColor()

        self.addChild(myLine)
    }
}

For the newbies, this is what my xCode project looks like: enter image description here

对于新手来说,这就是我的 xCode 项目的样子: 在此处输入图片说明

For the small handfuliOSof folks. Same code as above ported to touchBeganof the example iOS/ Game (aka SpriteKit) / Application default project. enter image description here

对于少数iOS 用户。相同的代码,上面移植到touchBegan的例子中的iOS/游戏(又名SpriteKit)/应用程序默认的项目。 在此处输入图片说明

Put this code in your GameScene.swift file

将此代码放在您的 GameScene.swift 文件中

import SpriteKit

var lastPoint: CGPoint = CGPoint(x: 0.0, y: 0.0)
var newPoint: CGPoint = CGPoint(x: 100.0, y: 100.0)


class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */
        self.backgroundColor = SKColor.blackColor()
        let myLabel = SKLabelNode(fontNamed:"default")
        myLabel.text = "SKSpriteNode Draw Lines";
        myLabel.fontSize = 15;
        myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));

        self.addChild(myLabel)
    }


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)

        newPoint = location

        let pathToDraw:CGMutablePathRef = CGPathCreateMutable()
        let myLine:SKShapeNode = SKShapeNode(path:pathToDraw)


        CGPathMoveToPoint(pathToDraw, nil, lastPoint.x, lastPoint.y)
        CGPathAddLineToPoint(pathToDraw, nil, newPoint.x, newPoint.y)
        lastPoint = newPoint

        myLine.path = pathToDraw
        myLine.strokeColor = SKColor.whiteColor()
        self.addChild(myLine)
   }}}

sunflower drawing

向日葵图画

Enjoy.

享受。

回答by abanet

Here is my Swift 4 function to add a Line between two points:

这是我的 Swift 4 函数,用于在两点之间添加一条线:

func drawLine(from: CGPoint, to: CGPoint) {
    let line = SKShapeNode()
    let path = CGMutablePath()
    path.addLines(between: [from, to])
    line.path = path
    line.strokeColor = .black
    line.lineWidth = 2
    addChild(line)
}

Hope it helps!!

希望能帮助到你!!