如何制作三角形而不是矩形?

时间:2015-12-02 14:01:22

标签: ios swift

我目前正在我的第一款iOS游戏中制作一只猫。目前它只是由矩形组成,我真的希望它与三角形更漂亮。

我正在使用此代码创建矩形,它完美无缺。

func loadAppearance() {
    body = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(63, 40))
    body.position = CGPointMake(0, 2)
    addChild(body)

    let skinColor = UIColor(red: 207.0/255.0, green: 193.0/255.0, blue: 168.0/255.0, alpha:1.0)
    let face = SKSpriteNode(color: goldColor, size: CGSizeMake(40, 25))
    face.position = CGPointMake(30, 0)
    body.addChild(face)

    bodyLine = SKSpriteNode(color: UIColor.magentaColor(), size: CGSizeMake(7, 18))
    bodyLine.position = CGPointMake(-21, 11)
    body.addChild(bodyLine)

    bodyLine2 = bodyLine.copy() as! SKSpriteNode
    bodyLine2.position.x = -11
    body.addChild(bodyLine2)

    let eyeColor = UIColor.whiteColor()
    let leftEye = SKSpriteNode(color: eyeColor, size: CGSizeMake(10, 7))
    let rightEye = leftEye.copy() as! SKSpriteNode
    let pupil = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(5,4))
    let leftEar = SKSpriteNode(color: goldColor, size: CGSizeMake(7, 10))
    let rightEar = leftEar.copy() as! SKSpriteNode
    let mouth = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(15, 5))

    leftEar.position = CGPointMake(-10, 15)
    face.addChild(leftEar)

    rightEar.position = CGPointMake(10, 15)
    face.addChild(rightEar)
}

3 个答案:

答案 0 :(得分:2)

看一下SKShapeNode课程 SKShapeNode class reference

它使用路径绘制形状

示例代码:

目标-C

    SKShapeNode* triangle = [SKShapeNode node];
    UIBezierPath* path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake(0.0, 0.0)];
    [path addLineToPoint:CGPointMake(100.0, 0.0)];
    [path addLineToPoint:CGPointMake(100.0, 100.0)];
    triangle.path = path.CGPath;
    triangle.lineWidth = 10.0;
    triangle.strokeColor = [UIColor greenColor];

夫特

    var triangle = SKShapeNode()
    var path = UIBeizerPath()
    path.moveToPoint(CGPointMake(0.0, 0.0))
    path.addLineToPoint(CGPointMake(100.0, 0.0))
    path.addLineToPoint(CGPointMake(100.0, 100.0))
    triangle.path = path.CGPath
    triangle.lineWidth = 10.0
    triangle.strokeColor = UIColor.greenColor()

答案 1 :(得分:2)

创建一个等边三角形。

Swift 4

let path = UIBezierPath()
path.move(to: CGPoint(x: 0.0, y: 50.0))
path.addLine(to: CGPoint(x: 50.0, y: -36.6))
path.addLine(to: CGPoint(x: -50.0, y: -36.6))
path.addLine(to: CGPoint(x: 0.0, y: 50.0))
SKShapeNode(path: path.cgPath)

答案 2 :(得分:0)

正如我所说,你可以使用图像。

将图像添加到纹理图集

  1. 在Xcode中打开Project Navigator cmd + 1
  2. 选择Assets
  3. 在主区域右键单击左侧面板,然后选择 New Sprite Atlas
  4. 已创建名为Sprite的纹理地图集
  5. 现在将三角形的PNG图像从桌面拖到Sprites组
  6. 使用SpriteAtlas

    现在打开与GameScene(或任何其他节点)相关的Swift文件,您可以创建一个Sprite,其中包含您使用此代码添加到Texture Atlas的图像

    let triangle = SKSpriteNode(imageNamed: "Triangle.png")
    

    就是这样!