How to use the Zposition of UIBezierPath() and SKNode?

173 Views Asked by At

I have a UIView where i draw lines with UIBezierPath() and where i have SKSHapeNode child. Sometimes, the drawing is superimposed with the nodes. When it is the case, i want the node to be upside of the drawing. But even wehn the nodes have a bigger Zpos, the drawing is upside nodes.

I use this script when i want to draw a line :

private func addLine(fromPoint start: CGPoint, toPoint end:CGPoint) {
    let line = CAShapeLayer()
    let linePath = UIBezierPath()
    linePath.move(to: start)
    linePath.addLine(to: end)
    line.zPosition = 80
    line.path = linePath.cgPath
    line.strokeColor = UIColor.black.cgColor
    line.lineWidth = 5.0
    line.lineJoin = CAShapeLayerLineJoin.round
    self.view!.layer.addSublayer(line)
}

And i use this script for the SKSHapeNodes :

private func drawBase(map : Int) {
    for i in 0...maps.xMaps[map].count - 1 {
        let team = maps.mapTeam[map][i]
        let weight = CGFloat(game!.baseWeight[i])
        let base = SKShapeNode(rectOf: CGSize(width: weight, height: weight))
        base.zPosition = 90
        base.position = CGPoint(x: maps.xMaps[map][i], y: maps.yMaps[map][i])

        if team == 0 {
            base.fillColor = .blue
        } else if team == 1 {
            base.fillColor = .red
        } else if team == 2 {
            base.fillColor = .green
        } else if team == 3 {
            base.fillColor = .purple
        }
        mapEntity.append(base)
        self.addChild(base)
    }
}

I call the function here :

func drawMap(map : Int){
    drawRoad(map: map)
    drawBase(map: map)
}

DrawRoad is the function that draw lines :

private func drawRoad(map : Int) {
    for i in 0...maps.xRoad[map].count - 1 {
        let start = CGPoint(x: maps.xRoad[map][i][0], y: self.frame.height - maps.yRoad[map][i][0])
        let end = CGPoint(x: maps.xRoad[map][i][1], y: self.frame.height - maps.yRoad[map][i][1])
        addLine(fromPoint: start, toPoint: end)
    }
}

As you can see i draw the line before adding the SKShapeNode but it do not work...

0

There are 0 best solutions below