Reputation: 11
I'm having some issues getting my game working in Swift/SpriteKit, and can't find answers elsewhere so I was hoping I'd get help here.
I have a screen filled with sksprite nodes, around 1800 of them, and when my finger moves over them they disappear.
I originally had this working using physics, by dragging a hidden node around under my finger, however I've just loaded it on my phone and the performance isn't up to scratch.
I then redesigned it to use 'containsPoint'.
Now the performance is better when at rest (60FPS), however whenever I touch the screen is hangs, and CPU goes up.
This is because it's having to loop through the location of each tile on every move, which is over 1800 calculations per move, and compare it to my current touch.
Please can someone suggest a more elegant solution? I've tried using a dictionary however it complains that CGPOINT isn't hashable.
I'll paste the relevant code below, many thanks!
func addTiles() {
let rowsReq=56, colsReq=32
for i in 0..< rowsReq*colsReq {
...
let tileNode:SKSpriteNode = SKSpriteNode()
tilePosArray.append(tileNode.position)
...
}
}
func moveMarker(location: CGPoint){
markerLayer.position=location
for i in 0..<tilePosArray.count {
if (markerLayer.containsPoint(tilePosArray[i])) {
tileTouched(i)
}
}
}
func tileTouched(tileIdInt: Int) {
tileArray[tileIdInt].alpha=0
checkComplete()
}
Upvotes: 1
Views: 230
Reputation: 47
You could use:
self.nodeAtPoint(point: CGPoint)
or
self.nodesAtPoint(point: CGPoint)
with the touch location to find the node(s) that are being touched, and then the alpha can be changed for the node(s)
Upvotes: 2