Sam Sam - 1 year ago 58
Swift Question

Managing GameScenes update method and referencing changing instance properties within other classes

I'm making my first game in Swift and I'm trying to get a

subclass instance (
) to follow or "trail" another
subclass (
) that the user controls. I was able to achieve this by making a
method within the
class that applies a velocity vector to to the trailing body thats the difference of the two
I then call this method inside of GameScenes
function. However, this
isn't always on screen and can spawn at random times in differing quantities. Is there a more effective way of retrieving "live" or updated positions of the
instance within each
instance instead of having to call a move method for each one inside of Update (which ultimately wont work)? I also tried passing the
instance by reference but this still didn't work as the velocity vector never updated from the original positions. Any help would be awesome!

Answer Source

Inside the update method you should be able to call a move method on every trailingSprite in your scene by finding all the nodes with the corresponding name.

self.enumerateChildNodes(withName: "trailingSprite", using: { node, _ in
    if let trailingSprite = node as? TrailingSprite {
        trailingSprite.move(aUser: user)

This would require that you set the name property on your TrailingSprite class: = "trailingSprite"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download