Gee, the last 24 hours have been quite productive for me. I achieved three separate, but important, things.
- Microphone Detection
- Flipping a coin
- Saving
Now, yes, technically I cheated by following code tutorials. But how else am i meant to garner this new knowledge? Guess? Stab the keys alot until things magically work? No, bugger that I like things explained to me.
So lets start with the microphone thing. It’s all in chronological order anyway.
Microphone
So the point of the microphone input was that for the story we may have things like leaves or dust covering the screen and you had to blow them off. How would I do that?
Well I could explain the code I used but this tutorial explains it pretty well anyway.
http://www.mobileorchard.com/tutorial-detecting-when-a-user-blows-into-the-mic/
Well that works all fine and dandy. But, I wanted it to animate an image or something when you blow. Ideally, I would like the animation to pause when you arent blowing but that caused a lot of problems due to how its structured so I dropped that. Of course, if you use an if statement for if blowing or not, not blowing constitutes the view starting up and that tended to crash things when i tried to pause an animation that didnt exist yet.
Anyway, it just required a general if statement and an action to be set up with the animation held in it. Turns out to be very simple! Hooray!
Flipping a coin.
Gary requested this one off me. He asked if it was possible to flick the phone up as if flipping a coin then having a 50/50 chance of going to two different views.
How hard could it be? Just needed an accelerometer class with the outcome being a random function set between the parameters of 1 and 2. It then had to read that outcome and then move to the associated frame.
Apparently there are two types of random function. That alone seems odd to me. One, random(); requires a lot more code and only works one time then the app has to be restarted. Is there a need for this one?
The other arc4random(); works a lot better. One line of code and it is random each time you run it.
Lets go for that one then.
int randomNumber = 1+ arc4random() %(2);
if (randomNumber == 1){
MvtutAppDelegate *delegate = (MvtutAppDelegate *) [[UIApplication sharedApplication] delegate];
ThirdViewController *thirdView = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
[delegate switchView:self.view toView:thirdView.view];
}
if(randomNumber == 2){
MvtutAppDelegate *delegate = (MvtutAppDelegate *) [[UIApplication sharedApplication] delegate];
FirstViewController *thirdView = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
[delegate switchView:self.view toView:thirdView.view];
}
}
Simple yes? Sets up the random function and reads the results. Marvelous.
Making the flip action work is simple too. Just a minor edit to my accelerometer code and we are away. Just had to set it to read from the z axis alone rather than all 3. I also lowered the tolerance so it reacted easier than before.
Saving.
Now this was tricky. Not because it was hard to implement because it wasnt. But because its all explained rather poorly.
What I did was to create a new button on the first screen. It reads continue and when you click it it goes to where you last left off when you closed the app. So you can close the app at any time and return to it exactly where you were.
To do this, you need to use NSUserDefaults.
For the main bulk of the remembering code, I followed along from this tutorial:
http://icodeblog.com/2008/10/03/iphone-programming-tutorial-savingretrieving-data-using-nsuserdefaults/
It handled it quite well. I just needed to get it to pick up what view it was on.
aha, well, what you need to do is to declare an integer on each page (annoyingly) so that when the user visits that page, the string gets updated and holds it in memory.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:2 forKey:@"saveFile"];
Replace 2 with what ever view you are on. Then just use if statements so that when you click the button it knows what view you were on last and sends you to the appropriate view.
I’m betting theres a less codey version to what I did though. Mine had a lot of lines of if statements. Crude but it works. Im pretty sure there is now some dev pro out there who’s head exploded.
wheeee hew. Now with saving sorted, the part I was most dreading seeing as the few explanations of it i found previously were horrible and involved lots of confusing words.
Everything else, interface wise will be making it look pretty now. All the hard engineering work is done I believe. I say that now but theres bound to be something else I need to do.
