I'm looking for a way to simplify some of my code so it doesn't take up 18,000 lines. Basically the code is the same code inside 12 switches.
Code:
Switch (1stage){
case 0:
1.animationimages = empty; // this is an array of image filenames, works fine
break;
case 1:
if (1stuff >= 3){
if (1otherstuff >= 3{
1stage++;
1stuff = 0;
1otherstuff = 0;
1.animationimages = newstageimage; // this is an array of image filenames, works fine
}
}
And so on for 112 stages
What I'm having trouble finding how to do is creating an array of each of the 12 items and a forLoop that makes the changes for each of the items in the array. Thus reducing my 18,000 lines of code to only 1700ish.
Any suggestions?
__________________
Nathan Stoltenberg
Lead Programmer - Fractal Fox Studios
Mobile Applications Consultant - The Principal Consulting
You almost certainly want to use an NSArray or NSMutableArray of items instead of 12 separate variables, and you probably want a custom class too. Your variable names are all too generic to tell what's going on, but you want to write something like this:
Code:
for ( (StageClass )*stage in stageArray){
stage.count++;
stage.stuff=0;
stage.otherStuff=0;
stage.animationImages = newStageImage
}
This is going to require writing your own class call StageClass, and setting up stuff and otherStuff as "properties." It's also possible to do it without your own class, with multiple arrays like stuffArray and otherStuffArray, but it's bad design and it will be difficult to maintain.
Better yet, you may be able to put the operation inside a method in StageClass, and just run that method on every object. This would cause every object in the array to call [self myMethod] .
Thats the gist of my code, there are a total of 12 plants, each one has its one Water, Fertilizer, stage and image. The switch is designed to be the end of a day cycle that causes the plants to grow. They grow only if they have enough water and fertilizer.
Then you definitely want to create a Plant class, with a dayCycle method. Then when the sun goes down, you can call this code to to update every plant in the array. Search this board for NSMutableArray if you need help creating the array.
Inside your plant class, you'll have an "init" method that sets up all of the variables, and the "dayCycle" method that updates all of the variables for this *ONE* plant. dayCycle will look something like this:
Alright, I am having alot of issues with creating my own class called StageClass. Do you know of any tutorial on how to create a custom class? Every google search turns up junk about how the iphone is world class ect. I'll keep looking though.
Edit: So I finally found some tutorials, and here's what I'm up to:
The only problem is, I can't get the image to show up at all.
Code:
//
// StageClass.h
//
#import <Foundation/Foundation.h>
@interface StageClass : NSObject
{
// Initialization of the properties
int plantState;
int water;
int harvest;
int harvestCounter;
int fertilizer;
UIImageView *imageView;
}
@property(assign, readwrite) int plantState;
@property(assign, readwrite) int water;
@property(assign, readwrite) int harvest;
@property(assign, readwrite) int harvestCounter;
@property(assign, readwrite) int fertilizer;
@property(retain, readwrite) UIImageView *imageView;
@end
You've done very well! Only a few things I see missing. You don't need plant1 to be an IBOutlet; it's easier to create it in code than in interface builder. To create the plant object, you do this:
Code:
//in (void)viewdidload from ViewController.m:
plant1 = [[StageClass alloc] init];
Also replace your @synthesize lines to just this one:
So when you say I do not need an IBOutlet, I run into problems with that, I use plant1 in more places than just viewdidload. If I only init it in viewdidload, it shows as not found.
Am I supposed to remove it from viewcontroller.h? or just take out the IBOutlet? OR take out the entire IBOutlet line?
Experimentation tells me I just take out the IBOutlet part of the line and it works fine now! Wow, I never thought I would figure out classes!
So when you say I do not need an IBOutlet, I run into problems with that, I use plant1 in more places than just viewdidload. If I only init it in viewdidload, it shows as not found.
Am I supposed to remove it from viewcontroller.h? or just take out the IBOutlet? OR take out the entire IBOutlet line?
Experimentation tells me I just take out the IBOutlet part of the line and it works fine now! Wow, I never thought I would figure out classes!
Success! I was afraid that learning to create classes would be intimidating, but it's necessary in the long run. May as well do it now.
Alright, So remember How I had those arrays that I would use to set the animationImages?
They are set in the Viewcontroller. How would I go about using them in the Class in the endDayCycle?
Alright, So remember How I had those arrays that I would use to set the animationImages?
They are set in the Viewcontroller. How would I go about using them in the Class in the endDayCycle?
Many options; you could make them properties of the viewController, and pass the whole viweController into the endDayCycle method. If you did that, then endDayCycle would look like this:
Another method would be to put them in the application delegate, which you can get to from anywhere in your program with [[UIApplication sharedApplication] delegate] , or you could put them in a C global variable.
So basicly just use them as if the viewController as a class itself and use the objects in it? Sounds like a very simple solution! I'll have to try it out when I go back to work on Monday.
EDIT: When i try and set it up like this:
-(void) endDayCycle: (ProjectViewController*) controller{
It says error: expected ')' before 'ProjectViewController'
__________________
Nathan Stoltenberg
Lead Programmer - Fractal Fox Studios
Mobile Applications Consultant - The Principal Consulting