I have a ScrollView on my page and I have an array with a bunch of strings in it. I'm trying to display the strings on the page in a column with one string on each line.
For example I run:
let usernameData = documents.map { $0["playername"]! }
print(usernameData)
My Output shows:
[UsernameOne, UsernameTwo, UsernameThree, UsernameFour, UsernameFive]
And when I run:
let usernameData = documents.map { $0["playername"]! }
for usernames in usernameData {
print(usernames)
}
My Output shows:
UsernameOne
UsernameTwo
UsernameThree
UsernameFour
UsernameFive
When I try to add these strings to show on my ScrollView in a column I was trying to just add them to a SKLabelNode and assign my string as text, then I would just increment the y position of the SKLabelNode for each string. For example:
I ran this:
let stringOne = SKLabelNode(fontNamed: "Times New Roman")
let usernameData = documents.map { $0["playername"]! }
for usernames in usernameData {
self.stringOne.text = usernames as? String
self.stringOne.fontSize = 150
self.stringOne.fontColor = SKColor.white
self.stringOne.position = CGPoint(x: self.frame.midX * 1.50, y: self.contrainer.frame.maxY * 2.90)
self.stringOne.zPosition = 100
self.moveableNode.addChild(self.stringOne)
}
But it failed because:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKLabelNode> name:'(null)' text:'UsernameOne' fontName:'Times New Roman' position:{1536, 1978.031982421875}'
How can I display all of the strings that I have stored in this array on my screen in a column with one string on each column? I tried using a ScrollView since I have used it other places and my actual array contains about 100 strings that I need to break apart and display in a column on the users screen. Thanks.
The error occurs because you are adding the same SKLabelNode
again and again in the for loop. Instead, you should be creating a new SKLabelNode
in each iteration:
let usernameData = documents.map { $0["playername"]! }
for usernames in usernameData {
let label = SKLabelNode(fontNamed: "Times New Roman")
label.text = usernames as? String
label.fontSize = 150
label.fontColor = SKColor.white
label.position = CGPoint(x: self.frame.midX * 1.50, y: self.contrainer.frame.maxY * 2.90)
label.zPosition = 100
self.moveableNode.addChild(label)
}