This is a very newbie question but Im new also in objective-c, I want to call an animation in a game loop, I try to find how in this forum but I find ansers like "make an singleton dictionary for your animations and call them when you need" ???????? I understand the concept but not how to doit can some post the code of a hole singleton with an animation and the call, pleeees :)
How can I use a singleton to call animations
(9 posts) (5 voices)-
Posted 1 year ago #
-
http://lmgtfy.com/?q=objective-c+singleton
i hate to be rude, but this isn't a forum for people to just freely give you code, and alot of it.
i will however give you some snippets
// Register this layer with shared Level manager LevelManager *myManager = [LevelManager sharedLevelManager]; [myManager registerControlLayer:self];i have a layer called ControlLayer, this is my code that i use so that the LevelManager is able to send messages to it.
(declared in the interface of my singleton) ControlLayer *_controlLayer; (the method in my implementation area of the singleton) -(void)registerControlLayer:(ControlLayer*)newLayer { // Registers a pointer to the Control Layer _controlLayer = newLayer; }This is the method within LevelManager. It saves a pointer to the controlLayer in a variable called _controlLayer.
now i can send messages such as
[_controlLayer startGame]; [_controlLayer updateScore];I also have a few methods within the singleton that i use for allowing my hud to interact with my control layer, i merely call a method on the LevelManager that 'forwards' on my data from one layer to the other.
Posted 1 year ago # -
@Bonge if you dont want to help dont post you only block it for real awsers
bounce, bounce, :S
Posted 1 year ago # -
Well some people dont like to share and other dedicate his full time to share like riq, When I get how to implement a singleton that contains an NSMutableDictionary with objects to be called from any where else, I going to post it here.
Posted 1 year ago # -
@MikeTron I really recommend the http://www.71squared.com/iphone-tutorials. It might not be a cocos2d tutorial, but is great anyway :D Check out the project in tutorial 10 (http://www.71squared.com/2009/07/iphone-game-programming-tutorial-10-game-structure/) and you'll find some Singleton classes. There is even a nice SYNTHESIZE_SINGLETON_FOR_CLASS macro.
Posted 1 year ago # -
@MikeTron if given you almost everything you need to send messages from a singleton to an object and vica versa, it is no different with animations.
Just make methods in your sprites that have say
[self runAction:yourSequenceOrAction];Being new to objective-c isn't a great place to start when taking on a framework, you should atleast have a grounding of basic concepts such as object orientated programming, the syntax used in objective-c to send a message to an object
[object message];and messages that pass data
[object message:data];im just asking you to do a little bit of leg work and not expect people to give you the entire code on a plater. I help alot on this forum when i can, but i am not prepared to hand over shovel loads of code. What riq has done is amazing, but i have donated, and am sure i will donate again once my apps are on the store.
The CCDirector is a singleton, if you investigate its code it should also help you.
Posted 1 year ago # -
@Bongeh well you are right I shuld have some prior experience on objective c, but be honest object c is really a very crapy language that a lot of people is using it just for iphone dev.
why you have to make a single istantce object to make a global variable isted of something like
object X = new object;
public static object _Xglobal{get { return X; }set { X = value;}in your main class
why I cant use any NSObject and derivates as a method parameter or at least I try with CCAnimation as parameter with out succes.
I had make a singleton add my NSMutabledictionary as a property, import it, but Im still not been able to make it work, when i call it at the second time it loses the animations, (yea I made my singleton with a lock bloking second treads for inicialization, yep im not doing this:
singleton* _sharSing= [[singleton alloc] init]; when i callback
I use singleton* _shareSing= [sibgleton shareSing];also I dont even want to call the NSMutableDictionary from diferent classes, I just want to call an CCAnimation to be call at any the mthods of my class is that so tuff in object c.
I dont know what is small talk but fore sure was writen by the devilPosted 1 year ago # -
There's been quite a discussion already about this topic but I guess you haven't found any.
I was in on on a discussion a few weeks back:
http://www.cocos2d-iphone.org/forum/topic/4809#post-28625In there David994A posted some tutorials about singleton creation:
http://numbergrinder.com/node/29
http://getsetgames.com/2009/08/30/the-objective-c-singleton/Apple even has the documentation in the developer site as well:
Creating a Singleton Instancewhy you have to make a single istantce object to make a global variable isted of something like object X = new object; public static object _Xglobal{get { return X; }set { X = value;}You can totally just make a global variable. You have all the abilities of doing anything in c it's just singletons are better at dealing with globals in an object oriented way. (Though there are debates about it being an anti-pattern)
I had make a singleton add my NSMutabledictionary as a property, import it, but Im still not been able to make it work, when i call it at the second time it loses the animationsYou probably did not retain so the autorelease pool deallocated that NSMutableDictionary you created. A little understanding of memory management will probably help:
Memory management Programming Guide in objective-cIt really should be a singleton that has methods like:
-(void)addAnimation:(CCAnimation*)animation { [thatAnimationDictionary setValue:animation forKey:animation.name]; } -(CCAnimation*)getAnimationByName:(NSString*)name { return [thatAnimationDictionary valueForKey:name]; }well you are right I shuld have some prior experience on objective c, but be honest object c is really a very crapy language that a lot of people is using it just for iphone devWhat I strongly believe is the hallmark of a good developer is that they are flexible when faced with a certain problem.
I heard of a saying once before, "It's a Poor Carpenter Who Blames His Tools".
A language, programming paradigm or software methodology can change and probably will. So learn to be limber enough to cope when change occurs.
And even with cocos2d development you can use objective-c, c, c++, ruby, lua and probably a few other languages to get the job done.
Posted 1 year ago # -
@MikeTron - I suggest you learn how to program before you attempt to tackle a game with a framework that uses a language you do not even understand. Nobody is going to write the code for you when they could just as easily write it for themselves, and then sell the product in the app store. Forget about the app store, you need to learn basic programming concepts first.
Posted 1 year ago #
Reply
You must log in to post.