PDA

View Full Version : Question about code example from "Appsamuck"




davejas69
Feb 19, 2009, 10:06 AM
Hi there,
I am very new to programming and learning from examples such as "Appsamuck", in particular an
application/game with a stoplight and a gas pedal. The object is to hit the gas pedal as fast as you can
when the light turns green:

http://appsamuck.com/day6.html

(The project file is downloadable with all the code)

I understand most of this except that I don't know:

* Where is the code to utilize the image of the gas pedal?

* Where is the code to utilize the image of the background?

* Where is the code for handling the touching of the pedal image is located?

I've looked throughout the code files and XIB files without luck. I understand the rest
of the example and it has taught me a lot but I am stuck with this.

Also, I know how to play a short sound:

NSString *path = [[NSBundle mainBundle] pathForResource:@"myCustomSound" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path]
, &soundID);
AudioServicesPlaySystemSound (soundID);


I am wondering, where I would add this (as an IBAction perhaps?) in the code in order to play a
short sound as each of the lights change and another as the pedal is pressed?

Sorry if this is obvious, I am trying to learn from the code examples and by attempting to modify them.
Thanks!



davejas69
Feb 20, 2009, 02:09 PM
Anyone?

simon-says
Feb 21, 2009, 09:43 AM
The gas pedal and the background are both initialized within the MainView.xib file. The background is a regular UIImageView that is set to the size of the screen. The gas pedal is a UIButton so that it accepts events. If you open the inspector (shift-command-I) and click on each of the elements you can see the properties of these objects.

The gas pedal button has an event connection on it for the 'Touch Up Outside' event that is tied to the gasPedalPressed method in MainView. This is where the code that handles when that button is pressed.

As far as playing a sound each time the light is changed you should look in the MainView.m file. You will find the methods associated with NSTimers for the lights, onRedLightTimer and onYellowLightTimer.

davejas69
Feb 21, 2009, 03:22 PM
Thanks so much for the explanation! I could not see the window in
Interface Builder earlier. This time I clicked on the "main view" icon and
"reveal in workspace" under the Tools drop down menu and I see all of the
elements now. I also see where they are linked up.

About playing a sound... I have tried to simply add an action inside the
method that changes the lights and it is not working...

I've also imported the audio toolbox framework:

http://pixelshack.com/img/54799202.jpg

and I declared the action in mainview.h:

http://pixelshack.com/img/87694401.jpg

-----------------------------------------------------------------------------------------------------
Below is the sound added within the method that turns the light green within mainview.m:
-----------------------------------------------------------------------------------------------------

http://pixelshack.com/img/812157ex1.jpg

simon-says
Feb 21, 2009, 09:47 PM
The method you declared pedalSound you gave a return type of IBAction. IBAction's are for Interface Builder and define methods for events from your controls, such as a button press. Since you are not actually calling this method from an event but within an event, you might change your return type to void as it is not an action. Leaving it as an IBAction is fine though, if you start learning more about the language you will discover that IBAction is defined as void.

Define in MainView.h

-(void)pedalSound;


In your MainView.m file you put this new method you created inside onRedLightTimer method. You will need to put this outside the method. Like so.


-(void)onRedLightTimer
{
.....
}

-(void)pedalSound
{
......
}

-(IBAction)gasPedalPressed
{
.....
}


Now whenever you want to call the pedalSound method, just use [self pedalSound];

davejas69
Feb 22, 2009, 02:27 PM
I think I did what you mentioned and still getting many errors,
is this what you meant?.....

http://pixelshack.com/img/40751604.jpg

simon-says
Feb 22, 2009, 09:24 PM
Everything looks to be right. I have never used the AudioToolbox though, so I'm assuming that's right. What errors are you getting?

davejas69
Feb 23, 2009, 06:51 PM
I received these errors and a warning:

http://pixelshack.com/img/357919001a.jpg


http://pixelshack.com/img/318985002a.jpg

dejo
Feb 23, 2009, 07:18 PM
Can we see your MainView.h?

davejas69
Feb 23, 2009, 07:40 PM
(mainview.h)

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MainView : UIView {
NSDate *startDate;
-(void)pedalSound;
IBOutlet UIImageView *stopLight;
}

@property (nonatomic, copy) NSDate *startDate;

- (IBAction)gasPedalPressed;
@end

simon-says
Feb 23, 2009, 10:23 PM
You will need to move the forward declaration of the pedalSound method.


#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface MainView : UIView {
NSDate *startDate;
IBOutlet UIImageView *stopLight;
}

@property (nonatomic, copy) NSDate *startDate;

- (IBAction)gasPedalPressed;

- (void)pedalSound;
@end

davejas69
Mar 2, 2009, 08:37 PM
Finally got this to work.
Thanks especially to Simon-Says for your help.

The difference is, I did it over and when I added the AudioToolbox
frameworks to the Frameworks folder, I left this UN-checked:
"Copy items into destination(s) group folder (if needed)"
Previously, it was checked.

When it was checked, I got 2 build errors stating that
"symbol(s) not found"

Does this make sense or can anyone explain it?
Instead of just moving on after I solve a problem i would much
rather understand why something worked.

dejo
Mar 2, 2009, 09:08 PM
It makes sense because you don't want to add the framework to your project you just want to link to it. Also, it is important that you change the Reference Type to "Relative to Current SDK". The book Beginning iPhone Development explains this need better than I can: "This option will allow the selected framework to change when we change the SDK we're using. The iPhone and iPhone simulator each have their own SDK, and we'll change SDKs when we build the applications for the iPhone itself, instead of for the simulator. With this option, changing SDKs automatically changes which version of the framework we're linking to." Hope that helps.

davejas69
Mar 2, 2009, 09:15 PM
It does help, thank you!
I had previously thought that we were adding the framework to the project
not linking it.

I tried it both on the simulator and my iPod touch and both now work.