Passing simple data between classes

A have a Swift project that moves sprites around the screen. The GameVC has scene size (width, height) values in it.


I have code inside a MoveComponent class in the agentWillUpdate function to keep sprites on the screen by testing max and min limits

and reversing sprite direction if the limits are hit.


The above works perfectly IF I use hard numbers for the scene limits in the agentWillUpdate function.


I have been unable to figure out how to pass the scene height and width parameters from the GameVC to the MoveComponent class file so that I don't have to use hard numbers (actually since (0,0) are the lower limits, those are OK. I only need the upper limits).


What am I missing? It seems like this should be easy. Could someone provide a simple example of how this should be done? I've looked on the web for examples, but some involve sequeing to another VC which is not what I'm doing. So far all I've tried has failed.


Any advice would be appreciated. Thanks in advance.

Replies

Been over a week with no replies and a few hundred views. Decided at first to just declare global variables. That worked, but then I decided to use a struct{ } because the struct has both get and set capabilities. Anyway, both of my own solutions seem a little brute force. I thought there might be a more elegant way to do this.

Have you considered Delegation? That's how I usually achieve what you're attempting. The Swift 5 Programming Language guide has an example here https://docs.swift.org/swift-book/LanguageGuide/Protocols.htmlthat looks like it's very relevant to your need.


Best wishes,


Michaela