2

I have:
1. Class GameScene that extends SKSCene:

#import <SpriteKit/SpriteKit.h>  
#import "GameLogic.h"  
#import "Hero.h"  

@interface GameScene : SKScene

-(void) addHeroMovementLineSegmentTo:(CGPoint)newTrajectoryPoint;

@property (nonatomic) Hero * hero;

And class Hero that extends SKSpriteNode:

#import <SpriteKit/SpriteKit.h>  
#import "GameScene.h"  

@interface Hero : SKSpriteNode  

-(void) initHero:(CGPoint) position onScene:(SKScene *) currentScene;  

@property (nonatomic) GameScene * currentScene;  

I want these two classes (Hero and GameScene) to know about each other and use each other's methods (not to mention the XCode's autocomplete functionality for methods writing for both classes. Somehow, this is not allowed in XCode SpriteKit, because of the cyclic dependency, which I can understand, but wish it wasn't there. Any suggestions?

Vladimir Despotovic
  • 3,200
  • 2
  • 30
  • 58
  • 1
    You should post a snippet of your code. Without seeing it, I'd mention that one way of handling all your object properties and functions is to have the SKSpriteNode be a *property* of your Hero class, rather than having your Hero be a subclass of a sprite node. Your Hero class then can contain all kinds of stuff, and you could even set up a delegate pattern between your hero and the scene. Or use notifications and use your game's main view controller (the one containing your scenes) to call functions in both objects. – Chris Slowik Jan 30 '16 at 19:16
  • Updated. I am getting this error: Unknown type name GameScene on this row: @property (nonatomic) GameScene * currentScene; – Vladimir Despotovic Jan 30 '16 at 19:34
  • Thanks. Your answer would pretty much solve this. I wanted to knokw though if this is possible to avoid somehow, but have the same structure (properties in both classes so they can point to one another). – Vladimir Despotovic Jan 30 '16 at 19:36
  • (And not having to change my whole game structure) – Vladimir Despotovic Jan 30 '16 at 19:43
  • The structure is wrong though, so that's why you're getting the error. Try looking into some of the stuff I mentioned. You're going to have to rewrite things to fix your error, sorry! – Chris Slowik Jan 30 '16 at 19:48
  • ok, but do you know how to make two classes interact with each other and not havimg a middle-man class? because this is what i need... – Vladimir Despotovic Jan 30 '16 at 19:51
  • i am mew to xcode and sprite kit, but i think this can be done too. – Vladimir Despotovic Jan 30 '16 at 19:52
  • 3
    [Forward declarations](http://stackoverflow.com/a/5191507/3402095) can solve the problem with cyclical dependencies. Anyways, what you are having here is a bad design, and you should use delegation as pointed already. Another problem with your code (let's assume that dependencies are solved) is that you will create a strong reference cycle like this, because Hero has a strong reference to the GameScene, and a GameScene has a strong reference to the Hero. – Whirlwind Jan 30 '16 at 22:50

1 Answers1

1

I know it is kind of late to answer on this, but using forward declarations in your interface declaration solves the problem for me.