Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

davejas69

macrumors newbie
Original poster
Feb 19, 2009
25
0
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:mad:"myCustomSound" ofType:mad:"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!
 

simon-says

macrumors regular
May 24, 2005
125
7
Louisiana
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

macrumors newbie
Original poster
Feb 19, 2009
25
0
Thanks & question about playing sound...

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:

54799202.jpg


and I declared the action in mainview.h:

87694401.jpg


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

812157ex1.jpg
 

simon-says

macrumors regular
May 24, 2005
125
7
Louisiana
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
Code:
-(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.

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

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

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

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

davejas69

macrumors newbie
Original poster
Feb 19, 2009
25
0
still not working

I think I did what you mentioned and still getting many errors,
is this what you meant?.....

40751604.jpg
 

simon-says

macrumors regular
May 24, 2005
125
7
Louisiana
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

macrumors newbie
Original poster
Feb 19, 2009
25
0
(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

macrumors regular
May 24, 2005
125
7
Louisiana
You will need to move the forward declaration of the pedalSound method.

Code:
#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

macrumors newbie
Original poster
Feb 19, 2009
25
0
It works! Although not sure why...

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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors newbie
Original poster
Feb 19, 2009
25
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.