I am having trouble fixing this error message down by the CGRect.minX/Y and CGRect.MaxX/Y. "Instance member 'minX' cannot be used on type 'CGRect" is the error. What could I do to fix it?
func spawnNewDisc(){
var randomImageNumber = arc4random()%4
randomImageNumber += 1
let CheeseBlock = SKSpriteNode(imageNamed: "CheeseBlock\(randomImageNumber)")
CheeseBlock.zPosition = 2
CheeseBlock.name = "CheeseObject"
let randomX = random(min: CGRect.minX(gameArea) + CheeseBlock.size.width/2,
max: CGRect.maxX(gameArea) - CheeseBlock.size.width/2)
let randomY = random(min: CGRect.minY(gameArea) + CheeseBlock.size.height/2,
max: CGRect.maxY(gameArea) - CheeseBlock.size.height/2)
CheeseBlock.position = CGPoint(x: randomX, y: randomY)
self.addChild(CheeseBlock)
}
minX
, maxX
, etc are instance properties, which mean they need to be called on a CGRect
instance, not the type itself.
Change
CGRect.minX(gameArea)
to
gameArea.minX
Side Note: It's important in Swift (and other languages as well) to understand the distinction between types and instances. In Swift the convention is to name types starting with capital letters and instances starting with lowercase letters.
Your line of code
let CheeseBlock = SKSpriteNode(imageNamed: "CheeseBlock\(randomImageNumber)")
should be changed to
let cheeseBlock = SKSpriteNode(imageNamed: "CheeseBlock\(randomImageNumber)")
This makes it easier to tell which variables are types and which are instances.