Accessing a method from anywhere in the project... Help!

Member
Posts: 22
Joined: 2009.08
Post: #1
This may be just me and my lack of knowledge of Objective-C, but here's my problem: I have a class called "SpriteLibrary" that holds an NSMutableDictionary of "Sprite" objects. What I want to do is access the "Sprite" objects in the "SpriteLibrary" object. I only want ONE "SpriteLibrary" and I want any object in the game to be able to access its sprites through a method called
"getSprite: (NSString *)pName". "pName" contains a string that is used as the key to look up the sprite in the dictionary. So something like:
Sprite *mySprite = [SpriteLibrary getSprite:@"marioSprite"];
Would set mySprite to reference the sprite object with the key "marioSprite" in the SpriteLibrary.

How do I allow SpriteLibrary to be used in any class/object in the entire project, and accept a parameter in a method?

Sorry if this is confusing... I'd just like a walkthrough of some sort, please! Thanks in advance!
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
What you want is called a "singleton". You should be able to google up a tutorial on creating 'em in Objective-C.
Quote this message in a reply
Member
Posts: 22
Joined: 2009.08
Post: #3
I've checked it out, but I'm having trouble with putting in new methods with parameters now.

[[SingletonSpriteLibrary spriteLibrary] getSprite:@"marioSprite"];

Is there a way to simplify all that down into what I was looking for above?
[spriteLibrary getSprite:@"marioSprite"];
Or am I going to have to call on the SingletonSpriteLibrary class each time to reference spriteLibrary?
Quote this message in a reply
Member
Posts: 22
Joined: 2009.08
Post: #4
Oh hey, I got it up and running. Thanks for the thread view, though!
Quote this message in a reply
Member
Posts: 93
Joined: 2008.11
Post: #5
SamBaylus Wrote:I've checked it out, but I'm having trouble with putting in new methods with parameters now.

[[SingletonSpriteLibrary spriteLibrary] getSprite:@"marioSprite"];

Is there a way to simplify all that down into what I was looking for above?
[spriteLibrary getSprite:@"marioSprite"];
Or am I going to have to call on the SingletonSpriteLibrary class each time to reference spriteLibrary?

If you go the singleton way you will have to call a class method on your Singleton class to get a real instance of that class and then call your instance methods on that class instance.. The clue here is that the first call gives you the same instance every time.. means you can't create multiple instances of that class by hand but only ask the singleton class to give you the one and only one instance in your app.

However, to simplify the code you could use this:

Code:
#define SPRITE(name) [[Lib sharedLib] getSprite:name]

This ends up with code like this:

Code:
Sprite* mario = SPRITE(@"mario");
Sound* jump = SOUND(@"jump");

HTH,
Alex

TapMania - iPhone StepMania // Human knowledge belongs to the world!
Quote this message in a reply
Member
Posts: 306
Joined: 2009.03
Post: #6
You could also just have a static function that calls the shared objects function
then you would just do
[StaticLib func:@"abc"];
which in there it does
+(void) func:(NSString*)var{
[_singleton internalFunc:var];
}
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Accessing iOS Folders scarypajamas 4 6,232 Oct 17, 2011 02:52 PM
Last Post: scarypajamas
  Accessing the GCC compiler flags Madrayken 6 5,883 Sep 19, 2009 01:18 AM
Last Post: OneSadCookie