tags:

views:

50

answers:

2

I have a game which have 5 class/level.Each class contain almost same variables.Now i want to know in which place should i declare my class variable.I have three option-

1.declaring the class variable in corresponding class .h file. 2.declaring a variable .h file(an empty .h file only for declaring variable and then import it to corresponding class.m file). 3.declaring the class variable in class .m file before init method.

Now I want to know which is the correct method.

**each class must import other class .h file because after completing level-1 i have to go level-2.So for scene transition i have to import level-2.h file and so on.

And one fore things if i import a class.h file which has same class variable like my class file ,then is it conflict with my class variable, or what will happen??

A: 

If the classes are that similar, it sounds like they should probably have a common superclass.

Chuck
Should inheritance be involved? It sounds like each level could just be an instance of a level class with different data.
dreamlax
No there is no inheritance,Yea you may be right.I should have use a superclass.But thats not my problem.i just want to know in where should i place class variable if i use different class for different level??
russell
@dreamlax: I may have misunderstood. It sounded to me like he meant that levels are made out of five different kinds of objects (e.g. terrain, creatures, hazards, items and triggers), which could reasonably be thought of as specializations of a general class representing the concept of "things in a level." If we're talking about a separate singleton class for each level, yeah, that's probably a design mistake.
Chuck
+1  A: 

To answer your question: "if i import a class.h file which has same class variable like my class file ,then is it conflict with my class variable, or what will happen??"

No there will not be a conflict. When you define instance variables in a class, it is for objects of that class type only. For example, if you have Class1 which has a property UIColor *color and Class2 has a property UIColor *color, and you include the Class2.h file in class1.m, then you do something like color = [UIColor blueColor]; in a method inside Class1.m you will be referring to class1's color property. To refer to the color property of class2, you would have to instantiate a class2 object and then set it's property like so:

Class2 *c2 = [[Class2 alloc] init];
c2.color = [UIColor greenColor];

Hope that helps a little.


Also, as others have mentioned, you should really just have one game class and either create new instances of the same class or just change the properties of the existing instance. If the 5 classes are very similar, but just harder, you should just have a difficulty property, and then use new instances of the same class with that property set to a higher value.

And, to clear any confusion, let's say you have Class1 again and it's a view controller. You can, inside a method in Class1, create a new Class1 object and display it and it will be a new instance of Class1, with whatever different properties you set.

einsteinx2
thanx a lot.But i can't understand how to make it only with a single class.I am working with cocos 2d.So each of my level class is a subclass of CClayer.I am making transition from one level to another by replacing CCLayer subclass level1,level2 etc.So how will i make transition between if a use only a single CCLayer subclass??
russell
Basically, you can do it the exact same way you are now except instead of creating say a Level1, Level2, Level3 class each having the properties of that level hard coded, just make a Level class and set the properties when you instantiate the object. Then, when you want to make let's say level two, just create a new instance of the Level class, say by doing Level *level2 = [[Level alloc] init]; level2.levelNum = 2; level2.difficulty = 5; etc, then display the level2 object the same way you are displaying your level2 class now. Does that make sense?
einsteinx2