1

I've created an array in my GameScene instance, and populated it inside didMove(to view: )...

From an instance of an SKSpriteNode, how do I access a value in that array?

So far as I know, from the template, I don't have a reference to the GameScene instance. But I could be way off on this, too.

Confused
  • 6,048
  • 6
  • 34
  • 75
  • This is almost entirely answered by this answer, to a different problem.: http://stackoverflow.com/a/33957745/2109038 – Confused Oct 13 '16 at 09:18
  • What is this array for? And what does this sprite represent? I would assume not just any sprite in the scene needs access to the array. The scenario will dictate possible approaches – Mobile Ben Oct 13 '16 at 14:49
  • I'm sure @MobileBen there's a million different approaches for a million different scenarios. But, at this stage, I simply want to grab elements from that array, modify them, and go from there. Software architecture design is the single most confusing thing in the world, to me, so I set this question to a simple goal. I don't want to burden SO with the entire objective, just windows that open panes for me to see views. yeah... I regret the puns. – Confused Oct 13 '16 at 17:28
  • I took a look at that link and based on what you are describing it seems like it would work. You say it is close but in what respects is it not? – Mobile Ben Oct 13 '16 at 19:10
  • As far as I know, and that's a small pool, it's what I need. I've tested it, and it gives me the access I want. And I'm happy! The bit that confuses me, why is it in a function? – Confused Oct 14 '16 at 01:33
  • He just arbitrarily made it a function. It doesn't have to be. The key points there are you need to make sure to cast your `SKScene` to the scene in question. You'll have access to the scene's properties/methods provided the access level is proper. I myself would not approach it this way, but it will more than likely suffice for what you need. I'd be looking a viable design that can endure changes ;). – Mobile Ben Oct 14 '16 at 02:42
  • I'm (just now) starting the battle with understanding how to have different states in elements of the game world. I'm doing this with if statements. Which feels wrong. But it works, sort of. I'm probably supposed to use enums or something, but figuring out how to do that would take longer than handling the nesting of "ifs...". At my point, making a more viable design would take days of conception and weeks of attempts. If I can just have universal access to anything and everything through dot.syntax, I can at least move forward. – Confused Oct 14 '16 at 03:09
  • Generalized idea is to move state where you need it. So try and move it into objects that are in the scene as possible. Then your scene only manages higher level state. `if` is not bad. A code smell warning is when you find you have say 20 states you're managing. I typically use `switch`. Example would be say you have an `SKSpriteNode` that is a timer that, for whatever reason flashes. The flashing and other state related to that timer should be managed in the `SKSpriteNode`. – Mobile Ben Oct 14 '16 at 03:12
  • Which is not to say I don't wish I had perfect design, but there's no ready source of information that says... "hey, chappo, here's how you use a code architecture design that's cool, and fun, and right!" that I know of. And it would take months of back and forth on SO to find, ask, query and learn how to do it, I think. So... as a designer... I'm very deliberately NOT designing my architecture ;) – Confused Oct 14 '16 at 03:12
  • I am thinking about three states. amTarget, amWrongTarget, amNotNothingMateLeaveMeAlone. Is this best done with enums and switch? – Confused Oct 14 '16 at 03:14
  • Both. `enum` is used to make a type safe way to encapsulate state. `switch` or `if` would be used to test which state you are in and do the appropriate action. – Mobile Ben Oct 14 '16 at 03:18
  • CHEERS! I'll dive back into reading, AGAIN, how these things work. And hope it sticks. enums are new to me. – Confused Oct 14 '16 at 03:30

1 Answers1

2

While the answer you've linked to will solve your problem, it introduces a coupling between the node and the scene which you might regret later. A more robust solution would allow the node to reference whatever scene it happens to be running in, which is a common design pattern in iOS called delegate pattern.

Mark Brownsword
  • 2,287
  • 2
  • 14
  • 23
  • I don't mean for you to fire code at this question, just point to anything that shows how a delegate system might work. I don't really get delegate design patterns. Perhaps not at all. – Confused Oct 13 '16 at 17:30
  • @Confused You can look at this [SO answer](http://stackoverflow.com/questions/39561177/how-can-i-indicate-a-view-which-has-created-with-uikit-from-a-skscene/39564948#39564948) that I posted previously. – Mark Brownsword Oct 13 '16 at 18:56
  • Cheers. This has sent me on a reading journey. Hopefully something will stick. It's not clear to me, at all, why the coupling is bad. I actually like it, and mentally need it. – Confused Oct 14 '16 at 05:17
  • @Confused When you add another scene and try to add your node to this new scene, you will see the problem with coupling! – Mark Brownsword Oct 14 '16 at 05:23
  • Couldn't I then just uncouple it from the first scene, and recouple it with the second scene? – Confused Oct 14 '16 at 06:41
  • @Confused Your `SKNode` would be hardcore to look for `Level1` so when you add the node to `Level2` it is going to crash! – Mark Brownsword Oct 14 '16 at 06:49