0
votes

many thanks in advance. I was trying to search on StackOverflow or just google it, maybe I used wrong key words or something else, I was unable to find an answer to my question. So I'm new to iOS programing, here is what I did, I set a square in the middle of the screen with 4 different colors(this is a picture), every time I tap on the screen, it will rotate 90 degrees. There are also smaller balls that will come from outside of the screen with the colors, like red ball, green ball, blue ball, etc. When the ball contacts the square with the same color, score one point. Just like the game .

So how should I set up the square with different colors to accomplish this? I thought it can only set one color to a single sprite. or I should put 4 triangles together to make the square?

2

2 Answers

2
votes

You can setup the four colored Square using the following code.

class FourColorSquare : SKNode {

    private var colors : [UIColor] = []
    private var size : CGSize!


    init(colors : [UIColor], size: CGSize) {
        super.init()
        self.colors = colors
        self.size = size
        self.setupNodes()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    func setupNodes() {

        let node1 = SKSpriteNode(color: colors[0], size: self.size)
        node1.position = CGPointMake(0, 0)
        node1.anchorPoint = CGPointMake(1, 0)
        addChild(node1)

        let node2 = SKSpriteNode(color: colors[1], size: self.size)
        node2.position = CGPointMake(0, 0)
        node2.anchorPoint = CGPointMake(0, 0)
        addChild(node2)

        let node3 = SKSpriteNode(color: colors[2], size: self.size)
        node3.position = CGPointMake(0, 0)
        node3.anchorPoint = CGPointMake(0, 1)
        addChild(node3)

        let node4 = SKSpriteNode(color: colors[3], size: self.size)
        node4.position = CGPointMake(0, 0)
        node4.anchorPoint = CGPointMake(1, 1)
        addChild(node4)
    }

    func rotate(angle : CGFloat, animated : Bool) {
        var rotateAction : SKAction!

        if animated {
            rotateAction = SKAction.rotateByAngle(angle, duration: 0.6)
        }
        else {
            rotateAction = SKAction.rotateByAngle(angle, duration: 0)
        }
        for node in self.children as [SKSpriteNode] {

            node.runAction(rotateAction)
        }
    }
}

You can use it like this

let colors = FourColorSquare(colors: [.redColor(),.greenColor(),.blueColor(),.yellowColor()], size: CGSizeMake(50, 50))
colors.position = CGPointMake(200, 100)
addChild(colors)

colors.rotate(-3.14/2, animated: true)

You can setup separate physics bodies to each node in the FourColorSquare to detect collision with each color. Each color should have a separate categoryBitMask to test collision with the each colored ball.

1
votes

You can use path, stroke and fill functions associated with the graphics CGContext and modify the code below to make it draw the pattern you want in the image and fill different sections with colors. The resulting UIImage would be the basis of a new Sprite. See the CGContext Documentation

    import CoreImage
    import CoreGraphics
    int width = 100
    int height = 100
    var colorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue)
    var ctx = CGBitmapContextCreate(nil, width, height, 8, 0, colorspace, bitmapInfo)!
    .
    . draw into your ctx (context) here
    .
    var image = UIImage(CGImage: CGBitmapContextCreateImage(ctx))!