I need to call a class method to run a Cocos2D scene. I have a game controller in which I will pass in different states (or layers for those familiar with Cocos2D). See the code below:
-(void)startGameWithState:(Class)s {
[[CCDirector sharedDirector] runWithScene: [s scene]];
}
The thing is, this is working fine but generates the following warning:
No '+scene' method found
As best as possible, 开发者_JAVA百科I want to avoid having warnings so how do I fix this?
Update: This is what I did.
-(void)changeStateTo:(Class <GameState>)s {
[[CCDirector sharedDirector] runWithScene: [s scene]];
}
By having the GameState protocol define the +scene method, I don't get any warnings.
Basically, just declare a protocol or abstract class with the scene
class method so the compiler knows it exists.
It's telling you that your generic type -- Class
-- doesn't have a method called scene
. If you're passing in states/layers, those must be of some actual class, right? You need to tell the compiler what class s
actually is so that it can find the scene
method in it.
If you will always pass concrete class the write
-(void)startGameWithState:(ConcreteClass*)s
If you will use different classes without any hierarchy between them use id to avoid the warning
-(void)startGameWithState:(id)s
The second method is not very good because if you will pass the incorrect class object to this method you'll get runtime error. But at the compiling state everything will be fine
If you know that every class passed will be derived from some base class with a +(id) scene
method then pass the base class:
-(void)startGameWithState:(BaseClass*)s
精彩评论