I asked another question before about how to access a global variable in Swift, but realized the global variable did not work for my purposes. Some background:
The game is a math/match game. Player's will have an equation, and they have to get the sheep with the matching answer.
I have sheep that spawn on screen. Each sheep holds a unique int value (1-12) that is generated within my spawnSheep method:
var sheepValue: Int = Int(arc4random_uniform(UInt32(12))+1)
Multiple sheep will spawn on screen- so each will need their own value. Before, the global variable of sheepValue would update all sheep's value to the most recent sheep that spawned, thereby negating any form of 'unique' numbers for all sheep. I have the values converted into strings which will be visible on each sheep.
Here's a snippet of where I'm trying to access the sheepValue outside of the function it was declared in:
func checkCollisions() {
var hitSheep: [SKSpriteNode] = []
enumerateChildNodesWithName("sheep") { node, _ in
let sheep = node as SKSpriteNode
if CGRectIntersectsRect(sheep.frame, self.sandman.frame) {
if sheep.sheepValue == self.equation {
hitSheep.append(sheep)
}
sheep.sheepValue gets an error: 'SKSpriteNode does not have member named "sheepValue"
But as I understand it, I can set properties to my SKSpriteNode (the sheep) and access it in that syntax. But not variables/ints? I'm not sure what I'm doing wrong.
Some other thoughts- could I just use the string values to check sheepValues??
If a sheep spawns with a sheepValue of 5, then '5' would be printed out on the sheep and in the console. This string value is saved, and acts how I wish the int var would act. I'm thinking about just using string values to check if a sheepValue is equal to the player's equation. But that would involved converting more ints into more strings, which is a bit more work.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…