Attention: iDevGames forum has a new home at http://www.idevgames.com/forums.
Please update your bookmarks and linkage.
This forum has been locked and will be available for archival purposes only.
iDevGames - Mac, iPhone, iPad & iPod Game Developers Forum  


iPhone, iPad & iPod Game Development Game programming topics devoted to the Apple iPhone, iPad & iPod.

 
 
Thread Tools Display Modes
Old
  (#1)
SamBaylus
Member
 
Posts: 22
Join Date: 2009.08
Location: United States
Accessing a method from anywhere in the project... Help! - 2010.01.24, 02:34 AM

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!
   
Old
  (#2)
OneSadCookie
Member
 
Posts: 4,923
Join Date: 2002.04
Location: Darkest California
2010.01.24, 02:46 AM

What you want is called a "singleton". You should be able to google up a tutorial on creating 'em in Objective-C.
   
Old
  (#3)
SamBaylus
Member
 
Posts: 22
Join Date: 2009.08
Location: United States
Yess... - 2010.01.24, 03:28 AM

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?
   
Old
  (#4)
SamBaylus
Member
 
Posts: 22
Join Date: 2009.08
Location: United States
Oh! - 2010.01.24, 04:37 AM

Oh hey, I got it up and running. Thanks for the thread view, though!
   
Old
  (#5)
godexsoft
Member
 
Posts: 80
Join Date: 2008.11
Location: St.Petersburg, Russia
2010.01.25, 02:44 AM

Quote:
Originally Posted by SamBaylus View Post
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!
   
Old
  (#6)
kendric
Member
 
Posts: 306
Join Date: 2009.03
Location: Dallas,USA
2010.02.04, 11:24 AM

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];
}
   
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com
DevServe Network: iDevApps | uDevGames | iDevGames