1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Question about code example from "Appsamuck"

Discussion in 'iPhone/iPad Programming' started by davejas69, Feb 19, 2009.

  1. macrumors newbie

    #1
    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!
     
  2. macrumors newbie

    #2
    Anyone?

    Anyone?
     
  3. macrumors regular

    #3
    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.
     
  4. macrumors newbie

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

    [​IMG]

    and I declared the action in mainview.h:

    [​IMG]

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

    [​IMG]
     
  5. macrumors regular

    #5
    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];
     
  6. macrumors newbie

    #6
    still not working

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

    [​IMG]
     
  7. macrumors regular

    #7
    Everything looks to be right. I have never used the AudioToolbox though, so I'm assuming that's right. What errors are you getting?
     
  8. macrumors newbie

    #8
    I received these errors and a warning:

    [​IMG]


    [​IMG]
     
  9. Moderator

    dejo

    Staff Member

    #9
    Can we see your MainView.h?
     
  10. macrumors newbie

    #10
    (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
     
  11. macrumors regular

    #11
    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
    
     
  12. macrumors newbie

    #12
    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.
     
  13. Moderator

    dejo

    Staff Member

    #13
    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.
     
  14. macrumors newbie

    #14
    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.
     

Share This Page