When you remove a SpriteNode from Parent in SpriteKit, does it automatically remove it from any arrays that it is in? Also, when you remove an element from an array, does every other element shift or is there a gap where the element used to be?
Asked
Active
Viewed 625 times
-1
-
These are things you could find out pretty quick in a playground. Or in the docs. – rickster Jul 06 '15 at 03:38
-
(No, and it shifts.) – rickster Jul 06 '15 at 03:39
-
ugh ok, I need to figure out how I can replace one node with another first and then remove the replaced node so that nothing shifts – Tommy Jul 06 '15 at 03:43
-
In most cases, there is no need to maintain a separate array to keep track of sprites, because there are multiple ways to access a sprite once it's been added to a scene (or another node). See SKNode's `nodeAtPoint`, `nodesAtPoint`, `childNodeWithName`, and `children`. – 0x141E Jul 06 '15 at 04:02
-
@0x141E This is true, however keep in mind it is always much more efficient performance wise to keep track of your sprites in an array or by reference than searching by name, or worse by point. – Epic Byte Jul 06 '15 at 04:06
-
@EpicByte I agree. That's why I included `children`, which _is_ an array. – 0x141E Jul 06 '15 at 04:18
-
@0x141E The problem is, I used a for loops to create 21 nodes and so they all have the same name and the only way I could think to refer to a specific one is to add them into an array and then call them by their array position. As you can see I am inexperienced but I could not think of a better way to do this. Any Ideas? Thanks – Tommy Jul 06 '15 at 04:33
-
@EpicByte Please let me know if you can help also – Tommy Jul 06 '15 at 04:34
-
@Tommy I still don't understand what exactly it is you are trying to do. You can refer to your nodes by index position. If you do this create an enum with integers as raw values that way you are not hard coding the index every where. – Epic Byte Jul 06 '15 at 04:46
-
@EpicByte Once I create an enum, how would I refer to the individual nodes? – Tommy Jul 06 '15 at 04:48
-
1You can use a node's `name` property to uniquely identify each node. For example, set node.name = "sprite\(i)", where `i` is a `for` loop control variable. You can then access each node by name. – 0x141E Jul 06 '15 at 05:09
-
@0x141E ahh that's what I need to do. Is there also a way to check the name of the image of a node? For example, would there be a way to make an if statement like this: if node1.imagename == node2.imagename { do this } – Tommy Jul 06 '15 at 05:13
-
1The code in my comment above should be `node.name = "sprite\(i)"`. Also, you can't compare image names. – 0x141E Jul 06 '15 at 05:55
-
@0x141E Yes, I have that. Now how would I refer to a node with a specific name? – Tommy Jul 06 '15 at 05:57
-
You can loop over the sprites in the array (e.g., `self.children`) and look for a specific sprite with `if node.name == "sprite5"` – 0x141E Jul 06 '15 at 06:02
1 Answers
1
Yes when a node is removed from a parent it is removed from the parent in every way. There is no array automatically created for a parent and its children. If you create an array manually you'd have to remove the element manually as well. When an element is removed it doesn't leave a gap in between. All the elements after it just shift one spot down.
var array = [0,1,2,3] //A new array
array.removeAtIndex(1) //The 1 would remove the element in the second place which is a 1 in this case
//Now the array should look like [0,2,3] instead of [0,,2,3]//The one is not nil. No gap in between.

Aryaman Goel
- 538
- 1
- 4
- 15
-
Thanks, this is what I was looking for. I then assume that if you do array.insert(Element, atIndex: ) then everything after the added element would shift back? – Tommy Jul 08 '15 at 07:57
-
@Tommy Yes if you add an element in the middle of the array, all the elements after it would shift back one spot. – Aryaman Goel Jul 08 '15 at 14:19
-
Nice check out my game!! https://itunes.apple.com/ca/app/save-square-squares-adventures/id967632086?mt=8 – Aryaman Goel Jul 09 '15 at 00:57