Alexandre Di Pisa
Alexandre Di Pisa

Reputation: 21

How to make triangle instead of a rectangle?

I am currently building a cat in my first iOS game. For the moment it's only made of rectangles and I really want it to be more beautiful with triangles.

I am using this code to create rectangles and it works perfectly.

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)
}

Upvotes: 1

Views: 2688

Answers (3)

Jayden Irwin
Jayden Irwin

Reputation: 977

Creates an equilateral triangle.

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)

Upvotes: 6

Hong Wei
Hong Wei

Reputation: 1407

Take a look at SKShapeNode class SKShapeNode class reference

It uses a path to draw a shape

Sample code:

Objective-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];

Swift

    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()

Upvotes: 2

Luca Angeletti
Luca Angeletti

Reputation: 59526

As I said you can use images.

Adding the images to a Texture Atlas

  1. In Xcode open the Project Navigator cmd + 1
  2. Select Assets
  3. In main area right click on the left panel and select New Sprite Atlas
  4. A Texture Atlas named Sprite has been created
  5. Now drag the PNG image of a triangle from your desktop to the Sprites group

Using the SpriteAtlas

Now open the Swift file related to the GameScene (or any other node), you can create a Sprite containing the image you added to the Texture Atlas with this code

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

That's it!

Upvotes: 0

Related Questions