Swift Skshapenode Shapewithsplinepoints

Swift SKShapeNode shapeWithSplinePoints

init(splinePoints:count:) takes an UnsafeMutablePointer<CGPoint> (i.e., a C array of CGPoints) and an unsigned integer as its arguments. It also returns an SKShapeNode object not a CGPathRef. Try the following...

let p = SKShapeNode(splinePoints: &path, count: UInt(count))

EDIT: Make the following changes as well

let speed:CGFloat = 1.0

let action = SKAction.followPath(p.path, speed: speed)
sprite.runAction(action)

Inherit SKShapeNode

SKShapeNode.init(circleOfRadius:) is a convenience initializer on SKShapeNode so you can't call it from a Swift initializer. Swift enforces the designated initializer pattern more strictly than Objective C does.

Unfortunately, it appears the designated initializer for SKShapeNode is just init, so you'll need to do something like this:

public class Player: SKShapeNode {
public var playerName : String
private var inventory: [enumObject]

init(nameOfPlayer:String, position:CGPoint, radius: CGFloat) {
playerName = nameOfPlayer
inventory = [enumObject]()

super.init()

self.path = CGPath(ellipseIn: CGRect(origin: .zero, size: CGSize(width: radius, height: radius)), transform: nil)

self.position = position
self.fillColor = SKColor.white
}

// init?(coder:) is the other designated initializer that we have to support
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

The code above works for subclassing SKShapeNode, but given the API that Apple provides and considering how your code might need to change in the future, it might make more sense to create an SKNode subclass that contains one or more SKShapeNodes. In this setup, if you wanted to represent the player as more than just a simple circle, you could simply add additional nodes to the player node.

How to populate an SKShapeNode uniformly

Instead of let x = CGFloat(arc4random_uniform(UInt32(size.width * 20))) - size.width / 4
let y = CGFloat(arc4random_uniform(UInt32(size.height * 90))) - size.height / 100
you define how you want your x to be placed, for example:

let x = (i * size.width)
let y = 0

will place your lines in a single straight line along the x axis where y is 0

How to animate the opacity of a SKShapeNode?

Try this:

    var fadeOut = SKAction.fadeAlpha(to: 0.1, duration: 0.5)
var fadeIn = SKAction.fadeAlpha(to: 0.5, duration: 0.5)

rect.run(SKAction.repeatForever(SKAction.sequence([fingerScalingSequence, fadeOut, fadeIn])))

Adjust the durations as per your liking



Related Topics



Leave a reply



Submit