I have a problem. I need to call a method of a scene (HellowWorld for instance) from another class (AnotherClass).
If i right
[HellowWorld methodName];
I've got an error
Could you please help me&
A fast, easy to use, free, and community supported 2D game engine
I have a problem. I need to call a method of a scene (HellowWorld for instance) from another class (AnotherClass).
If i right
[HellowWorld methodName];
I've got an error
Could you please help me&
Does the instance of AnotherClass know about the instance of HelloWorld ? If it doesn't they can't communicate in that fashion. Also, it helps to know the error.
What sort of thing are you trying to accomplish? You might want to consider using delegation, notification or a singleton depending on your needs.
I have a class PositionCalc, which calculates position of sprite. And i'd like to call an updateScene method in HellowWorld scene from PositionCalc class.
is it going to work if i make HellowWorld method static?
Create a singlton to share information, and then use notification center to send messages...
Although I don't understand how positioncalc is getting called unless you are threading it, in which case this should be basic stuff...
With no code posted we have no real understanding of what you are doing.
The way you have phrased the question leads me to believe you have no idea how objects work, which will cause some serious havoc later in your development. Maybe you need to read some more about obj-c, or object oriented programming before you begin writing software.
I would also recommend a singleton.
Here is a very simple way of doing one:
#import "Global.h"
@implementation Global
static Global *_instance = nil;
@synthesize _level;
+(Global *)instance
{
if(_instance) return _instance;
@synchronized([Global class])
{
if(!_instance)
{
_instance = [[self alloc] init];
}
return _instance;
}
return nil;
}
@synthesize _playerFix, _pMainGameLayer;
@end
@interface Global : NSObject {
b2Fixture *_playerFix;
MainGameLayer *_pMainGameLayer;
int _level;
}
@property (nonatomic) b2Fixture *_playerFix;
@property (nonatomic,retain) MainGameLayer *_pMainGameLayer;
@property (readwrite) int _level;
+(Global *)instance;
@end
This is something I've been using in my game.
You may also want to look at the observer pattern( a little like delegates ), in fact a good book on design patterns is essential for good software engineering :-)
Regards,
Steve
Why don't simply message the node itself?
[[HelloWorld node] methodName];
Though this is a far outdated topic, I want to share my experiences.
The method I wrote above is a complete idiotism.
So I managed to get a reference to the running scene's node in two steps.
At HelloWorldScene.m in +(id)scene's implemetation:
//Tag the brand new layer with a tag
layer.tag = 42;
Then in any class we can get a reference to the running scene, and we can get it's layer by tag.
//Send message to the scene (to the CCLayer subclass actually).
_node = [[[CCDirector sharedDirector] runningScene] getChildByTag:42];
[(HelloWorld*)_node methodName];
(We can type the node to avoid warning)
The real problem the original poster likely had, is that he was not importing the HelloWorld header, and/or that he was trying to call an instance method on a class.
Your method works well, assuming you can be certain the child with tag 42 is actually an instance of HelloWorld, and will respond to the message you're sending it. I would say the main layers are significant enough that they warrant tracking with a property (and so HelloWorldScene would respond to mainLayer and return the HelloWorld *mainLayer pointer).
Personally I think the real problem the O.P. had is that he just didn't understand basic objective c.
You must log in to post.