如何在Sprite-kit中画一条线

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Sprite-kit中画一条线相关的知识,希望对你有一定的参考价值。

如何在Sprite-kit中划一条线?例如,如果我想在cocos2d中绘制一条线,我可以轻松地使用ccDrawLine();

sprite-kit中有相同的东西吗?

答案

使用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];

相当于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)
另一答案

使用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];
另一答案

如果你只想要一条线,那就是人们如何使用UIViews进行线条(仅限),那么你可以只使用一个SKSpriteNode

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

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
另一答案

这是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)

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

另一答案

我在尝试在每个mouseDown上绘制一条线时发现了这个帖子,例如xCode / OS X / Game(又名SpriteKit)/ Application。

您可以将此代码复制/粘贴到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)
    }
}

对于新手来说,这就是我的xCode项目:enter image description here

对于少数ios的人来说。与上面相同的代码移植到示例iOS / Game(又名SpriteKit)/ Application默认项目的touchBegan。 enter image description here

将此代码放在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

请享用。

另一答案

这是我的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)
}

希望能帮助到你!!

以上是关于如何在Sprite-kit中画一条线的主要内容,如果未能解决你的问题,请参考以下文章

在现代 OpenGL 中画一条线

如何在HTML中画一条线

如何使用 c++ 在带有 opencv 3.0.0 的视频中画一条线

在jetpack compose中画一条线

在自定义视图中画一条线

借助工具栏在 mfc 中画一条线