![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
(#1)
|
|
|
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! |
|
|
|
|
(#2)
|
|
|
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.
|
|
|
|
|
(#3)
|
|
|
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? |
|
|
|
|
(#4)
|
|
|
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!
|
|
|
|
|
(#5)
|
||
|
Member
Posts: 80
Join Date: 2008.11
Location: St.Petersburg, Russia
|
2010.01.25, 02:44 AM
Quote:
However, to simplify the code you could use this: Code:
#define SPRITE(name) [[Lib sharedLib] getSprite:name] Code:
Sprite* mario = SPRITE(@"mario"); Sound* jump = SOUND(@"jump"); Alex |
|
|
|
|
|
(#6)
|
|
|
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 | |
|
|